0

I have a list of 30 dataframes.

I want to create a vector that contains the standard deviation of the nth element of one of the columns, across all 30 data frames in the list of dataframes. I don't think I've explained that clearly. But the code for my for loop should make it clear.

FFT233_sd <- list()
for (i in 1:431999) {FFT233_sd[[i]] <- sd(c(FFT233_data[[1]][i,6], FFT233_data[[2]][i,6], FFT233_data[[3]][i,6], FFT233_data[[4]][i,6], FFT233_data[[5]][i,6], FFT233_data[[6]][i,6], FFT233_data[[7]][i,6], FFT233_data[[8]][i,6], FFT233_data[[9]][i,6], FFT233_data[[10]][i,6], FFT233_data[[11]][i,6], FFT233_data[[12]][i,6], FFT233_data[[13]][i,6], FFT233_data[[14]][i,6], FFT233_data[[15]][i,6], FFT233_data[[16]][i,6], FFT233_data[[17]][i,6], FFT233_data[[18]][i,6], FFT233_data[[19]][i,6], FFT233_data[[20]][i,6], FFT233_data[[21]][i,6], FFT233_data[[22]][i,6], FFT233_data[[23]][i,6], FFT233_data[[24]][i,6], FFT233_data[[25]][i,6], FFT233_data[[26]][i,6], FFT233_data[[27]][i,6], FFT233_data[[28]][i,6], FFT233_data[[29]][i,6], FFT233_data[[30]][i,6]))}

That for loop works, but is obviously quite slow. I have been told I should use lapply, but i do not understand how to do that. I have tried the following:

results2738 <- lapply( FFT2738_data , function(x) {sd(x) } )

but it has resulted in teh following errror:

Show Traceback

 Rerun with Debug
 Error in is.data.frame(x) : 
   (list) object cannot be coerced to type 'double' In addition: Warning message:
In mean.default(x) : argument is not numeric or logical: returning NA

If anyone can suggest a resource for me to investigate, i would be greatful.

Bobby M
  • 159
  • 1
  • 2
  • 9
  • 1
    Stack your 30 data.frames into one, following the "combining" part of Gregor's answer here https://stackoverflow.com/a/24376207/ and I guess it will be a simpler operation. – Frank Sep 08 '17 at 19:37

2 Answers2

3

Something like this:

FFT233_sd <- sapply(1:431999, function(i) {
  values <- sapply(1:length(FFT233_data), function(j) {
    FFT233_data[[j]][i,6]
  })
  sd(values)
}
thc
  • 9,527
  • 1
  • 24
  • 39
0

Try this

Reproducible example, a list of mtcars (10 repeats)

library(purrr)
L <- map(1:10, ~mtcars)

Solution

You'll need purrr:map

library(purrr)
sixthcol <- Reduce("cbind", map(L, ~.x[,6]))
ans <- apply(sixthcol, 1, sd)

Output

sixthcol <- Reduce("cbind", map(L, ~.x[,6]))

 [1,] 2.620 2.620 2.620 2.620 2.620 2.620 2.620 2.620 2.620 2.620
 [2,] 2.875 2.875 2.875 2.875 2.875 2.875 2.875 2.875 2.875 2.875
 [3,] 2.320 2.320 2.320 2.320 2.320 2.320 2.320 2.320 2.320 2.320
 [4,] 3.215 3.215 3.215 3.215 3.215 3.215 3.215 3.215 3.215 3.215
 [5,] 3.440 3.440 3.440 3.440 3.440 3.440 3.440 3.440 3.440 3.440
 [6,] 3.460 3.460 3.460 3.460 3.460 3.460 3.460 3.460 3.460 3.460
 [7,] 3.570 3.570 3.570 3.570 3.570 3.570 3.570 3.570 3.570 3.570
 [8,] 3.190 3.190 3.190 3.190 3.190 3.190 3.190 3.190 3.190 3.190
 # etc

ans <- apply(sixthcol, 1, sd)

# 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
CPak
  • 13,260
  • 3
  • 30
  • 48