问题
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)
which looks like
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
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
What I did is the following:
D4 = D %>% gather(Measure,value,3:4)
which gives
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
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.
回答1:
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
来源:https://stackoverflow.com/questions/42103882/can-spread-in-tidyr-spread-across-multiple-value