r Remove parts of column name after certain characters

You can use

names(df) = gsub(pattern = "_3*", replacement = "", x = names(df))

We can use sub

sub("_3.*", "", df1[,1])
#[1] "col1" "col2" "col3"

We can try the str_extract with regular expression pattern "^[^_]+(?=_)":

stringr::str_extract(c("col1_3x_xxx", "col2_3y_xyz", "col3_3z_zyx"), "^[^_]+(?=_)")
[1] "col1" "col2" "col3"

where in the pattern:

The first ^ matches the beginning of the string; [^_]+ matches one or more non _ character, ^_ means any character but _. (?=...) stands for lookahead, so we are looking for pattern ahead of _.


certainly late for this answer, but just in case someone is looking for a solution

colnames(df1)[col] <-  sub("_3.*", "", colnames(df1)[col])

And if you have multiple columns :

for ( col in 1:ncol(df1)){
    colnames(df1)[col] <-  sub("_3.*", "", colnames(df1)[col])
}

Tags:

R