简体   繁体   中英

R changing string pattern of column names

is there an easy order to change the string pattern of my column names? I've got a data set like the following, and I would like to have all the column names without the "_R1".

df <- read.table(header=TRUE, text="
T_H_R1 T_S_R1 T_A_R1 T_V_R1 T_F_R1 
5 1 2 1 5 
3 1 3 3 4 
2 1 3 1 3  
4 2 5 5 3 
5 1 4 1 2 
")

Thank you!

We can use sub to match the _R1 at the end ( $ ) of the string of names , and replace with blank ( "" )

names(df) <- sub("_R1$", "", names(df))
names(df)
#[1] "T_H" "T_S" "T_A" "T_V" "T_F"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM