简体   繁体   English

从R数据框中删除行

[英]Removing rows from R data frame

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. 我想从此数据框中删除21个特定行。

> 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. 因此,当我删除这21行时,我将有一个3149 - 21 = 3128行的新数据帧。 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? 除了修改原始逻辑表达式之外,如何在没有这21行的情况下获取新数据帧? 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. 编辑 (特别是对downvoters,我希望他们明白,为什么我需要一个替代的解决方案):我问了不同的解决方案,因为我正在写一个长码,和(好像有各种变量赋值a在我的例子“收费)我的代码的各个部分。 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. 因此,当我需要删除代码前进部分中的行时,我不想回过头来尝试在a表达式中编写逻辑表达式的反转。 That's why df[-a,] is more usable for me. 这就是为什么df[-a,]对我来说更有用。

只是否定你的逻辑下标:

a <- df[!(df$plan==0 & df$sevk==0),]

You can use the rownames to specify a "complementary" dataframe. 您可以使用rownames指定“互补”数据框。 Its easier if they are numerical rownames: 如果它们是数字rownames它更容易:

df[-as.numeric(rownames(a)),]

But more generally you can use: 但更一般地说,您可以使用:

df[setdiff(rownames(df),rownames(a)),]

Are you looking for subset() ? 你在寻找subset()吗?

dat <- airquality
dat.sub <- subset(dat, Temp > 80 & Month < 10)

dim(dat)
dim(dat.sub)

Applied to your example: 适用于您的示例:

df.sub <- subset(df, plan != 0 & sevk != 0)

You're almost there. 你快到了。 'a' needs to be a vector of indices: 'a'需要是指数的矢量:

    df <- data.frame(plan=runif(10),sevk=runif(10))
    a <- c(df$plan<.1 | df$sevk < .1) # some logical thing
    df[-a,]

or, with your data: 或者,与您的数据:

    a <- c(df$plan==0 & df$sevk==0)
    df[-a,]

I don't see why you object to your solution, but here's another way. 我不明白你为什么反对你的解决方案,但这是另一种方式。

which( df[df$plan==0 & df$sevk==0,], arr.ind=TRUE) ->killlist 
newdf <- df[-c(killlist[1,])] 

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

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