0

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.

6
  • 1
    please add a more realistic snipped of the TXT file. Add the DESIRED RESULT then. Commented Jun 27, 2018 at 8:31
  • 4
    Please make your example fully reproducible and also include what the expected output should look like. For example, it's not clear to me what "change the variables in dependency of a specific variable" means in terms of your example. Commented Jun 27, 2018 at 8:32
  • Thanks for your suggestions and edits, I already updated the question. Hopefully it got clearer Commented Jun 27, 2018 at 9:05
  • what is state ? it's not in your sample data Commented Jun 27, 2018 at 9:25
  • You're right, was an artefact of the original code. Already changed it. Commented Jun 27, 2018 at 9:30

1 Answer 1

5

To remove the first line in each dataframe try this:

lapply(ls, function(x) {x <- x[-1, ]})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That did it and kept the structure of the list (in contrast to my solution)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.