宽数据变长数据

2024-04-14 23:50:47 浏览数 (2)


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.

0 人点赞