-2

I am new to R and I'm stuck. I made a list of data frames and then used lapply to create a list of the number of rows in each data frame. How do I turn turn this number of rows list into a data frame of two columns, where the first column is the element index and second column the value of the element. e.g. a list of 1)124. 2)300. 3)550 turns into data frame of columns Id and Rows, with values 1, 2, 3 and 124, 300, 350 respectively. Below is the code I have so far. I have searched for answers here but its difficult to find specific ones being a newcomer.

fileList <- list.files("directory1", full.names = T)
moniFrames <- list()
for (i in seq_along(fileList)) {
     moniFrames[[i]] <- read.csv(file = fileList[i])
}
moniNaOff <- lapply(moniFrames, na.omit)
numRowsList <- lapply(moniNaOff, nrow)

numRowsList sample result:

[[1]]
[1] 117

[[2]]
[1] 1041

[[3]]
[1] 243

My other problem is how to how to get a correlation vector after applying the cor function on a list of data frames.

numRowsList <- lapply(moniNaOff, nrow) # creates list of the number of rows per monitor

thresholdPass <- list() # creates empty list for elements that will pass threshold
for (i in 1:332) {
      if (numRowsList[i] > 900) {
              thresholdPass[[i]] <- moniNaOff[i]     
      } else {
              thresholdPass[[i]] <- NULL
      } # selects elements that pass the threshold and turns others null
}

thresholdPass[sapply(thresholdPass, is.null)] <- NULL # removes null elements
thresholdPassFrames <- lapply(thresholdPass, as.data.frame)

corrColms <- lapply(thresholdPassFrames, function(x) x[!(names(x) %in% c("Date", "ID"))])
correlations <- lapply(corrColms,cor)

correlations above gives a list of matrices like this:

[[1]]
            sulfate     nitrate
sulfate  1.00000000 -0.01895754
nitrate -0.01895754  1.00000000

[[2]]
           sulfate    nitrate
sulfate  1.0000000 -0.1578286
nitrate -0.1578286  1.0000000

[[3]]
           sulfate    nitrate
sulfate 1.00000000 0.05774168
nitrate 0.05774168 1.00000000

But I would like a list of single numeric elements that represent the correlations as shown below, and then turn it into a single vector listing all correlations. I greatly appreciate your help

[[1]]
-0.01895754

[[2]]
-0.1578286

[[3]]
0.05774168
Jaap
  • 81,064
  • 34
  • 182
  • 193
uwayor
  • 1
  • 4
  • I answered your first question. If you have multiple questions you should consider splitting them up into multiple questions, not asking one large multi-part question. Finally, please follow the reproducible example **[guidelines](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)**. Notice how I created a reproducible list of three data frames to answer the question with. – BrodieG Oct 24 '14 at 21:59

1 Answers1

1

Make a list of data frames

set.seed(1)
n <- 3
df.list <- replicate(n,
  data.frame(a=sample(1:100, sample(1:1000, 1), rep=T)), simplify=F
)

Get the number or rows

data.frame(
  id=1:n,                          # we know the index is just 1:n
  nrows=sapply(df.list, nrow)      # use `sapply` to apply `nrow` to each df in list
)
#   id nrows
# 1  1   266
# 2  2   755
# 3  3   844

And to confirm this works:

str(df.list)

# List of 3
# $ :'data.frame':  266 obs. of  1 variable:
#   ..$ a: int [1:266] 38 58 91 21 90 95 67 63 7 21 ...
# $ :'data.frame':  755 obs. of  1 variable:
#   ..$ a: int [1:755] 63 17 7 11 39 17 30 20 26 19 ...
# $ :'data.frame':  844 obs. of  1 variable:
#   ..$ a: int [1:844] 17 77 52 28 35 71 60 47 34 97 ...
BrodieG
  • 51,669
  • 9
  • 93
  • 146