我正在寻找一种巧妙的方法来获取仅由列名称组成的向量:
sample <-data.frame(cbind(id = c(1,2,3,4,5,6),
ticker=c("A","A","AA","AA","AAA","AAA"),
value=c(33,23,454,532,3421,3421)))
sample$id <- as.numeric(sample$id)
在上面的数据框中,代码shoule返回这两个列名但不返回id:
"ticker" "value"
如果可能,寻找非循环的基本解决方案。感谢您阅读本文。
答案 0 :(得分:3)
(您不应该为对象指定与基本函数相同的名称,在本例中为sample
。)
names(sample)[sapply(sample, is.factor)]
答案 1 :(得分:1)
被忽略的小*pply
兄弟,rapply
sample <-data.frame(cbind(id = c(1,2,3,4,5,6),
ticker=c("A","A","AA","AA","AAA","AAA"),
value=c(33,23,454,532,3421,3421)))
sample$id <- as.numeric(sample$id)
rapply(sample, class, classes = 'factor')
# ticker value
# "factor" "factor"