简体   繁体   English

使用迭代器在for循环中分配给对象,作为对象名称的一部分

[英]Assign to object in for loop with iterator as part of the object name

I have a dataframe COG representing an experiment with a couple of different conditions. 我有一个数据框COG表示具有几个不同条件的实验。 For a couple of calculations, I'd like to subset this dataframe into as many subsets as there are conditionsm, and I'd like the subset name to be something like COG.i with i being the condition. 为了进行一些计算,我想将此数据帧子集化为与有条件的子集一样多的子集,并且我希望子集名称为类似于COG.i类的COG.i其中i为条件。

for (i in unique(COG$cond)) {
 ??? <- subset(COG, COG$cond == i)
}

What would I need to put in place of the ??? 我需要什么来代替??? ? It's not COG.i and definetely not paste0("COG.", i) 它不是COG.i ,也绝对不是paste0("COG.", i)

(I realize that one could also take care to "tidy" the data or use Dummy Codes for the different conditions, but apart from the statistics I'd like to get a better grasp of R in the first place.) (我意识到,还可以注意“整理”数据或针对不同的情况使用虚拟代码,但除了统计信息之外,我还想首先更好地理解R。)

Thank you for your help! 谢谢您的帮助!

Lists exist for this very purpose. 为此存在列表。 Instead of having multiple variables, put the results in a list. 而不是具有多个变量,而是将结果放在列表中。

As suggested in the comments, you should use split to split your dataset into different subsets. 如注释中所建议,您应该使用split将数据集拆分为不同的子集。

COG.split <- split(COG, COG$cond)

You can then access the different subsets using the [[ ]] operator, or use the *apply family of functions to perform operations on the various subsets. 然后,您可以使用[[ ]]运算符访问不同的子集,或使用*apply函数系列对各个子集执行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM