0

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.

Harish
  • 255
  • 7
  • 18
  • 1
    oops it got removed in copy paste – Harish Feb 14 '18 at 10:13
  • Are you taking the mean or sum of those values ? – akrun Feb 14 '18 at 10:15
  • 2
    It is more straightfoward with `data.table` i.e. `melt(as.data.table(iris), measure = patterns("Length", "Width"), value.name = c("Length", "Width"))` – akrun Feb 14 '18 at 10:16
  • the flower column got removed in copy paste. Please imagine flower column in output data set, which is a number to indicate the flower. – Harish Feb 14 '18 at 10:16
  • an adaptation @akrun's comment to get the exact desired outcome: `melt(as.data.table(iris), id = 'Species', measure.vars = patterns('Length','Width'), variable.name = 'Part', value.name = c('Length', 'Width'))[, Part := c('Sepal','Petal')[Part]][]` – Jaap Feb 14 '18 at 10:36

0 Answers0