简体   繁体   English

R:删除data.frames列表中的行

[英]R: Remove rows in a list of data.frames

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. 任务:我想创建和操作各种控制delphi模型的txt文件。 I want to use R for that. 我想为此使用R。

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. 最后,我想根据最多由4个因素定义的特定变体来更改变量。 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. 文件名应为:每个变量的Factor1_factor2_factor3_factor4.txt。 I already solved that last step as I tried an for-loop approach. 在尝试for循环方法时,我已经解决了最后一步。 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. 现在,列表的每个data.frame的第一行中仍然有子模型名称,在研究了处理列表的SO线程数小时之后,我仍然无法删除它们。 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. 但是我对输出不满意,我认为还有一种更优雅的方法。

要删除每个数据框中的第一行,请尝试以下操作:

lapply(ls, function(x) {x <- x[-1, ]})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 r从data.frames列表中删除异常值,并创建一个data.frames新列表? - r remove outliers from a list of data.frames and make a new list of data.frames? 使用R选择data.frames列表中的data.frames,基于行数 - Selecting data.frames in a list of data.frames, based on numbers of rows, with R 将data.frame列表中的data.frame列名称分配给R中data.frame列表中的其他(空间)data.frames - Assign column names of data.frames in a list of data.frames to other (Spatial) data.frames in a list of data.frames in R R中data.frames列表的子集 - subset from a list of data.frames in R 从 data.frames 列表中创建具有特定行的 data.frames 列表 - Create list of data.frames with specific rows from list of data.frames 从data.frames列表中删除data.frames中特定的重复观测值 - Remove specific duplicated observations in data.frames from a list of data.frames 在 R 中使用 lapply 循环遍历 R 中的 data.frames 列表 - Using lapply in R to loop through a list of data.frames in R R 中两个 data.frames 行的每个组合的快速 setdiff - Fast setdiff for each combinations of rows of two data.frames in R 将R中的两个data.frames组合在一起,具有不同的行 - Combine two data.frames in R with differing rows R:将data.frames列表转换为栅格列表 - R: Convert list of data.frames to list of raster
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM