gpt4 book ai didi

根据 R 中的条件重命名因子水平

转载 作者:行者123 更新时间:2023-12-02 04:45:21 24 4
gpt4 key购买 nike

我想将计数小于 n 的所有因子合并为一个名为“Else”的因子

例如,如果 n = 3,那么在下面的 df 中,我想将“c”、“d”和“e”组合为“Else”:

df = data.frame(x=c(1:10), y=c("a","a","a","b","b","b","c","d","d","e"))

我首先得到一个包含所有低计数值的 df:

library(plyr)
lowcounts = ddply(df, "y", function(z){if(nrow(z)<3) nrow(z) else NULL})

我知道我可以手动更改这些,但实际上我有几十个级别,所以我需要自动化。

我只想选择并重命名 %in% lowcount in levels(df) 关卡,其余的保持不变,但不确定如何继续。

最佳答案

另一种选择:

#your dataframe
df = data.frame(x=c(1:10), y=c("a","a","a","b","b","b","c","d","d","e"))

#which levels to keep and which to change
res <- table(df$y)
notkeep <- names(res[res < 3])
keep <- names(res)[!names(res) %in% notkeep]
names(keep) <- keep

#set new levels
levels(df$y) <- c(keep, list("else" = notkeep))

df
# x y
#1 1 a
#2 2 a
#3 3 a
#4 4 b
#5 5 b
#6 6 b
#7 7 else
#8 8 else
#9 9 else
#10 10 else

关于根据 R 中的条件重命名因子水平,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19905237/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com