如何在R中循环浏览列表 [英] How to loop through a list in R
本文介绍了如何在R中循环浏览列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个清单可以说:
dataSet$mixed_bag <- list("Hello", c("USA", "Red", "100"), c("India", "Blue", "76"))
我想遍历此列表并根据列表的内容创建新的变量.伪代码看起来像这样.
I want to iterate through this list and create new variables based on the contents of the list. A pseudo code would look something like this.
foreach row in dataSet$mixed_bag {
if (sapply(dataSet$mixed_bag, length) == 3) {
dataSet$country <- dataSet$mixed_bag[[row]][1];
dataSet$color <- dataSet$mixed_bag[[row]][2];
dataSet$score <- dataSet$mixed_bag[[row]][3];
} else if (sapply(dataSet$mixed_bag, length) == 2) {
#Do something else
} else {
# Do nothing
}
}
请提出我将如何在R中做到这一点
Please suggest how would I do this in R
推荐答案
类似的东西?
dataList=list("Hello", c("USA", "Red", "100"), c("India", "Blue", "76"))
for(i in dataList){print(i)}
返回:
[1] "Hello"
[1] "USA" "Red" "100"
[1] "India" "Blue" "76"
或:
for(i in dataList){for(j in i){print(j)}}
返回:
[1] "Hello"
[1] "USA"
[1] "Red"
[1] "100"
[1] "India"
[1] "Blue"
[1] "76"
这篇关于如何在R中循环浏览列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文