I'm trying to perform a loop with for in R. 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:
x = c(1,3,4)
my data frame prova1 is:
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
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.
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. Is there any way to stop R to overwrite the list? And how to perform the same loop on several column?
Thank you in advance, Luca
Here is one option with dplyr
and 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
for(i in 3) # i.e. one time
Instead, it should be
for(i in 1:3)
ie
for(i in seq_len(n))
It is also better to initialize the output list
with the predefined length
library(plyr)
usq <- vector('list', length(x))
for(i in seq_len(n)) {
usq[[i]] <- ddply(prova1, .(anno), summarize,
sum = sum(value >= x[i] ))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.