Replace substring with nothing

夙愿已清 提交于 2019-12-12 03:38:56

问题


How can I replace everything starting with the _ with just nothing?

Here is a simple data frame

d <- data.frame(a = c("A_foo1", "B_foo2", " _foo3"))

I want d to look like:

a
A
B
" "

回答1:


a <- c("A_foo1", "B_foo2", " _foo3")
gsub("_.*", "", a)
#[1] "A" "B" " "


来源:https://stackoverflow.com/questions/13570312/replace-substring-with-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!