0

I have read multiple questionnaire files into DFs in R. Now I want to create new DFs based on them, buit with only specific rows in them, via looping over all of them.The loop appears to work fine. However the selection of the rows does not seem to work. When I try selecting with simple squarebrackts, i get the error "incorrect number of dimensions". I tried it with subet(), but i dont seem to be able to set the subset correctly.

Here is what i have so far:

 for (i in 1:length(subjectlist)) {
  p[i] <- paste("path",subjectlist[i],sep="")
  files <- list.files(path=p,full.names = T,include.dirs = T)
  assign(paste("subject_",i,sep=""),read.csv(paste("path",subjectlist[i],".csv",sep=""),header=T,stringsAsFactors = T,row.names=NULL))
  assign(paste("subject_",i,"_t",sep=""),sapply(paste("subject_",i,sep=""),[c((3:22),(44:63),(93:112),(140:159),(180:199),(227:246)),]))
  }
eipi10
  • 91,525
  • 24
  • 209
  • 285
Maulig
  • 3
  • 3

1 Answers1

0

Here's some code that tries to abstract away the details and do what it seems like you're trying to do. If you just want to read in a bunch of files and then select certain rows, I think you can avoid the assign functions and just use sapply to read all the data frames into a list. Let me know if this helps:

# Get the names of files we want to read in
files = list.files([arguments])

df.list = sapply(files, function(file) {

  # Read in a csv file from the files vector
  df = read.csv(file, header=TRUE, stringsAsFactors=FALSE)

  # Add a column telling us the name of the csv file that the data came from
  df$SourceFile = file

  # Select only the rows we want
  df = df[c(3:22,44:63,93:112,140:159,180:199,227:246), ]

}, simplify=FALSE)

If you now want to combine all the data frames into a single data frame, you can do the following (the SourceFile column tells you which file each row originally came from):

# Combine all the files into a single data frame
allDFs = do.call(rbind, df.list)
eipi10
  • 91,525
  • 24
  • 209
  • 285
  • hi, thanks for the answer. i am not sure if i get it completely. however, i want to read in all the files into seperate DFs and then select from those each time specific rows. sorry if i am not being concrete enough, i a newbie. – Maulig Oct 19 '15 at 09:18
  • The code in the answer will do that. `list.files` returns a vector of file names (if you include the argument `full.names=TRUE` this will include the full paths if you have files in more than one directory). The code after that creates a list that contains one data frame for each file in `files` with only the rows you selected included. – eipi10 Oct 19 '15 at 14:48