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