我的代码如下
got = read.csv("battles.csv")
got = got[!(is.na(got$attacker_king) | got$attacker_king==""), ]
got = got[!(is.na(got$region) | got$region == ""), ]
变量缺少值,但使用上面的代码将其删除
spiderR = table(got$attacker_king, got$region)
spiderR
当我尝试运行上面的代码时,它会显示:
Beyond the Wall The Crownlands The North The Reach The Riverlands
0 0 0 0 0
Balon/Euron Greyjoy 0 0 5 2 0
Joffrey/Tommen Baratheon 0 0 2 0 9
Robb Stark 0 1 1 0 6
Stannis Baratheon 1 1 2 0 0
The Stormlands The Westerlands
0 0
Balon/Euron Greyjoy 0 0
Joffrey/Tommen Baratheon 2 1
Robb Stark 0 2
Stannis Baratheon 1 0
为什么第一行有零?
答案 0 :(得分:1)
如果在levels(got$attacker_king)
中您有一个非预期的因素,您可以获取got
表的一部分并再次运行您的代码。那就是:
# Define the subset of interest
subset.got <- got[got$attacker_king!="",]
# Redefine the factor
subset.got$attacker_king <- factor(subset.got$attacker_king)
# Run just what you did before
spideR <- table(subset.got$attacker_king, subset.got$region)
spideR