如何将变量传递到 split() 中?
我想在 for 循环中运行 split() ,但是当我传递变量文本时,它只是创建一个包含文本的新 data.frame 。这里的想法是基于CMPD_DF_1[5]
、CMPD_DF_2[5]
拆分CMPD_DF_1
、CMPD_DF_2
等> 等。如何传入 data.frame 而不是字符串?
for (i in 1:10) {
split(paste("CMPD_DF", i, sep = "_"),
paste(paste("CMPD_DF", i, sep = "_"), "[5]", sep=""))
}
I want to run split()
in a for loop, but when I pass it variable text, it just creates a new data.frame containing the text. The idea here is to split CMPD_DF_1
, CMPD_DF_2
, etc. based on CMPD_DF_1[5]
, CMPD_DF_2[5]
, etc. How do I pass in the data.frame and not a string?
for (i in 1:10) {
split(paste("CMPD_DF", i, sep = "_"),
paste(paste("CMPD_DF", i, sep = "_"), "[5]", sep=""))
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于最初的困惑感到抱歉。您可以将数据框放入列表中,然后使用
lapply
。这假设您要拆分的列在每个数据框中都是相同的。我将使用更通用的解决方案进行更新...这是一种使用
maply
执行此操作的方法,该方法允许在每个数据框中进行不同的拆分列。您只需在单独的列表中预先指定列并将它们传递给mapply
:Sorry for the initial confusion. You can put your data frames in a list and then use
lapply
. This assumes the column you are splitting on is the same in each data frame. I'll update with a more general solution...And here's a way to do this using
mapply
that will allow for different splitting columns in each data frame. You just pre-specify the columns in a separate list and pass them tomapply
:您的代码不起作用,因为您没有将 data.frame 传递给
split
。您正在传递一个字符向量,其中包含带有 data.frame 名称的字符串。像这样的东西应该可以工作,但它不太像 R。 @joran 的答案更可取。Your code doesn't work because you're not passing a data.frame to
split
. You're passing a character vector that contains a string with the name of your data.frame. Something like this should work, but it's not very R-like. @joran's answer is preferable.