Reputation: 13062
I have some survey data of the form:
id A_Type1 B_Type1 C_Type1 A_Type2 B_Type2 C_Type2
1 T11 T12 T11 T23 T23 T21
2 T12 T13 T11 T21 T22 T24
3 ...
Some answers are NULL. Answers for type1 are (T11, T12, T13, ..) and for type2 are (T21, T22, T23, ..). I want to show the relationship between Type1 and Type2 for each subject A, B, C. So, I'll need the data in the form
id subject Type1 Type2
1 A T11 T23
1 B T12 T23
1 C T11 T21
2 A T12 T21
2 B T13 T22
...
How would I do this in R?
Upvotes: 0
Views: 91
Reputation: 887163
You can use the devel version of data.table
i.e. v1.9.5
. Instructions to install the devel version are here
library(data.table)
melt(setDT(df1), id.var='id', measure.vars=list(2:4, 5:7))
Or
library(splitstackshape)
setnames(merged.stack(df1, var.stubs=c('_Type1', '_Type2'), sep='var.stubs',
atStart=FALSE), 2:4, c('subject', 'Type1', 'Type2'))[]
# id subject Type1 Type2
#1: 1 A T11 T23
#2: 1 B T12 T23
#3: 1 C T11 T21
#4: 2 A T12 T21
#5: 2 B T13 T22
#6: 2 C T11 T24
Upvotes: 3
Reputation: 54237
You can use reshape
:
df <- read.table(header=T, text="id A_Type1 B_Type1 C_Type1 A_Type2 B_Type2 C_Type2
1 T11 T12 T11 T23 T23 T21
2 T12 T13 T11 T21 T22 T24")
names(df) <- sub("([A-Z])_Type([0-9])", "Type\\2_\\1", names(df))
reshape(df, idvar = "id", varying = 2:7, direction = "long", sep = "_", timevar = "subject")
# id subject Type1 Type2
# 1.A 1 A T11 T23
# 2.A 2 A T12 T21
# 1.B 1 B T12 T23
# 2.B 2 B T13 T22
# 1.C 1 C T11 T21
# 2.C 2 C T11 T24
Upvotes: 1