[R] sum multiple csv files

Jeremie Juste jeremiejuste at gmail.com
Mon Jan 15 13:17:00 CET 2018


Alejandra Lopez-Galan <alejandra7galan at gmail.com> writes:
Hello,

I'm not sure to fully answer you question but I'll give it a try. I'll
use the library "data.table" as I forgot how to do it in base R. If you
don't have it you will have to install it by doing

> install.packages(data.table,repos ="http://cran.us.r-project.org")

let's say you have this list of data.frame

> lapply(1:2,function(x) data.frame(B=letters[1:10],A=rnorm(10)))

## [[1]]
##     B          A
##  1 a  1.8276026
##  2 b  0.2870562
##  3 c -0.6304431
##  4 d  0.3066375
##  5 e  0.3274438
##  6 f  0.3370640
##  7 g -1.6660051
##  8 h -0.3736336
##  9 i  0.1494459
## 10 j -1.0036616

## [[2]]
##     B          A
##  1 a -1.1488812
##  2 b -0.4423796
##  3 c -0.2690834
##  4 d -1.1390742
##  5 e  0.7142574
##  6 f  0.3316523
##  7 g  0.3187546
##  8 h  0.1099996
##  9 i  0.3972000
## 10 j  0.4749161

and want to sum the A column for each data.frame then you can use the
following:

# Assign the list to the variable aa
aa <- lapply(1:2,function(x) data.table(B=letters[1:10],A=rnorm(10)))

then:
> >lapply(aa,function(x) data.table(x)[,sum(A)])

## [[1]]
## [1] 6.533179

## [[2]]
## [1] 2.075677


Notice that I converted the each data.frame to a data.table and then sum
the column A.

If you want to bind the resutls together you can do
do.call(rbind,lapply(aa,function(x) data.table(x)[,sum(A)]))

          [,1]
[1,] -9.377476
[2,] -3.853971

HTH,

Best Regards,

Jeremie







> Hi, I am pretty new to R and I would apreciatte very much your help to
> solve my problem. I have 40 csv files that have the same structure, and I
> want to merge them into a single data frame.
>
> I already have load and combined all the cvs files into a large list, and I
> created two
>
> filenames <- list.files('data',full.names=TRUE)
>
> All_data <- lapply(filenames,function(i){
>   ###read cvs files and add the row and column names to each data frame###
>   read.csv(i, header=FALSE, sep = "", col.names = col_names, row.names =
> row_names)
>  })
>
> However I would like to sum the rows of cvs files to get a single data
> frame (each cvs file has 47 rows and colunms, so the final data frame
> should have the same). I could only do it one by one data data frame, but I
> was wondering if anyone could give an idea of how to write a function for
> this.
>
> Thanks,
> Alejandra
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list