Reputation: 1011
I have several files in one folder that I read into R as a list. Each element in the list is a data frame and I need to remove a random consecutive 6000 rows form each data frame. I can unlist the data frames and pull out the rows but ideally I would like to keep it in the list and just go through each element of the list and remove the rows I need. I thought a for loop or apply function would work but the individual elements don't seem t be recognized as data frames when they're in the list.
Here is what I have so far
files <- list.files('file location')
fs <- lapply(files, read.table, sep=',',skip=3,header=TRUE)
##separates the list into individual data frames
for (i in seq(fs))
assign(paste("df", i, sep = ""), fs[[i]])
##selects a random 6000 rows to remove from a dataframe
n <- nrow(df1)
samp <- sample(1:(n-6000),1)
rmvd <- df1[-seq(from = samp, to =samp+5999),]
I would either like to apply the last part to each dataframe individually and put those back into a list or be able to apply it to the list. I want it in a list in the end because it will be easier to write each dataframe to its own csv file.
Upvotes: 1
Views: 1330
Reputation: 66819
If you stick with the list of data.frames, fs
, instead of assign
ing them, you can do something like
lapply(fs, function(x) x[-(sample(nrow(x)-6000,1)+0:5999), ])
If n=nrow(x)
is ever under 6000
, you're in trouble, of course.
Upvotes: 3