user969113
user969113

Reputation: 2429

Replace substring with nothing

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
" "

Upvotes: 0

Views: 641

Answers (1)

Dason
Dason

Reputation: 61933

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

Upvotes: 3

Related Questions