R 的新手,想从我的数据框中的列名中删除无用的部分
df <- data.frame(column.0.1 = c(1,2,3,4,5),
column.0.2 = c(1,2,3,4,5),
column.0.3 = c(1,2,3,4,5))
#i have tried
colnames(df) <- sub('\\.[^.]+$', '', colnames(df))
colnames(df)
[1] "column.0" "column.0" "column.0"
#Instead, I want to remove the useless 0 in the middle of the column names so the result would be
[1] "column.1" "column.2" "column.3"
非常感谢!
最佳答案
如果你的名字都有一个额外的“.0”,你就非常接近了:
colnames(df) <- sub('\\.0', '', colnames(df))
colnames(df)
[1] "column.1" "column.2" "column.3"
关于从R中的列名中间删除部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61481713/