0

I have seen lots of answers for this but when I have tried them, my dataframe is empty - "attempt to set an attribute on NULL"

setwd("*my path*")
library(ggplot2)
library(class)
set.seed(3060)

label<-11
index<-000

for(label in 11:37)
{
  for(index in 000:419){

    nameOfFile <- paste(label, index, "features.csv", sep = "_")

    files <- list.files(path=getwd(), pattern=nameOfFile)

    training <- do.call(rbind, lapply(files, read.csv, header=FALSE))


  }    
}

All of my csv files are of the same format - one row and 22 columns.

  • 1
    Not easy to give you a tip here without a reproducible example. First, try if you can read a single file : `tmp <- read.csv(nameOfFile)`. If that works, then you can store these in a list, and merge later outside the loop with do.call. – Marc in the box Jan 07 '19 at 13:13
  • or perhaps take a look into the [`rio`](https://github.com/leeper/rio) package – jay.sf Jan 07 '19 at 13:15
  • Your script should find files called `11_000_features` etc, but it won't necessarily pick them up if they have a suffix (such as `.csv`). The `pattern` argument should be a regular expression, so if your filenames have a suffix, perhaps include `"\\.csv"` or `".*"` in your `paste` statement. – Andrew Gustar Jan 07 '19 at 13:21
  • Thanks @AndrewGustar - it is no longer null but it now only stores one of the files? – user10739557 Jan 07 '19 at 13:29
  • @user10739557 That's because you are overwriting `files` each time you go through the loop. Perhaps you want `files` to be a list, and use the loop to append results to it? Or you could forget the loop altogether and just use `pattern="\\d+_\\d+_features\\.*"` to capture all filenames with the XX_YY_features pattern. – Andrew Gustar Jan 07 '19 at 13:33
  • Replace your forloops with lapplys, something like: `my_all_data <- lapply(11:37, function(label){ lapply(0:419, function(index){ ... }) })` – zx8754 Jan 07 '19 at 13:40
  • Thanks @zx8754 - could you please provide more code to help me understand? – user10739557 Jan 07 '19 at 14:38
  • Maybe related, possible duplicate post: https://stackoverflow.com/questions/11433432/how-to-import-multiple-csv-files-at-once – zx8754 Jan 07 '19 at 15:41

1 Answers1

0

might need to mess with this code a bit but you shouldn't need to loop to read files.

library(tidyverse)

do.call(bind_rows,
        lapply(paste0("data_location/", list.files("data_location/")), read_csv))
B Williams
  • 1,992
  • 12
  • 19