所以我有一个csv,我操纵过给我这个叫做的表 Completion
dput(Completion)
structure(list(value = structure(1:16, .Label = c("0%", "100%", "13%", "15%","16%", "24%", "26%", "28%", "33%", "40%", "50%", "53%", "66%", "73%", "75%", "93%"), class = "factor"), All = c(13L, 0L, 3L, 0L, 0L, 1L, 1L, 0L, 1L, 1L, 0L, 2L, 0L, 1L, 0L, 3L),
M0 = c(14L, 10L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L,
0L, 0L, 0L, 0L), M1 = c(17L, 6L, 0L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L), M2 = c(21L, 4L, 0L, 0L,
1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), M3 = c(21L,
2L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 2L, 0L)),
.Names = c("value", "All", "M0", "M1", "M2", "M3"), row.names = c(NA, 16L), class =
c("cast_df", "data.frame"), idvars = "value", rdimnames = list(structure(list(value =
structure(1:16, .Label = c("0%", "100%", "13%", "15%", "16%", "24%", "26%", "28%", "33%",
"40%", "50%", "53%", "66%", "73%", "75%", "93%"), class = "factor")),
.Names = "value", row.names = c("0%", "100%", "13%", "15%", "16%", "24%", "26%", "28%",
"33%", "40%", "50%", "53%", "66%", "73%", "75%", "93%"), class = "data.frame"),
structure(list(Module = structure(1:5, .Label = c("All",
"M0", "M1", "M2", "M3"), class = "factor")), .Names = "Module", row.names = c("All",
"M0", "M1", "M2", "M3"), class = "data.frame")), .Label =
c("0-49","50-59","60-69", "70-79",
"80-89", ">90"))
此表显示课程中给定模块完成(值)%的人数。
我想要做的是为值创建类别如下; 多数(90%-100%),实质性(75%-89%),部分(50%-74%),最小(1%-49%)和不完整(0%) 然后,我希望能够计算出 by module 的所有实例,如下所示:
M0 M1 M2 M3 All
Majority 1 2 3 4 5
Substantial 5 4 3 2 1
Partial 4 3 2 1 5
Minimal 3 2 1 5 4
Incomplete 2 1 5 4 3
每个实例的计数将填满表格。
有办法做到这一点吗?我尝试制作不同的数据透视表,并附加关卡;
comp.rate <- Completion$value
comp.rate <- ordered(comp.rate, levels = c("Majority", "Substantial", "Partial", "Minimal",
"Incomplete"))
我想我不知道如何将我想要的值分配给这些级别然后反过来让R吐出它们的出现次数。我也有问题,因为值是百分比形式...而不是十进制。我没有附上我的完整代码,但如果有任何用处我可以... 任何帮助是极大的赞赏。谢谢!
答案 0 :(得分:1)
您可以这样做:
首先,您必须使用gsub
d$value <- as.numeric(gsub("[%]", "", d$value))
然后您可以使用条件和colSums
来计算所有出现次数:
#Majority(90%-100%)
colSums(d[ d$value >= 90, ])
value All M0 M1 M2 M3
193 3 10 6 4 2
# or Substantial(75%-89%)
colSums(d[ d$value >= 75 & d$value < 89, ])
value All M0 M1 M2 M3
75 0 0 0 0 2
将所有内容保存在一个data.frame中:
a1 <- colSums(d[ d$value >= 90,])
a2 <- colSums(d[ d$value >= 75 & d$value < 89, ])
result <- data.frame(rbind(a1, a2))
result$value <- c("Majority", "Substantial")
result
value All M0 M1 M2 M3
a1 Majority 3 10 6 4 2
a2 Substantial 0 0 0 0 2