0

Consider this toy data frame:

df <- read.table(text = "target birds    wolfs     
                         0       21         7  
                         0        8         4  
                         1        2         5 
                         1        2         4 
                         0        8         3 
                         1        1         12  
                         1        7         10 
                         1        1         9 ",header = TRUE)

I would like to run a loop function that will calculate the mean per each variable's 2:5 rows and save all results as a CSV.

I wrote this line of code:

for(i in names(df))  {print(mean(df[2:5,i]))}

and got the following results:

[1] 0.5
[1] 5
[1] 4

But when I tried to export it to csv using the code below I got in the file only the last result: [1] 4. code:

for(i in names(df))  { j<-(mean(df[2:5,i]))
                       write.csv(j,"j.csv")    }

How can I get in the same csv file a list of all the results?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
mql4beginner
  • 2,193
  • 5
  • 34
  • 73
  • the first parameter to `write.csv` is _"the object to be written, preferably a matrix or data frame. If not, it is attempted to coerce x to a data frame"_. You can use `rbind(colMeans(df[2:5,]))` to make a 1 row matrix and write that out. – hrbrmstr Oct 18 '15 at 11:59
  • Why are you trying to this and why cant you just do `colMeans(df[2:5,])`? – David Arenburg Oct 18 '15 at 11:59
  • You are writing over `j.csv` on every iteration of the loop. j only holds the last value. Initialize it before the loop `j <- list()`, assign means to `j[[i]]`, and write the csv after the whole loop is done. That said, this is not the proper way to compute column means. Take a look at `?colMeans` or at `?dplyr::summarize_each` – scoa Oct 18 '15 at 12:01
  • [This](http://stackoverflow.com/questions/30835815/save-imported-csv-data-in-vector-r/30835924#30835924) answer of mine can be very useful; in fact it is almost a duplicate, even though it need to be adapted to export files and not save them in R environment. – SabDeM Oct 18 '15 at 12:39
  • Hello @David Arenburg, I used the mean function just to give the structure for the loop.My real world function is different. – mql4beginner Oct 18 '15 at 12:43
  • Hello @scoa.Is there a code sample that I can use for this matter?I'm new to looping.How can practicaly make j hold all the results? – mql4beginner Oct 18 '15 at 12:48
  • Maybe that will help: [link](http://stackoverflow.com/questions/32937972/store-results-of-for-loop-in-unique-objects/32938126#32938126) – Alex Oct 18 '15 at 13:15
  • Thanks @Alex, It works :i<- NULL for (i in names(df)){ j[i] <- (mean(df[1:5,i])) write.csv(j,"j.csv") } – mql4beginner Oct 18 '15 at 13:22

1 Answers1

1

In dplyr, you could use summarise_each to perform a computation on every columns of your data frame.

library(dplyr)
j <- slice(df,2:5) %>% # selects rows 2 to 5
  summarise_each(funs=funs(mean(.))) # computes mean of each column

The results are in a data.frame:

j
  target birds wolfs
1    0.5     5     4

If you want each variable mean on a separate line, use t()

t(j)
       [,1]
target  0.5
birds   5.0
wolfs   4.0

And to export the results:

write.csv(t(j),"j.csv")
scoa
  • 19,359
  • 5
  • 65
  • 80