[R] how to combine data of several csv-files

jim holtman jholtman at gmail.com
Tue Jul 31 12:18:52 CEST 2007


Here is the modified script for computing the 'sd':

v1 <- NA
v2 <- rnorm(6)
v3 <- rnorm(6)
v4 <- rnorm(6)
v5 <- rnorm(6)
v6 <- rnorm(6)
v7 <- rnorm(6)
v8 <- rnorm(6)
v8 <- NA

list <- list(v1,v2,v3,v4,v5,v6,v7,v8)
categ <- c(NA,"cat1","cat1","cat1","cat2","cat2","cat2",NA)

# create partitioned list
list.cat <- split(list, categ)
# combine each partition into a matrix
list.mat <- lapply(list.cat, function(x) do.call('rbind', x))
# now take the means of each column
lapply(list.mat, colMeans)
# compute the 'sd' by using 'apply' on the columns
lapply(list.mat, apply, 2, sd)



On 7/31/07, 8rino-Luca Pantani <ottorino-luca.pantani at unifi.it> wrote:
> > Hi Jim,
> > that's exactly what I'm looking for. Thank you so much. I think, I
> > should look for some further documentation on list handling.
> I think I will do the same...................
> Thanks to Jim I learned "textConnection" and "rowMeans".
>
> Jim, could you please go a step further and tell me how to use lapply to
> calculate
> the sd instead of the mean of the same items?
> I mean
> sd(-0.6442149 0.02354036 -1.40362589)
> sd(-1.1829260 1.17099178 -0.046778203)
> sd(-0.2047012 -1.36186952 0.13045724)
> etc
>
> x <- read.table(textConnection("  v1 v2 v3  v4 v5  v6 v7 v8
> NA -0.6442149  0.02354036 -1.40362589 -1.1829260  1.17099178 -0.046778203 NA
> NA -0.2047012 -1.36186952  0.13045724  2.1411553  0.49248118 -0.233788840 NA
> NA -1.1986041 -0.42197792 -0.84651458 -0.1327081 -0.18690065  0.443908897 NA
> NA -0.2097442  1.50445971  1.57005071 -0.1053442  1.50050976 -1.649740180 NA
> NA -0.7343465 -1.76763996  0.06961015 -0.8179396 -0.65552410  0.003991354 NA
> NA -1.3888750  0.53722404  0.25269771 -1.2342698 -0.01243247 -0.228020092 NA"), header=TRUE)
>
>
>
>
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list