我正在尝试创建一个r循环来在一系列数据集上运行命令。该命令来自make.design.data
库RMark
。它所采用的唯一参数是列表的名称。我有17个这样的列表,我想传递给make.design.data这是我一直在尝试使用的代码
DFNames<-c("DFAmerican.Goldfinch", "DFAmerican.Robin","DFBarn.Swallow","DFBobolink", "DFBrown.head.Cowbird", "DFCedar.Waxwing", "DFCommon.Grackle","DFCommon.Yellowthroat", "DFEuropean.Starling","DFHorned.Lark", "DFKilldeer","DFRed.wing.Blackbird", "DFSavannah.Sparrow", "DFSong.Sparrow","DFTree.Swallow", "DFVesper.Sparrow", "DFYellow.Warbler")
#in my environment each of the names given to DFNames represents a list
for (x in DFNames){
n<-make.design.data(x)
assign(paste0("ddl",x),n)
}
这给了我错误
Error in data$model : $ operator is invalid for atomic vectors
任何人都可以建议一种方法来修复我的代码,或采用不同的解决方法吗?
谢谢,Jude
答案 0 :(得分:3)
相反,您可以创建list
实际数据集而不是其名称的向量。
x <- list(DFAmerican.Goldfinch, ...)
然后你可以使用:
lapply(x, make.design.data)`.
或者在for循环中使用get
:
for (x in DFNames) {
make.design.data(get(x))
}
“R”方式是前者使用列表和apply
系列。然后你可以避免assign
的体操。