【发布时间】:2020-05-04 14:02:36
【问题描述】:
这是this question 的后续活动。在 data.frame DATA 中,我有一些列是跨越第一列的唯一行的常数,称为 study.name。例如,setting、prof 和 random 列对于 Shin.Ellis 的所有行是 constant,对于 Trus.Hsu 的所有行是 constant 等等.包括Shin.Ellis 和Trus.Hsu,共有10 个唯一的study.name 行。
我想知道如何找到这些常量列的名称?
下面提供了一个解决方案(见NAMES),但我想知道为什么NAMES会输出自始至终不是恒定的"error"?
DATA <- read.csv("https://raw.githubusercontent.com/izeh/m/master/cc.csv")
DATA <- setNames(DATA, sub("\\.\\d+$", "", names(DATA)))
is_constant <- function(x) length(unique(x)) == 1L
(NAMES <- names(Filter(all, aggregate(.~study.name, DATA, is_constant)[-1])) )
# > [1] "setting" "prof" "error" "random" ## "error" is NOT a constant variable
## BUT why it is outputted here!
# Desired output:
# [1] "setting" "prof" "random"
【问题讨论】:
标签: r list function dataframe lapply