删除 R 中数据框中所有列名的前两个字符 [英] remove first two characters for all column names in a data frame in R
本文介绍了删除 R 中数据框中所有列名的前两个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从数据框中的所有列名中按位置删除字符串
Is there a way to remove character strings by position from all column names in a data frame
例如,如果我有这样的列名:
for eg if i have column names like:
ab_sales1 kj_sales2 lm_sales3 .....pk_sales100
10 34 64 ..... 288
我希望我的输出列名称类似于
I would like my output column names to be something like
sales1 sales2 sales3 .....sales100
10 34 64 .... 288
我知道可以在行上使用字符串函数,但我找不到列名的内容
I know string functions can be used on rows but I could not find something for column names
推荐答案
使用 substring()
df <- data.frame(ab_sales1 = rnorm(6),
kj_sales2 = rnorm(6),
lm_sales3 = rnorm(6),
pk_sales100 = rnorm(6))
names(df) <- substring(names(df), 4)
这给出:
sales1 sales2 sales3 sales100
1 0.9486393 0.4727444 -1.5982694 0.01102933
2 0.2980252 -0.7979390 -2.2574233 -0.37381571
3 -0.5788511 -0.4873044 2.1668715 -0.26525840
4 -1.0711035 1.0311850 0.3495215 -0.58936920
5 0.2432300 1.7801097 -1.1982068 0.14810607
6 1.6965152 0.9655296 -1.1000140 -1.02301506
这篇关于删除 R 中数据框中所有列名的前两个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文