Let's say I have a set of CSV files in a folder called "my data" which is a folder in my working directory. The CSV files are matrices and looking something like:
> a
[,1] [,2] [,3] [,4]
[1,] 3 3 3 3
[2,] 3 3 3 3
[3,] 3 3 3 3
[4,] 3 3 3 3
> b
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 2 2 2 2
[3,] 1 1 1 1
[4,] 2 2 2 2
> c
[,1] [,2] [,3] [,4]
[1,] 3 3 3 3
[2,] 9 9 9 9
[3,] 3 3 3 3
[4,] 9 9 9 9
Let's say I have another folder in my working directory called "my results." In it I want to create another CSV file (call it "my_results") that has the results of the analyses performed on the CSV files in "my data." For example if I calculated the mean and standard deviation of a, b, and c "my_results" would look like:
> my_results
mean sd
a 3.0 0.00
b 1.5 0.52
c 6.0 3.10
The idea being every row of the new CSV would correspond to one of the CSVs in "my data" and the columns of the new CSV would correspond to a particular analysis run. Thank you.
lapply is your friend:
my_data = lapply(list.files("my data", "*.csv"), read.csv)
my_result = lapply(my_data, your_fun())
write.csv(dplyr::bind_rows(my_result), "my data/my results.csv")
you get the convienient bind_rows function with package dplyr
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.