I have the iris data set with below format,
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa 5.1 3.5 1.4 0.2
setosa 4.9 3.0 1.4 0.2
setosa 4.7 3.2 1.3 0.2
setosa 4.6 3.1 1.5 0.2
setosa 5.0 3.6 1.4 0.2
setosa 5.4 3.9 1.7 0.4
I wish to transform it to the below format,
Species Part Length Width
setosa Petal 1.4 0.2
setosa Petal 1.4 0.2
setosa Petal 1.3 0.2
setosa Petal 1.5 0.2
setosa Petal 1.4 0.2
setosa Petal 1.7 0.4
I am able to achieve it using tidyr package. Code below,
iris.wide <- iris %>%
gather(key, value, -Flower, -Species) %>%
separate(key, c("Part", "Measure"), "\\.") %>%
spread(Measure, value)
But I just feel its a lot of code/steps use gather, seperate and spread functions.
I am looking for suggestions to do it in a more better/near way, if any.