tidyr中的spread()可以跨多个值分布吗? [英] Can spread() in tidyr spread across multiple value?

查看:87
本文介绍了tidyr中的spread()可以跨多个值分布吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iris数据集,首先,我对该数据集进行了一些处理,并将其设置为以下格式

I am using the iris data set, first, I did some manipulation with that data set and make it into the following form

D1 = iris[,c(1,2,5)]
D2 = iris[,c(3,4,5)]
colnames(D1)[1:2] = c('Length','Width')
colnames(D2)[1:2] = c('Length','Width')
D1 = D1 %>% mutate(Part = 'Sepal')
D2 = D2 %>% mutate(Part = 'Petal')
D = rbind(D2,D1)

看起来像

Species  Part Length Width
1  setosa Petal    1.4   0.2
2  setosa Petal    1.4   0.2
3  setosa Petal    1.3   0.2
4  setosa Petal    1.5   0.2
5  setosa Petal    1.4   0.2
6  setosa Petal    1.7   0.4

我想在tidyr中使用spread()函数,使数据集最终看起来像以下格式

I want to use the spread() function in the tidyr to make the data set look like the following format eventually

Measure Part setosa versicolor virginica
Length  Petal 1.4     4.7        6.0

我的工作如下:

D4 = D %>% gather(Measure,value,3:4)

给出

Species  Part Measure value
1  setosa Petal  Length   1.4
2  setosa Petal  Length   1.4
3  setosa Petal  Length   1.3
4  setosa Petal  Length   1.5
5  setosa Petal  Length   1.4
6  setosa Petal  Length   1.7

我试图在'D4'中添加行号,因为我发现有时spread()函数会导致某些错误,如

I've tried to add a row number to 'D4', since I found that sometimes, the spread() function will result into some error as discussed here. I don't know if there is a neat way to use spread() to achieve this goal.

推荐答案

我们需要先按组创建一个序列变量,然后再按spread

We need to create a sequence variable by group and then spread

library(tidyverse)
D %>% 
   gather(Measure, value, Length, Width) %>% 
   group_by(Species, Part, Measure) %>%
   mutate(i1 = row_number()) %>% 
   spread(Species, value) %>%
   select(-i1)
#    Part Measure setosa versicolor virginica
#*  <chr>   <chr>  <dbl>      <dbl>     <dbl>
#1  Petal  Length    1.4        4.7       6.0
#2  Petal  Length    1.4        4.5       5.1
#3  Petal  Length    1.3        4.9       5.9
#4  Petal  Length    1.5        4.0       5.6
#5  Petal  Length    1.4        4.6       5.8
#6  Petal  Length    1.7        4.5       6.6
#7  Petal  Length    1.4        4.7       4.5
#8  Petal  Length    1.5        3.3       6.3
#9  Petal  Length    1.4        4.6       5.8
#10 Petal  Length    1.5        3.9       6.1
# ... with 190 more rows

这篇关于tidyr中的spread()可以跨多个值分布吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆