与这里提出的另一个问题类似,我收到了主题行中的错误消息。我试图使用它的解决方法来解决我的问题,但我无法做到这一点。这是我的代码:
#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
当我总结我的尝试新值“Hostile-IED Attack”时,我发现所有的返回值都是NA。我能做类似的事情来处理其他值,但这一个不那么容易。谢谢。
read.table
或read.csv
读取数据时,需使用stringsAsFactors=FALSE
,以避免将字符向量转换为因子(factor)。 - akrun