重塑R中的错误

时间:2013-08-12 22:26:03

标签: r reshape

我已经浏览了其他人发布的关于从宽到长重塑数据框架的示例。

我的原始数据框包含以下列

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

1 个答案:

答案 0 :(得分:1)

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

...
varying= list( names(dfm)[12:47] ),
times="DBH", 
...