[英]Remove Rows From Data Frame where a Row matches a String
I do I remove all rows in a dataframe where a certain row meets a string match criteria?我是否删除了 dataframe 中某行符合字符串匹配条件的所有行?
For example:例如:
A,B,C
4,3,Foo
2,3,Bar
7,5,Zap
How would I return a dataframe that excludes all rows where C = Foo:我将如何返回排除 C = Foo 的所有行的 dataframe:
A,B,C
2,3,Bar
7,5,Zap
Just use the ==
with the negation symbol ( !
).只需将
==
与否定符号 ( !
) 一起使用。 If dtfm is the name of your data.frame:如果 dtfm 是您的 data.frame 的名称:
dtfm[!dtfm$C == "Foo", ]
Or, to move the negation in the comparison:或者,在比较中移动否定:
dtfm[dtfm$C != "Foo", ]
Or, even shorter using subset()
:或者,使用
subset()
甚至更短:
subset(dtfm, C!="Foo")
You can use the dplyr
package to easily remove those particular rows.您可以使用
dplyr
package 轻松删除这些特定行。
library(dplyr)
df <- filter(df, C != "Foo")
I had a column(A) in a data frame with 3 values in it (yes, no, unknown).我在数据框中有一列(A),其中包含 3 个值(是,否,未知)。 I wanted to filter only those rows which had a value "yes" for which this is the code, hope this will help you guys as well --
我只想过滤那些值为“是”的行,这是代码,希望这对你们也有帮助——
df <- df [(!(df$A=="no") & !(df$A=="unknown")),]
if you wish to using dplyr, for to remove row "Foo":如果您希望使用 dplyr 来删除行“Foo”:
df %>%
filter(!C=="Foo")
I Know this has been answered but doing this if anyone can get help from this我知道这已得到解答,但如果有人可以从中获得帮助,请执行此操作
library (dplyr) df %>% filter(!c=="foo)库 (dplyr) df %>% 过滤器(!c=="foo)
df[,df$c=="foo", ] df[,df$c=="foo", ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.