gpt4 book ai didi

r - 具有 4 个级别 False、FALSE、True、TRUE 的因子,但只需要 2 个级别

转载 作者:行者123 更新时间:2023-12-02 06:36:13 24 4
gpt4 key购买 nike

我在数据框中有一列设置为具有 4 个级别的因子

False, FALSE, True, TRUE

我需要减少到 2 级

FALSE, TRUE

我已经这样做了(效果很好)但是有没有更好的方法:

df$col1 <- as.character(df$col1)    # change the factor to chr
df$col1 <- toupper (df$col1) # Ensure all are uppercase
df$col1 <- as.factor(df$col1) # change back

最佳答案

简单地使用as.logical:

d <- c("False", "FALSE", "True", "TRUE")
factor(as.logical(d))
# [1] FALSE FALSE TRUE TRUE
# Levels: FALSE TRUE

来自 ?as.logical:

Character strings c("T", "TRUE", "True", "true") are regarded as true, c("F", "FALSE", "False", "false") as false, and all others as NA.

关于r - 具有 4 个级别 False、FALSE、True、TRUE 的因子,但只需要 2 个级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18211049/

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