前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >宽数据变长数据

宽数据变长数据

原创
作者头像
用户10989719
发布2024-04-14 23:50:47
960
发布2024-04-14 23:50:47
举报
文章被收录于专栏:RR

title: "宽数据变长数据"

output: html_document

date: "2024-04-14"


R Markdown

代码语言:r
复制
head(iris)
代码语言:r
复制
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
代码语言:r
复制
library(tidyverse)
代码语言:r
复制
## ── Attaching core tidyverse packages ────────────────────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ──────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
```r
代码语言:r
复制
#长数据变宽数据
head(gather(iris,key = "key",value = "value",-Species),10)
```r
代码语言:r
复制
##    Species          key value
## 1   setosa Sepal.Length   5.1
## 2   setosa Sepal.Length   4.9
## 3   setosa Sepal.Length   4.7
## 4   setosa Sepal.Length   4.6
## 5   setosa Sepal.Length   5.0
## 6   setosa Sepal.Length   5.4
## 7   setosa Sepal.Length   4.6
## 8   setosa Sepal.Length   5.0
## 9   setosa Sepal.Length   4.4
## 10  setosa Sepal.Length   4.9
代码语言:r
复制
library(reshape)
```r
代码语言:r
复制
## 
## 载入程辑包:'reshape'
## 
## The following object is masked from 'package:lubridate':
## 
##     stamp
## 
## The following object is masked from 'package:dplyr':
## 
##     rename
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, smiths
```r
代码语言:r
复制
head(melt(iris,id=c("Species")),5)
```r
代码语言:r
复制
##   Species     variable value
## 1  setosa Sepal.Length   5.1
## 2  setosa Sepal.Length   4.9
## 3  setosa Sepal.Length   4.7
## 4  setosa Sepal.Length   4.6
## 5  setosa Sepal.Length   5.0
```r
代码语言:r
复制
head(melt(iris,id=5),5)
```r
代码语言:r
复制
##   Species     variable value
## 1  setosa Sepal.Length   5.1
## 2  setosa Sepal.Length   4.9
## 3  setosa Sepal.Length   4.7
## 4  setosa Sepal.Length   4.6
## 5  setosa Sepal.Length   5.0
```r
代码语言:r
复制
library(tidyr)
#1:4列合并并命名为variable
pivot_longer(iris,cols = 1:4,names_to = "variable",values_to = "count")
```r
代码语言:r
复制
## # A tibble: 600 × 3
##    Species variable     count
##    <fct>   <chr>        <dbl>
##  1 setosa  Sepal.Length   5.1
##  2 setosa  Sepal.Width    3.5
##  3 setosa  Petal.Length   1.4
##  4 setosa  Petal.Width    0.2
##  5 setosa  Sepal.Length   4.9
##  6 setosa  Sepal.Width    3  
##  7 setosa  Petal.Length   1.4
##  8 setosa  Petal.Width    0.2
##  9 setosa  Sepal.Length   4.7
## 10 setosa  Sepal.Width    3.2
## # ℹ 590 more rows
```r

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R Markdown
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档