计算值并将其添加到数据框

时间:2019-04-01 17:34:46

标签: r

我的数据集如下:

"userid","progress"
1, incomplete
2, complete
3, not attempted
4, incomplete
5, not attempted
6, complete
7, complete
8, complete
9, complete
10, incomplete

我想制作一个饼状图,显示状态为已完成,未完成且未尝试的人所占的百分比,即total no of users/user id = complete/incomplete

此代码不起作用。

var1 = nrow(data1)/sum(data1$progress=="complete")
var2 = nrow(data1)/sum(data1$progress=="incomplete")

  df <- data.frame(
    val = c (var1, var2)

  )

  hchart(df, "pie")%>%hc_add_series_labels_values(values = df)

2 个答案:

答案 0 :(得分:3)

如果您要制作饼图,大多数方法将为您完成很多工作。无需显式计算百分比。无论如何,table的输出正是您想要与pie一起输出的

# Load your data
ds <- read.csv(header = TRUE, text = 
"userid,progress
1, incomplete
2, complete
3, not attempted
4, incomplete
5, not attempted
6, complete
7, complete
8, complete
9, complete
10, incomplete")

# Tabularize
tab <- table(ds$progress)
pie(tab) # Make piechart

enter image description here

如下所示,table计算每个级别的出现次数,并返回一个命名的integer向量。这里的好处是pie()根据相对频率计算角度/面积,并使用名称来标记图表。

print(tab)
#
#      complete     incomplete  not attempted 
#             5              3              2 

如果您坚持自己计算百分比,则可以使用tab/sum(tab)

编辑:我发现您尝试使用 highcharter 软件包。在这种情况下为什么不使用hcpie?该函数将一个因素作为输入:

library("highcharter")
hcpie(ds$progress)

enter image description here

答案 1 :(得分:0)

像这样:

userid <- c(1,2,3,4,5,6,7,8,9,10)
progress <- c("incomplete","complete", "not attempted", "incomplete", "not attempted", "complete","complete","complete", "complete","incomplete")
df <- data.frame("userid"=userid, "progress"=progress)
df$progress <- as.factor(df$progress)
var1 = nrow(df[which(df$progress=="complete"), ])/nrow(df)
var2 = nrow(df[which(df$progress=="incomplete"), ])/nrow(df) 
var3 = nrow(df[which(df$progress=="not attempted"), ])/nrow(df) 
data <- c(var1, var2, var3)
pie(data, labels=c("complete","incomplete", "not attempted"))