【R语言】按照列名筛选数据框
先定义一个简单的数据框
a<-c("a","b","c","d")
b<-c("1","2","3","4")
c<-c("spring","summer","autumn","winter")
d<-c("2","4","6","8")
data<-data.frame(a,b,c,d)
names(data)<-c("letter","number","season","letter2")
初始数据框如下
例1. 删除数据框中指定列名的列
##remove columns
cols_remove<-c("letter","number")
newdata<-data[ ,!(colnames(data) %in% cols_remove)]
删除指定列后的数据框
例2. 按列名提取数据框中指定列
cols_remain<-c("letter","season")
newdata2<-data[ ,colnames(data) %in% cols_remain]
按列名提取后的新数据框