Forums | OpenR

OpenR: R & Statistics /
role_count的环境变量是NA


Ya Wang's profile picture
Posts: 2

22 November 2022, 8:45 PM

library(ggplot2)
roles = readLines("ENVpractice/人物别称.txt")
txt = readLines("ENVpractice/倚天屠龙记.txt")
roles1 = paste0("(", gsub(" ", ")|(", roles), ")")
roles_l = strsplit(roles, " ") # 总结每个人的不同称呼

#计算每个人物出场总次数
role_para = sapply(roles1, grepl, txt)
colnames(role_para) = sapply(roles_l, function(x) x[1])
role_count = data.frame(role = factor(colnames(role_para),
levels = colnames(role_para[order(colSums(role_para), decreasing = TRUE)])),
count = colSums(role_para))

ggplot(role_count, aes(x = role, y = count, fill = role)) +
geom_bar(stat = "identity", width = 0.75) +
labs(title="倚天屠龙记主要人数登场次数",x="人物",y="登场次数")+
geom_text(aes(label=count),size=5,vjust=-0.2)+
theme(axis.text=element_text(size=20, angle = 90),
axis.title=element_text(size=15,face="bold"),
axis.title.x = element_text(vjust=-1),
legend.position="none",
plot.title = element_text(size=20,face='plain',hjust = 0.5),
panel.grid = element_blank())

Peng Zhao's profile picture
Posts: 128

22 November 2022, 9:46 PM

Please describe your issue in the post, so that everyone could understand it. 

Did you upload an irrelevant file?

BTW, in English.

Ya Wang's profile picture
Posts: 2
Shanxing Gong's profile picture
Posts: 6

23 November 2022, 12:59 PM

Because in this step

colnames(role_para) = sapply(roles_l, function(x) x[1])

you did not set what function(x) x[1] is, the colnames(role_para) will be full of NA. Then in this step

role_count = data.frame(role = factor(colnames(role_para),

you set role_count$role=factor(colnames(role_para)), then role_count$role will definately be full of NA. 

Peng Zhao's profile picture
Posts: 128

23 November 2022, 6:00 PM

You mentioned 人物别称.txt in your script, but I can't find it in your attachments.

5 results