[英]Drop a word in a column of data table in R
I have a data table that looks like我有一个看起来像的数据表
location county
1: 40.96875_-72.78125 Walla Walla County
2: 41.15625_-90.65625 Mercer County
3: 41.21875_-90.65625 Mercer County
4: 41.28125_-89.84375 Bureau County
5: 41.28125_-89.90625 Henry County
How can I, efficiently, drop the word "county" in the county
column, so, only the name of counties is left in there.我怎样才能有效地在
county
列中去掉“县”这个词,所以只留下县名。
We can use sub
to select one or more spaces followed by characters that are not a space until the end ( $
) of the string, replace with blanks ( ""
)我们可以使用
sub
来选择一个或多个空格后跟不是空格的字符,直到字符串的结尾( $
),替换为空格( ""
)
dt1[, county := sub("\\s+[^ ]+$", "", county)]
dt1$county
#[1] "Walla Walla" "Mercer" "Mercer" "Bureau" "Henry"
If it is specific to 'County' word, then如果它特定于“县”字,那么
dt1[, county := sub("\\s+County$", "", county)]
或者,您可以使用 de gsub
函数,将单词"county"
替换为""
:
df$county = gsub("county", "", df$county)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.