简体   繁体   English

在R中的不同数据集中删除多行

[英]Deleting multiple rows in different data sets in R

I am working with multiple data.frames and I encountered problem when I want to delete specific rows in all of the data.frames . 我正在使用多个data.frames ,当我想删除所有data.frames特定行时遇到了问题。 My problem is similar to the one posted here , but I cannot make it work on the rows: 我的问题类似于此处发布的问题 ,但是我无法使其在行上起作用:

lst <- list(dat1,dat2)
lapply(lst,`[`, !="A", TRUE)

Assuming my tables look like this and there are multiple row names so it is not possible to extract all but A by c("B", "C","D", ...) : 假设我的表看起来像这样,并且有多个行名,那么就不可能通过c("B", "C","D", ...)来提取除A以外的所有内容:


|   | 1 | 2 | 3 | 
|---|---|---|---|
| A |   |   |   | 
| B |   |   |   |
| A |   |   |   |

|   | 1 | 2 | 3 | 
|---|---|---|---|
| C |   |   |   | 
| A |   |   |   |
| A |   |   |   |

I would be grateful for any help! 我将不胜感激! Thanks 谢谢

If you want to use more complicated functions in an *apply call it's often useful or even necessary to use anonymous functions. 如果要在*apply调用中使用更复杂的函数,则使用匿名函数通常非常有用,甚至有必要。 The advantage is that you can build that function that is supposed to be applied to every list element (here, data.frames) just like you would do it in the case of a single data.frame. 好处是您可以构建应该应用于每个列表元素(在此为data.frames)的函数,就像在单个data.frame的情况下那样。

If you wanted to subset rows of a regular data.frame by rownames you would use something like 如果您想按行名对常规data.frame的行进行子集化,则可以使用类似

mtcars[rownames(mtcars) != "Mazda RX4", ]

And in your lapply call you will do it the same way using an anonymous function where x represents each list element: 在您的lapply调用中,您将使用匿名函数以相同的方式进行操作,其中x表示每个列表元素:

lapply(lst, function(x) x[rownames(x) != "A", ])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM