[英]For Loop on ddply
I'm trying to perform a loop with for in R.我正在尝试在 R 中使用 for 执行循环。 What I'm trying to do I think is quite simple.我想做的事情我认为很简单。 I have a vector (but I'm trying also with a column in a data frame) and I have a function in dpply in which R has to substitute the values from the vector:我有一个向量(但我也在尝试使用数据框中的一列)并且我在 dpply 中有一个 function ,其中 R 必须替换向量中的值:
x = c(1,3,4)
my data frame prova1 is:我的数据框 prova1 是:
anno variable value
1 1922 gen 0.5
2 1922 gen 0.0
3 1922 gen 1.5
4 1922 gen 0.0
5 1922 gen 4.0
6 1922 gen 2.5
7 1922 gen 5.0
8 1922 gen 0.0
9 1922 gen 0.0
10 1922 gen 0.0
11 1922 gen 0.0
12 1922 gen 0.0
13 1922 gen 0.0
14 1922 gen 0.0
15 1922 gen 0.0
16 1922 gen 2.5
17 1922 gen 0.0
18 1922 gen 0.0
also, I want r to store every result of the function in a list, thus I had created an empty list object另外,我希望 r 将 function 的每个结果存储在一个列表中,因此我创建了一个空列表 object
usq<-list()
then I use the loop然后我使用循环
n = length(x)
for(i in n) {
usq[[i]] <-ddply(prova1, .(anno), summarize,
sum = sum(value >= x[i] ))
}
the problem is that R overwrite each time the whole list, thus, in the end I obtain a list with the first value NULL
and only the last object of the list is correct.问题是 R 每次都会覆盖整个列表,因此,最后我获得了第一个值NULL
的列表,只有最后一个 object 是正确的。
Moreover, I would like to do the same loop on a column or a row like:此外,我想在列或行上执行相同的循环,例如:
gen feb mar apr mag giu lug ago set ott nov dic
3.93 5.2 3.2 4 5 6 7 8 9 10 11 12
in order to get an object in the list for each column.为了在每列的列表中获得 object。 Is there any way to stop R to overwrite the list?有没有办法阻止 R 覆盖列表? And how to perform the same loop on several column?以及如何在多列上执行相同的循环?
Thank you in advance, Luca提前谢谢你,卢卡
Here is one option with dplyr
and purrr
这是dplyr
和purrr
的一个选项
library(dplyr)
library(purrr)
map(x, ~ prova1 %>%
group_by(anno) %>%
summarise(Sum = sum(value >= .x)))
In the OP's loop, 'n' is a single value ie length
of 'x' = 3. We are looping over在 OP 的循环中,“n”是单个值,即“x”的length
= 3。我们正在循环
for(i in 3) # i.e. one time
Instead, it should be相反,它应该是
for(i in 1:3)
ie IE
for(i in seq_len(n))
It is also better to initialize the output list
with the predefined length最好用预定义的长度初始化 output list
library(plyr)
usq <- vector('list', length(x))
for(i in seq_len(n)) {
usq[[i]] <- ddply(prova1, .(anno), summarize,
sum = sum(value >= x[i] ))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.