【发布时间】:2018-12-25 19:17:34
【问题描述】:
在这个数据框的虚拟列表中,所有列名都有一个前缀,后跟一个下划线。列表中的每个数据框都有不同的前缀,但它们都包含下划线。
如何删除列表中所有数据框的列名中的下划线及其前面的文本?我可以单独完成,但我的真实列表包含更多数据框。谢谢
#add diferent prefixes to all col names
colnames(iris)<-paste('iris_',colnames(iris), sep='')
colnames(mtcars)<-paste('mt_',colnames(mtcars), sep='')
colnames(ToothGrowth)<-paste('TG_',colnames(ToothGrowth), sep='')
#create list of data frames
mylist<-list(iris, mtcars, ToothGrowth)
#name elements of list
names(mylist)<-c('iris','mtcars','ToothGrowth')
#remove prefix from colnames individually for each data frame
colnames(mylist['iris']$iris)<-sub('[^.]+\\_', '', colnames(mylist['iris']$iris))
【问题讨论】:
-
您只需要对列表中的每个元素执行此操作。 stackoverflow.com/questions/12297859/…