0

Edit: I've corrected the typo in the coding (copy and paste error). I can't add an example of the csv files, as its too complex to model in a simple example (I tried..)

I've spent hours looking through similarly titled questions to solve a for loop problem in R, and have tried a lot of different approaches, but I'm having no luck.

I have many different csv files, each of which has a set of 10 separate strings (variables) identifying a specific row (e.g., names = c("Delta values", "Scream factor", "nightmare mode"). Two rows below such a string, I need the max value of that row of data. I can create loops scanning files for such a value in single csv files using the following

test files- test1.csv, test2.csv, test3.csv test4.csv

names<-list.files(pattern=".csv")
DF <- NULL
for (i in names){
dat <- read.csv(i, header=FALSE, stringsAsFactors=FALSE)
index <- which(dat=="Delta values", arr.ind=TRUE)
row=as.numeric(rownames(dat)[index[1]])
aver=dat[row+2,]
p=max(na.omit(as.numeric(aver)))
DF=rbind(DF, p)
colnames(DF)=dat[index]}

However, my problem comes in trying to generalize it, so that I get a data frame returned indicating the file each value was retrieved from as a row (not "p") and looping over the files so that I can retrieve the next several variables, while appending to the same data frame so that I end up with a data frame listing by row the filename the variable was derived from, and each variable listed in a separate column.

I'm pretty sure I need a nested loop listing the values I want to retrieve as calculated by "p" but I can't find any good examples describing how to iteratively loop using such an approach, and append the new variables to the growing data frame while staying consistent with the row numbering by file.

please help!

plastron
  • 11
  • 2
  • I do not see any processing to identify the targeted rows. (Not my downvote.) – IRTFM Feb 15 '14 at 01:34
  • It returns a single column of values, let me put in an actual example, so its clearer. The code runs as is, its just not good enough currently. – plastron Feb 15 '14 at 02:02
  • @downvoter - it's poor form not to comment! – Brandon Bertelsen Feb 15 '14 at 02:26
  • @plastron this is one of those situations where you're very likely to get a good answer if you spend the time to create a minimal reproducible example. The answers to this question are really instructive for this purpose: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Brandon Bertelsen Feb 15 '14 at 02:29
  • Ok, thanks, Ill spend some time constructing an example which works, thanks for the comment – plastron Feb 15 '14 at 02:39

0 Answers0