Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Receiving error: argument of length 0 when reading in separate tables from multiple csv files in for loop

$
0
0

Error in 1:nrow(csvs) : argument of length 0 when reading in separate tables from multiple csv files

I am trying to perform the following for loop on separate tables read in from multiple csv files in the working directory. But I keep getting the error 'Error in 1:nrow(csvs) : argument of length 0'. This is what I have tried below...

# Read all csvs from files path.
csv_files <- list.files(pattern="*.csv")
csvs <- lapply(csv_files, read.table)


count <- 0

for(x in 1:nrow(csvs) - 1) {
  for(y in 1:ncol(csvs)) {
    if((isTRUE(csvs[x,y] == 1)) && (isTRUE(csvs[x+1,y+1] == 0))) {
      count <- count + 1
    }
  }
}
count

This outputs nothing and shows the error: argument of length 0. Any suggestions?

I have also included a reproducible example using only one reproducible matrix below:

set.seed(99)
mat <- matrix(sample(c(0,1), 2500, prob=c(0.8,0.2), replace=TRUE), nrow=50) 

count <- 0

for(x in 1:nrow(mat) - 1) {
  for(y in 1:ncol(mat)) {
    if((isTRUE(mat[x,y] == 1)) {
      count <- count + 1
    }
  }
}
count

[1] 91

I can't figure out why this works for one matrix and not multiple. Can anyone help please?


Viewing all articles
Browse latest Browse all 201839

Trending Articles