I am trying to read several .RLS (spreadsheet) files into one matrix using R. These files have very formulaic names, which makes it easy to work with. My plan (see code below) is to create the file names in a vector called names, then use a for loop to read and add the second column of each of those files into a matrix. However, I have encountered some issue. I have tested the names part of my code, and it can read tables into R individually. However when I try to put them all together into the matrix collected using the 2nd for loop, I get an error that says, "incorrect number of subscripts on matrix". I am not sure what this means. Any advice would be welcome.
library(tidyverse)
collector <- function(min, max){
collected <- matrix(nrow = 601, ncol = max - min 2)
names = c()
for (i in 1:(max-min 1)){
names[i] = paste0("D:/CHY 498/UV-Vis/22822/BH4_3/12321A",(i min-1),".RLS")
}
for (j in 1:(max-min 1)){
e <- read_table(names[j], col_names=FALSE)
collected[,j 1] = e[,2]
}
}
test <- collector(15, 23)
test
CodePudding user response:
Regarding the issue, it may be because we used read_table
which returns a tibble
and tibble doesn't drop dimensions with [
. Instead, we need [[
.
collector <- function(min, max){
collected <- matrix(nrow = 601, ncol = max - min 2)
names = c()
for (i in 1:(max-min 1)){
names[i] = paste0("D:/CHY 498/UV-Vis/22822/BH4_3/12321A",(i min-1),".RLS")
}
for (j in 1:(max-min 1)){
e <- read_table(names[j], col_names=FALSE)
collected[,j 1] = e[[2]]## change
}
}
Instead of initializing with a NULL vector, we can create a vector of certain length and then assign with [i]
. Other than that the code works with a dummy data
collector <- function(min, max){
i1 <- max - min 1
collected <- matrix(nrow = 601, ncol = max - min 2)
names = character(i1)
for (i in 1:i1){
names[i] = paste0("D:/CHY 498/UV-Vis/22822/BH4_3/12321A",(i min-1),".RLS")
}
for (j in 1:i1){
e <- cbind(NA, 1:601) # created dummy data
collected[,j 1] = e[,2]
}
collected
}
test <- collector(15, 23)
-testing
test <- collector(15, 23)
> head(test)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] NA 1 1 1 1 1 1 1 1 1
[2,] NA 2 2 2 2 2 2 2 2 2
[3,] NA 3 3 3 3 3 3 3 3 3
[4,] NA 4 4 4 4 4 4 4 4 4
[5,] NA 5 5 5 5 5 5 5 5 5
[6,] NA 6 6 6 6 6 6 6 6 6
NOTE: The last part of reading the data couldn't be tested. It may be that some of the links doesn't have data and thus couldn't be read. Also, paste
is vectorized, so the first loop is not really needed