【发布时间】:2019-11-20 11:16:58
【问题描述】:
我有以下双循环:
indexnames = c(a, b, c, d, etc.)
# with
# length(indexnames) = 87
# class(indexnames) = "character"
# (indexnames = indexes I want to add in a column)
files = c(aname, bname, cname, dname, etc.)
# with
# length(files) = 87
# class(files) = "character"
# (files = name of files in the global environment)
现在我想遍历这两个列表并向文件[1] 添加名称为“index”的列和输入索引 [1]。我通过以下方式实现了这一点:
for(i in files){
for(j in indexnames){
files[i] = cbind(Index = indexnames[j], files[i])
}
}
当我运行它时,我收到一条包含 50 条或更多警告的错误消息。 我究竟做错了什么? 感谢任何帮助,谢谢。
【问题讨论】:
-
制作一个可重现的错误示例。
-
运行
warnings()时会发生什么?我可以看到您在files[i] = cbind(Index = indexnames[j], files[i])的左侧和右侧都有files[i]- 我认为这可能有问题。此外,当您以for(i in files)循环时,i不是索引,但files中的实际值 - 所以files[i]我认为没有意义。如果你想遍历files的索引,你可以将迭代更改为for(i in seq_along(files))。 -
尝试
list(aname, bname, cname, dname, etc.),然后在循环中files[[i]] <- etc。索引可能相同。 -
我得到以下错误: In filenames[i]