我想删除县 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)))
您正在使用str_sub()
默认end
参数,它会将您带到字符串的末尾:。试试这个:
x.dist %>% filter(str_sub(county1,1,1)!=str_sub(county2,1,1))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句