David
David

Reputation: 574

In R, how to reformat to a long format, creating multiple "descriptive" columns

I'm trying to reformat some incoming data in a data frame. It is read in in a wide format, in multiple columns, and I'd like to collapse it into a long format. I've used 'melt' to do this for other code I've written recently, but in this case, I want to break the measured vars into more than one column. For example, my data looks roughly like this:

Time   X-Mean   X-StdDev   Y-Mean   Y-StdDev
1       value    value      value    value
2       value    value      value    value
3       value    value      value    value
4       value    value      value    value
5       value    value      value    value

Ideally, I'd like this data reformatted as follows:

Time    Axis     Meas    Value
1       X        Mean    value
1       X        StdDev  value
1       Y        Mean    value
1       Y        StdDev  value
2       X        Mean    value
2       X        StdDev  value
2       Y        Mean    value
2       Y        StdDev  value
.....

From what I've read, it feels as though I should be using cast for this, but I can't work out what the syntax should be. I'm just discovering R at the moment, so any help would be appreciated! Thanks!

Upvotes: 0

Views: 154

Answers (2)

Chase
Chase

Reputation: 69201

Don't you just need to split the variable column from the results of melt()? For example:

library(reshape2)
x <- data.frame(id = 1:3, x_mean = rnorm(3), x_sd = rnorm(3,1), y_mean = rchisq(3, 1), y_sd = rchisq(3, df=2))

x.m <- melt(x, id.vars = "id")
x.m[, c("axis", "means")] <- colsplit(x.m$variable, "_", c("axis", "means"))

x.m[order(x.m$id), c("id", "axis", "means", "value")]
#----
   id axis means        value
1   1    x  mean  0.248144412
4   1    x    sd -0.078503777
7   1    y  mean  0.045275132
10  1    y    sd  0.297849771
....

If there's more to it, you should be able to cast() it into your appropriate dimensions.

Upvotes: 2

mnel
mnel

Reputation: 115425

This is classic fodder for base::reshape

assuming your data.frame is called dd.

# this will guess at split using `.` as the split
reshape(dd, direction = 'long',varying =2:5 )
         Time   time     X     Y id
1.Mean      1   Mean value value  1
2.Mean      2   Mean value value  2
3.Mean      3   Mean value value  3
4.Mean      4   Mean value value  4
5.Mean      5   Mean value value  5
1.StdDev    1 StdDev value value  1
2.StdDev    2 StdDev value value  2
3.StdDev    3 StdDev value value  3
4.StdDev    4 StdDev value value  4
5.StdDev    5 StdDev value value  5

Upvotes: 0

Related Questions