Reputation: 513
May be too much questions at once, but I'm not sure where to start to change my way of thinking. Task: I want to create and manipulate various txt files that control a delphi model. I want to use R for that.
What the files initially look like:
[submodelname1]
variable1=value1
variable2=value2
[submodelname2]
variable3=value3
variable4=value4
In the end I want to change the variables in dependency of a specific variant defined by up to 4 factors. So the files for each variant will look the same as in the beginning but differ in the values per variant. The filenames should than be: Factor1_factor2_factor3_factor4.txt for each variant. I already solved that last step as I tried an for-loop approach. But when it got too complicated with the many chained loops I switched to work with lists.
My work on the list-approach so far:
# read a sample file to get the desired pattern
file <- read.table(file="file.txt", sep="=", dec=".", header=FALSE,
fill=TRUE, blank.lines.skip = TRUE)
# extracted submodel names in "[]"
sublistnames <- as.factor(droplevels(file[grep("[\b[A-Z0-9]]", file$V1),1]))
# list of data.frame per submodel
ls <- split(file, cumsum(1:nrow(file) %in% grep("[\b[A-Z0-9]]", file$V1)))
# names the lists like the submodels
names(ls) <- sublistnames
Now there are still the submodel names in the first line of each data.frame of the list and I still fail to erase them after hours of studying SO threads dealing with lists. I learned
# this line addresses the rows I want to get rid of, but "<- NULL" doesn't work
lapply(ls, "[", 1, c(1,2))
Any suggestions how I solve this problem? Or any ideas how to face the task any other way? I'm eager to learn where I my thinking is wrong. Thanks in advance.
In the meantime I tried :
for (i in 1:length(ls)) {
ls[[i]][1,] <- NA
}
ls <- lapply(ls, function(x) x[!is.na(x)])
But I am not satisfied with the output and I think there is a more elegant way.
Upvotes: 0
Views: 3067
Reputation: 8374
To remove the first line in each dataframe try this:
lapply(ls, function(x) {x <- x[-1, ]})
Upvotes: 5