0

我已经浏览了其他人发布的关于将数据框从宽变长的示例。

我的原始数据框具有以下列

Country,Trial_ID,Trial_name,seed.zone, Prov_name,Alt_min,Alt_max,Prov_ID,Replication, Tree_id   Plot_id DBH_05, DBH_06, DBH_09, DBH_10, DBH_11, DBH_13, DBH_14, DBH_15, DBH_17, DBH_18, DBH_20, DBH_21, DBH_23, DBH_24, DBH_25, DBH_27, DBH_29, DBH_30, DBH_31, DBH_34, DBH_35, DBH_37

我想将我的数据框重塑为以下列

Country,Trial_ID,Trial_name,seed.zone, Prov_name,Alt_min,Alt_max,Prov_ID , DBH,  Age


library(reshape2)

mydata <- reshape(Database_Final, 
             idvar=c("Trial_name","Country", "Trial_ID",
                     "Trial_name","seed zone","Prov_name",
                    " Alt_min", "Alt_max","Prov_ID","Replication","Tree_id","Plot_id"),      

            varying = list( "DBH05","DBH06","DBH09","DBH10","DBH11","DBH13","DBH14",
                            "DBH15","DBH17","DBH18","DBH20","DBH21","DBH23",
                            "DBH24","DBH25","DBH27","DBH_29","DBH30","DBH31","DBH34","DBH35","DBH_37"),

             v.names = "DBH",
             timevar = "Age", 
             times = c( "DBH05","DBH06","DBH09","DBH10","DBH11","DBH13","DBH14",
                        "DBH15","DBH17","DBH18","DBH20","DBH21","DBH23",
                        "DBH24","DBH25","DBH27","DBH_29","DBH30","DBH31","DBH34","DBH35","DBH_37"),
             direction = "long")

但我不断收到错误:Error in reshapeLong(data, idvar = idvar, timevar = timevar, varying = varying, : 'times' is wrong length

4

1 回答 1

1

正如评论中所指出的, reshape 函数不是 reshape2 包的一部分。您的列已命名:DBH_05, DBH_06, DBH_09, DBH_10,...但您的大多数times(and varying) 参数都没有下划线。你可能只想要

...
varying= list( names(dfm)[12:47] ),
times="DBH", 
...
于 2013-08-12T23:11:14.303 回答