我想知道是否存在一个简单的函数(类似于drop.levels)来从仅包含一个观察的因子中删除级别。我将在下面提供一个可重现的例子。到目前为止,我只能通过一次观察来存储包含级别的因子的名称,但是编写所有代码以降低特定级别将会很痛苦,是否有一些快速的方法可以做到这一点?
db0 <- data.frame(let = c(sample(letters[1:5], 99, replace = T),"z"),
let2 = sample(letters[6:11], 100, replace = T))
#Checking which factor has levels with only one obs
facLevels <- lapply(db0, table)
facNames <- list()
for(i in 1:length(facLevels)){
facNames[i]<-ifelse(min(facLevels[[i]])==1, names(facLevels[i]), NA)
}
facNames <- as.character(facNames[!is.na(facNames)])
基本上我想做的就是在let中仅删除z级别。 感谢。
答案 0 :(得分:0)
这里的std::string
循环将设置任何因子级别,其中一个观察到NA,然后通过重构从列中完全删除该因子级别。
strcpy
答案 1 :(得分:0)
如果你不喜欢写循环
# create a sample dataset
db0 <- data.frame(let1 = c(sample(letters[1:5], 99, replace = T),"z"),
let2 = sample(letters[6:11], 100, replace = T))
# calculate how many times each level is present
facLevel <- lapply(db0, table)
# drop levels which are present once
test <- sapply(facLevel, function(x) x[x != 1])
# drop rows in the original dataset where a unique level is present (do this for both columns)
db1 <- db0[rowSums(mapply(function(x, y) x %in% names(y), db0, test)) == ncol(db0), ]