简体   繁体   中英

How to manipulate hierarchical data in R?

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?

You can use the devel version of data.table ie 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM