根据R中的单元格值从数据框中提取列名称

时间:2021-10-16 15:00:53

Hi I have a data frame like this:

嗨,我有一个这样的数据框:

location = c("100 ail","16th and Whitmore","40AB01 - ANTWERPEN") 
lastUpdated = c("2018-02-01 09:30:00", "2018-02-01 03:00:00", "2017-03-07 10:00:00") 
firstUpdated = c("2015-09-01 00:00:00","2016-03-06 19:00:00","2016-11-22 15:00:00")
pm25=c("FALSE","FALSE","FALSE")
pm10=c("TRUE","FALSE","FALSE")
no2=c("TRUE","FALSE","FALSE")
latitude=c(47.932907,41.322470,36.809700)
longitude=c(106.92139000,-95.93799000
,-107.65170000)

df = data.frame(location, lastUpdated, firstUpdated,latitude,longitude,pm25,pm10,no2)

I want to extract the colnames of the first row that have value="TRUE". I tried:

我想提取第一行的值为“TRUE”的colnames。我试过了:

colnames(df[1,6:8]=="TRUE")

but without success.

但没有成功。

1 个解决方案

#1


2  

You have to use the function which to extract the columns that are equal to TRUE

您必须使用提取等于TRUE的列的函数

colnames(df[,6:8][,which(df[1,6:8]=="TRUE")])

#1


2  

You have to use the function which to extract the columns that are equal to TRUE

您必须使用提取等于TRUE的列的函数

colnames(df[,6:8][,which(df[1,6:8]=="TRUE")])