R - idiomatic way of processing lists of data frames

I have 30 runs of data, each of which is stored in a separate CSV file, runi.csv, i = 0:29.

Say I want to put them all on a list. The best way I know how to do this is

runs = list()
for (i in 1:30) { runs[[i]] = read.csv(paste("run", i-1, ".csv")); }

Now, let’s further say that each of these data frames stored in the list has the same column layouts and that I am interested in the column identified by “x” and the column identified by “y”.

What is the easiest way to build all paired pairs (x, y) in 30 runs? Here is how I would do it (and I believe that there should be a better way):

xList = list()
yList = list()
for (i in 1:30) { xList[[i]] = runs[[i]]$x; yList[[i]] = runs[[i]]$y; }
matplot(x=as.data.frame(xList), y=as.data.frame(yList))

It gets even more painful when I try to convert the data; I cannot figure out how to apply a function to a specific column of each data frame stored in a list.

.

+3
2

, l * ply ( plyr) lapply , .

, , :

library(plyr)
runs <- llply(paste("run",1:30,".csv",sep=""), read.csv)

:

# some dummy data
runs <- list(a=data.frame(x=1:5, y=rnorm(5)), b=data.frame(x=1:5, y=rnorm(5)))
par(mfrow=c((length(runs)/2),2));
l_ply(1:length(runs), function(i) { plot(runs[[i]]$x, runs[[i]]$y) })

, (, pdf) par().

+3

, . , (runs[[i]] = data.frame(read.csv(paste("run", i-1, ".csv")), Run=i)), alldata <- do.call(rbind, runs).

lattice ggplot2 . , , , :

library(ggplot2)
qplot(x, y, colour=Run, data=alldata, geom="point")
+5

Source: https://habr.com/ru/post/1732597/


All Articles