从 R 数据框中删除行
我有以下数据框:
> str(df)
'data.frame': 3149 obs. of 9 variables:
$ mkod : int 5029 5035 5036 5042 5048 5050 5065 5071 5072 5075 ...
$ mad : Factor w/ 65 levels "Akgün Kasetçilik ",..: 58 29 59 40 56 11 33 34 19 20 ...
$ yad : Factor w/ 44 levels "BAKUGAN","BARBIE",..: 1 1 1 1 1 1 1 1 1 1 ...
$ donem: int 201101 201101 201101 201101 201101 201101 201101 201101 201101 201101 ...
$ sayi : int 201101 201101 201101 201101 201101 201101 201101 201101 201101 201101 ...
$ plan : int 2 2 3 2 2 2 7 3 2 7 ...
$ sevk : int 2 2 3 2 2 2 6 3 2 7 ...
$ iade : int 0 0 3 1 2 2 6 2 2 3 ...
$ satis: int 2 2 0 1 0 0 0 1 0 4 ...
我想从此数据框中删除 21 个特定行。
> a <- df[df$plan==0 & df$sevk==0,]
> nrow(a)
[1] 21
因此,当我删除这 21 行时,我将得到一个包含 3149 - 21 = 3128 行的新数据框。我找到了以下解决方案:
> b <- df[df$plan!=0 | df$sevk!=0,]
> nrow(b)
[1] 3128
我的上述解决方案使用修改后的逻辑表达式(!=
而不是 ==
和 |
而不是 & ;
)。除了修改原来的逻辑表达式之外,如何获得没有这21行的新数据框?我需要这样的东西:
> df[-a,] #does not work
编辑(特别是对于那些反对者,我希望他们理解为什么我需要替代解决方案):我要求一个不同的解决方案,因为我正在编写很长的代码,并且有我的代码的各个部分中的各种变量分配(例如我的示例中的a
)。因此,当我需要删除代码的前进部分中的行时,我不想返回并尝试在类似 a
的表达式中编写逻辑表达式的逆。这就是为什么 df[-a,] 对我来说更有用。
I have the following data frame:
> str(df)
'data.frame': 3149 obs. of 9 variables:
$ mkod : int 5029 5035 5036 5042 5048 5050 5065 5071 5072 5075 ...
$ mad : Factor w/ 65 levels "Akgün Kasetçilik ",..: 58 29 59 40 56 11 33 34 19 20 ...
$ yad : Factor w/ 44 levels "BAKUGAN","BARBIE",..: 1 1 1 1 1 1 1 1 1 1 ...
$ donem: int 201101 201101 201101 201101 201101 201101 201101 201101 201101 201101 ...
$ sayi : int 201101 201101 201101 201101 201101 201101 201101 201101 201101 201101 ...
$ plan : int 2 2 3 2 2 2 7 3 2 7 ...
$ sevk : int 2 2 3 2 2 2 6 3 2 7 ...
$ iade : int 0 0 3 1 2 2 6 2 2 3 ...
$ satis: int 2 2 0 1 0 0 0 1 0 4 ...
I want to remove 21 specific rows from this data frame.
> a <- df[df$plan==0 & df$sevk==0,]
> nrow(a)
[1] 21
So when I remove those 21 rows, I will have a new data frame with 3149 - 21 = 3128 rows. I found the following solution:
> b <- df[df$plan!=0 | df$sevk!=0,]
> nrow(b)
[1] 3128
My above solution uses a modified logical expression (!=
instead of ==
and |
instead of &
). Other than modifying the original logical expression, how can I obtain the new data frame without those 21 rows? I need something like that:
> df[-a,] #does not work
EDIT (especially for the downvoters, I hope they understand why I need an alternative solution): I asked for a different solution because I'm writing a long code, and there are various variable assignments (like a
's in my example) in various parts of my code. So, when I need to remove rows in advancing parts of my code, I don't want to go back and try to write the inverse of the logical expressions inside a
-like expressions. That's why df[-a,]
is more usable for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需否定您的逻辑下标:
Just negate your logical subscript:
您可以使用
rownames
来指定“补充”数据框。如果它们是数字行名,则更容易:但更一般地,您可以使用:
You can use the
rownames
to specify a "complementary" dataframe. Its easier if they are numerical rownames:But more generally you can use:
您在寻找
subset()
吗?应用于您的示例:
Are you looking for
subset()
?Applied to your example:
你快到了。 'a' 需要是索引向量:
或者,使用您的数据:
You're almost there. 'a' needs to be a vector of indices:
or, with your data:
我不明白你为什么反对你的解决方案,但这是另一种方法。
I don't see why you object to your solution, but here's another way.