Make rows with multiple observations in columns

I looked at similar questions, but I could not find a case similar to mine. I have a data frame that for each object has several state observations. It looks like this:

subject   <- c(rep("S1",4), rep("S2",4))
condition <- rep(c(rep("a",2), rep("b",2)),2)
value     <- c(1:8)
df        <- data.frame(subject,condition,value)

df
  subject condition value
  S1         a     1
  S1         a     2
  S1         b     3
  S1         b     4
  S2         a     5
  S2         a     6
  S2         b     7
  S2         b     8

I would like to change it like this:

  subject condition.a condition.b
  S1           1           3
  S1           2           4
  S2           5           7
  S2           6           8

I tried reshapeand cast, but they give me an error message, because there are several observations on the subject and condition. Does anyone have any suggestions on how they will do this?

Thank!

+1
source share
1 answer

Question is a bit ambiguous in the sense that it is not clear what values aand bshould be linked to each other.

, , (S1, a) (S2, b) .., count, (subject, condition), count_id to melt dcast :

library(plyr)
library(reshape2)

subject   <- c(rep("S1",4), rep("S2",4))
condition <- rep(c(rep("a",2), rep("b",2)),2)
value     <- c(1:8)
df        <- data.frame(subject,condition,value)

df.2 <- ddply(df, .(subject, condition), function(x) { x$count <- 1:nrow(x); x })
df.2
#   subject condition value count
# 1      S1         a     1     1
# 2      S1         a     2     2
# 3      S1         b     3     1
# 4      S1         b     4     2
# 5      S2         a     5     1
# 6      S2         a     6     2
# 7      S2         b     7     1
# 8      S2         b     8     2

df.3 <- melt(df.2, id.vars=c('subject', 'condition', 'count'))
dcast(df.3, subject + count ~ condition)
#   subject count a b
# 1      S1     1 1 3
# 2      S1     2 2 4
# 3      S2     1 5 7
# 4      S2     2 6 8

, ?

+1

All Articles