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