Solidifying a melted data frame?

后端 未结 3 1576
感动是毒
感动是毒 2021-01-12 10:19

Assuming I have a melted data.frame that looks like this:

  variable       value
1         A -0.19933093
2         A -1.19043346
3         A -1.         


        
3条回答
  •  自闭症患者
    2021-01-12 10:34

    Alright, begin with a data frame in wide form, containing an id. melt() it to give the long form, then dcast() it to get back to the original data frame.

    library(reshape2)
    df = read.table(text = "id   A   B
    1  1 -0.19933093 -0.10074686
    2  2 -1.19043346  0.72451483
    3  3 -1.32248172 -0.40914044
    4  4 -1.98644507  0.02913376
    5  5 -0.07930953  0.16062491", sep = "", header = TRUE)
    
    df
    
    df.melt = melt(df, "id")
    df.melt
    
    df.original = dcast(df.melt, id~variable)
    
    df.original
    

提交回复
热议问题