gpt4 book ai didi

r - 因子水平无效,NA 生成警告

转载 作者:行者123 更新时间:2023-12-04 02:05:41 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Warning message: In `...` : invalid factor level, NA generated

(5 个回答)


7年前关闭。




与此处提出的另一个问题类似,我在主题行中收到错误消息。我试图使用它的补救措施来解决我的问题,但我无法这样做。这是我的代码:

#Change the format of IED deaths to be uniform
USdata$Cause[USdata$Cause=="Hostile - hostile fire - IED attack" | USdata$Cause=="Hostile - hostile fire - IED attack (suicide attack)" | USdata$Cause=="Hostile - hostile fire - IED attack (suicide car bomb)" | USdata$Cause=="Hostile - hostile fire - IED attack (while defusing)" | USdata$Cause=="Hostile - hostile fire - IED attack, RPG" | USdata$Cause=="Hostile - hostile fire - IED attack, RPG, small arms fire" | USdata$Cause=="Hostile - hostile fire - IED Attack, small arms fire" | USdata$Cause=="Hostile - hostile fire - IED Attack, small arms fire, indirect fire"] <- "Hostile - IED Attack"

Warning message:
In `[<-.factor`(`*tmp*`, USdata$Cause == "Hostile - hostile fire - IED attack" | :
invalid factor level, NA generated

我看到当我对我尝试的新值“敌对 - 简易爆炸装置攻击”进行总结时,我得到的所有内容都以 NA 的形式返回。我能够用其他值做类似的事情,但这个不是那么容易。谢谢。

最佳答案

首先从一个因子转换它,进行更改并重新转换回来。另外,%in%从长远来看,可能对您更有效:

ied_causes <- c("Hostile - hostile fire - IED attack",
"Hostile - hostile fire - IED attack (suicide attack)",
"Hostile - hostile fire - IED attack (suicide car bomb)",
"Hostile - hostile fire - IED attack (while defusing)",
"Hostile - hostile fire - IED attack, RPG",
"Hostile - hostile fire - IED attack, RPG, small arms fire",
"Hostile - hostile fire - IED Attack, small arms fire",
"Hostile - hostile fire - IED Attack, small arms fire, indirect fire")

USdata$Cause <- as.character(USdata$Cause)
USdata$Cause[USdata$Cause %in% ied_causes] <- "Hostile - IED Attack"
USdata$Cause <- factor(USdata$Cause)

关于r - 因子水平无效,NA 生成警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25729700/

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