5

How to do this more elegantly with plyr, reshape2, aggregate function and/or data.table?

library(plyr)

set.seed(1) 
x <- data.frame(Ind = paste0("Ind", 1:10), Treatment = c(rep("Treat",10),rep("Cont",10)),
value = rnorm(20,60,8))

tr <- subset(x, Treatment == "Treat")
tr <- rename(tr, c("value" = "Treat"))

ct <- subset(x, Treatment == "Cont")
ct <- rename(ct, c("value" = "Cont"))

merge(ct[-2], tr[-2], by = "Ind", all = T, sort = F)

# Do not run, data.frame:
     Ind     Cont    Treat
1   Ind1 72.09425 54.98837
2   Ind2 63.11875 61.46915
3   Ind3 55.03008 53.31497
4   Ind4 42.28240 72.76225
5   Ind5 68.99945 62.63606
6   Ind6 59.64053 53.43625
7   Ind7 59.87048 63.89943
8   Ind8 67.55069 65.90660
9   Ind9 66.56977 64.60625
10 Ind10 64.75121 57.55689
3
  • Is this possible to do with plyr too? That and data.table are the packages I have used least, but seem very useful allround tools for these kinds of problems.
    – Mikko
    Apr 16, 2013 at 10:29
  • I've updated my answer with a plyr solution, though for this particular problem, I'm not sure that plyr is the best "tool". Apr 16, 2013 at 12:03
  • Thank you everybody for a puzzling variety of solutions. This is exactly what I wanted =) Now I can look back to this post and find a method, which is best adapted to a specific data frame reshaping challenge. I ticked @AnandaMahto 's answer as "accepted" because it had most options. However, for this particular case @DidzisElferts 's solution using reshape2 package is perhaps my favorite. Also @Arun 's solution with data.table deserves a closer look.
    – Mikko
    Apr 16, 2013 at 13:42

3 Answers 3

11

To add to your options...

Here's our starting data:

set.seed(1) # Nice for reproducible examples
x <- data.frame(Ind = paste0("Ind", 1:10), 
                Treatment = c(rep("Treat",10),rep("Cont",10)),
                value = rnorm(20,60,8))

xtabs

Note that the output is a matrix, not a data.frame.

xtabs(value ~ Ind + Treatment, x)
#        Treatment
# Ind         Cont    Treat
#   Ind1  72.09425 54.98837
#   Ind10 64.75121 57.55689
#   Ind2  63.11875 61.46915
#   Ind3  55.03008 53.31497
#   Ind4  42.28240 72.76225
#   Ind5  68.99945 62.63606
#   Ind6  59.64053 53.43625
#   Ind7  59.87048 63.89943
#   Ind8  67.55069 65.90660
#   Ind9  66.56977 64.60625

reshape

reshape(x, direction = "wide", idvar="Ind", timevar="Treatment")
#      Ind value.Treat value.Cont
# 1   Ind1    54.98837   72.09425
# 2   Ind2    61.46915   63.11875
# 3   Ind3    53.31497   55.03008
# 4   Ind4    72.76225   42.28240
# 5   Ind5    62.63606   68.99945
# 6   Ind6    53.43625   59.64053
# 7   Ind7    63.89943   59.87048
# 8   Ind8    65.90660   67.55069
# 9   Ind9    64.60625   66.56977
# 10 Ind10    57.55689   64.75121

If you wanted to change the names at the same time with the reshape option:

setNames(reshape(x, direction = "wide", idvar="Ind", timevar="Treatment"), 
         c("Ind", "Treat", "Cont"))

split + merge

Again, setNames could be used here, or you can rename the columns afterwards.

temp <- split(x[-2], x$Treatment)
merge(temp[[1]], temp[[2]], by = "Ind", suffixes = names(temp))
#      Ind valueCont valueTreat
# 1   Ind1  72.09425   54.98837
# 2  Ind10  64.75121   57.55689
# 3   Ind2  63.11875   61.46915
# 4   Ind3  55.03008   53.31497
# 5   Ind4  42.28240   72.76225
# 6   Ind5  68.99945   62.63606
# 7   Ind6  59.64053   53.43625
# 8   Ind7  59.87048   63.89943
# 9   Ind8  67.55069   65.90660
# 10  Ind9  66.56977   64.60625

ddply from plry

(I'm not a regular "plyr" user, so not at all sure if this is the best approach).

library(plyr)
ddply(x, .(Ind), summarize, 
      Treat = value[Treatment == "Treat"], 
      Cont = value[Treatment == "Cont"])
#      Ind    Treat     Cont
# 1   Ind1 54.98837 72.09425
# 2  Ind10 57.55689 64.75121
# 3   Ind2 61.46915 63.11875
# 4   Ind3 53.31497 55.03008
# 5   Ind4 72.76225 42.28240
# 6   Ind5 62.63606 68.99945
# 7   Ind6 53.43625 59.64053
# 8   Ind7 63.89943 59.87048
# 9   Ind8 65.90660 67.55069
# 10  Ind9 64.60625 66.56977

unstack (as if the options weren't enough!)

unique(data.frame(x[1], unstack(x, value ~ Treatment)))
#      Ind     Cont    Treat
# 1   Ind1 72.09425 54.98837
# 2   Ind2 63.11875 61.46915
# 3   Ind3 55.03008 53.31497
# 4   Ind4 42.28240 72.76225
# 5   Ind5 68.99945 62.63606
# 6   Ind6 59.64053 53.43625
# 7   Ind7 59.87048 63.89943
# 8   Ind8 67.55069 65.90660
# 9   Ind9 66.56977 64.60625
# 10 Ind10 64.75121 57.55689
2
  • I don't think you need as.vector in the ddply solution, but I agree it's not the best tool for this job.
    – hadley
    Apr 16, 2013 at 12:27
  • @hadley, You're right (of course). Thanks! Edited accordingly. Apr 16, 2013 at 12:36
7

Here's a data.table way:

x.dt <- as.data.table(x)
setkey(x.dt, "Ind")
x.dt[, setattr(as.list(value), 'names', c("Treat", "Cont")),by=Ind]
#       Ind    Treat     Cont
#  1:  Ind1 57.73997 54.06263
#  2: Ind10 64.23664 65.98024
#  3:  Ind2 58.71422 58.01650
#  4:  Ind3 52.71239 62.64899
#  5:  Ind4 65.09401 75.51550
#  6:  Ind5 47.04052 61.80900
#  7:  Ind6 61.95129 55.20021
#  8:  Ind7 58.02494 55.41143
#  9:  Ind8 69.38424 57.71132
# 10:  Ind9 62.02491 57.06147
6

You can use function dcast() from library reshape2.

 dcast(data=x,Ind~Treatment)
     Ind     Cont    Treat
1   Ind1 53.45988 53.68913
2  Ind10 54.02344 66.32866
3   Ind2 57.44591 62.32354
4   Ind3 67.53185 53.14807
5   Ind4 52.42713 55.04052
6   Ind5 63.80633 61.58893
7   Ind6 59.40308 51.66228
8   Ind7 67.79597 73.60620
9   Ind8 58.15420 65.06976
10  Ind9 61.45161 63.73947
9
  • That was simple...I see that I over simplified my problem and hence a rather basic question. Also, it should be noted that dcast seems to pick the value only if the column is named as "value". Otherwise, you have to specify the column: dcast(data=x,Ind~Treatment, value.var = "name of the column").
    – Mikko
    Apr 16, 2013 at 9:54
  • 2
    @Largh, why don't you go ahead and update your question with what you're actually trying to solve? Apr 16, 2013 at 9:58
  • @AnandaMahto, because I thought that it would be unfair for the people who have provided excellent solutions for the question as it was originally. I could of course add an edit to expand the question.
    – Mikko
    Apr 16, 2013 at 10:00
  • @Largh, your call. To me, the question is still "young" in SO terms, so no harm in updating. Apr 16, 2013 at 10:01
  • @Largh This works also if column name for value is different (just tried with other names). If there are more than one value column then you have to add value.var="column name" Apr 16, 2013 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.