Vote count: 0
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.
Can spread() in tidyr spread across multiple value?
Aucun commentaire:
Enregistrer un commentaire