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)


counter <- 0

for(i in 1:nrow(csvs) - 1) {
  for(j in 1:ncol(csvs)) {
    if((isTRUE(csvs[i, j] == 1)) && (isTRUE(csvs[i + 1, j] == 1))) {
      counter <- counter + 1
    }
  }
}
counter

Is there a way to do this without getting the error argument of length 0? Please help.

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) 

counter <- 0

for(i in 1:nrow(mat) - 1) {
  for(j in 1:ncol(mat)) {
    if((isTRUE(mat[i, j] == 1)) && (isTRUE(mat[i + 1, j] == 1))) {
      counter <- counter + 1
    }
  }
}
counter

[1] 91

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>