![](/img/trans.png)
[英]How to delete rows in a Dataframe that have same string entries two or more columns Using R
[英]R: delete rows that have two columns with the same first digit
我想删除县 fips 代码以相同数字开头的行。
# What I have ----
x.dist = data.frame(county1=c(8001,8001,8001),
mi_to=c(10:12),
county2=c(8005,34502,8007))
> x.dist
county1 mi_to county2
1 8001 10 8005
2 8001 11 34502
3 8001 12 8007
# What I want ----
county1 mi_to county2
2 8001 11 34502
在原始 df 中,county1 和 county2 在第 1 行和第 3 行都以 8 开头。我希望每列中的第一个数字不同,所以我只剩下第 2 行。我该怎么做?
我试过了,但绝对没有任何反应。
w.dist = x.dist %>%
filter(str_sub(county1, start= 1) != (str_sub(county2, start= 1)))
您正在使用带有默认end
参数的str_sub()
,它将您带到字符串的末尾:。 尝试这个:
x.dist %>% filter(str_sub(county1,1,1)!=str_sub(county2,1,1))
带有substr
和subset
的base R
选项
subset(x.dist, substr(county1, 1, 2) != substr(county2, 1, 2))
county1 mi_to county2
2 8001 11 34502
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.