I need to create a simply frequency table, like those ones we see on statistical books. I am working with this data:
我需要创建一个简单的频率表,就像我们在统计书上看到的那样。我正在处理这些数据:
Notas <- c(64,78,66,82,74,103,78,86,103,87,73,95,82,89,73,92,85,80,81,90,78,86,78,101,85,98,75,73,90,86,86,84,86,76,76,83,103,86,84,85,76,80,92,102,73,87,70,85,79,93,82,90,83,81,85,72,81,96,81,85,68,96,86,70,72,74,84,99,81,89,71,73,63,105,74,98,78,78,83,96,95,94,88,62,91,83,98,93,83,76)
And so far I did this:
到目前为止我做到了这一点:
library('dplyr')
tabela <- as.data.frame(Notas)
tabela <- tabela %>%
mutate(fr=round(prop.table(Notas),digits=2),
fr_perc = round(prop.table(Notas)*100,digits=2))
But I cannot believe there isn't an easier way, as its taking too much effort. So I believe I don't know a function which would do this in a very easier way. My intended result is this:
但我无法相信没有更简单的方法,因为它需要付出太多努力。所以我相信我不知道一个能以更简单的方式做到这一点的功能。我的预期结果如下:
My main pain is being to show column two and make data aggregate as suggested by it. This table is to be used with statistics 101 class students, to show then a question's answer, that's why I need to show all those columns, as thats the way they can double check their results.
我主要的痛苦是显示第二列,并按照它的建议进行数据聚合。这个表将与101名班级学生一起使用,以显示问题的答案,这就是为什么我需要显示所有这些列,因为他们可以仔细检查他们的结果。
1 个解决方案
#1
1
After some tries I finally come up with a solution. Isn't pretty but it worked like a charm, so I used it. Anyway if someone knows of a better solution, please, feel free to post it here.
经过一番尝试,我终于想出了一个解决方案。不漂亮,但它就像一个魅力,所以我用它。无论如何,如果有人知道更好的解决方案,请随时在此发布。
library(knitr)
library(dplyr)
Notas <- c(64,78,66,82,74,103,78,86,103,87,73,95,82,89,73,92,85,80,81,90,78,86,78,101,85,98,75,73,90,86,86,84,86,76,76,83,103,86,84,85,76,80,92,102,73,87,70,85,79,93,82,90,83,81,85,72,81,96,81,85,68,96,86,70,72,74,84,99,81,89,71,73,63,105,74,98,78,78,83,96,95,94,88,62,91,83,98,93,83,76)
int_clas <- floor(1+3.3*log10(length(Notas)))
ampl_amos <- ceiling((max(Notas) - min(Notas)) / int_clas)
i <- c(1:int_clas)
tabela <- as.data.frame(i)
base <- array(0,int_clas)
topo <- array(0,int_clas)
notas <- array(0,int_clas)
xi <- array(0,int_clas)
fi <- array(0,int_clas)
Fi <- array(0,int_clas)
Fri <- array(0,int_clas)
Fri_perc <- array(0,int_clas)
for (z in 1:int_clas) {
base[z] <- cbind(min(Notas)+(ampl_amos*(z-1)))
topo[z] <- cbind(min(Notas)+(ampl_amos*z))
notas[z] <- cbind(paste(as.character(base[z])," → ", as.character(topo[z]), sep = " "))
xi[z] <- cbind(ceiling((base[z]+topo[z])/2))
fi[z] <- cbind(sum(Notas>base[z]-1 & Notas<topo[z]))
Fi[z] <- cbind(sum(fi[1:z]))
}
tabela <- tabela %>%
mutate(notas,xi,fi,xifi=xi*fi,fri=fi/sum(fi),fri_perc=fi/sum(fi)*100,Fi)
for (z in 1:int_clas) {
Fri[z] <- cbind(sum(tabela$fri[1:z]))
Fri_perc[z] <- cbind(sum(tabela$fri_perc[1:z]))
}
tabela <- tabela %>%
mutate(Fri,Fri_perc)
kable(tabela,digits = 2)
The "& ra rr;" you can see in code is a html code to insert an arrow like this (→). As I am knitting it to html, I used this code.
“→”你可以在代码中看到一个html代码来插入这样的箭头(→)。当我把它编织成html时,我使用了这段代码。
#1
1
After some tries I finally come up with a solution. Isn't pretty but it worked like a charm, so I used it. Anyway if someone knows of a better solution, please, feel free to post it here.
经过一番尝试,我终于想出了一个解决方案。不漂亮,但它就像一个魅力,所以我用它。无论如何,如果有人知道更好的解决方案,请随时在此发布。
library(knitr)
library(dplyr)
Notas <- c(64,78,66,82,74,103,78,86,103,87,73,95,82,89,73,92,85,80,81,90,78,86,78,101,85,98,75,73,90,86,86,84,86,76,76,83,103,86,84,85,76,80,92,102,73,87,70,85,79,93,82,90,83,81,85,72,81,96,81,85,68,96,86,70,72,74,84,99,81,89,71,73,63,105,74,98,78,78,83,96,95,94,88,62,91,83,98,93,83,76)
int_clas <- floor(1+3.3*log10(length(Notas)))
ampl_amos <- ceiling((max(Notas) - min(Notas)) / int_clas)
i <- c(1:int_clas)
tabela <- as.data.frame(i)
base <- array(0,int_clas)
topo <- array(0,int_clas)
notas <- array(0,int_clas)
xi <- array(0,int_clas)
fi <- array(0,int_clas)
Fi <- array(0,int_clas)
Fri <- array(0,int_clas)
Fri_perc <- array(0,int_clas)
for (z in 1:int_clas) {
base[z] <- cbind(min(Notas)+(ampl_amos*(z-1)))
topo[z] <- cbind(min(Notas)+(ampl_amos*z))
notas[z] <- cbind(paste(as.character(base[z])," → ", as.character(topo[z]), sep = " "))
xi[z] <- cbind(ceiling((base[z]+topo[z])/2))
fi[z] <- cbind(sum(Notas>base[z]-1 & Notas<topo[z]))
Fi[z] <- cbind(sum(fi[1:z]))
}
tabela <- tabela %>%
mutate(notas,xi,fi,xifi=xi*fi,fri=fi/sum(fi),fri_perc=fi/sum(fi)*100,Fi)
for (z in 1:int_clas) {
Fri[z] <- cbind(sum(tabela$fri[1:z]))
Fri_perc[z] <- cbind(sum(tabela$fri_perc[1:z]))
}
tabela <- tabela %>%
mutate(Fri,Fri_perc)
kable(tabela,digits = 2)
The "& ra rr;" you can see in code is a html code to insert an arrow like this (→). As I am knitting it to html, I used this code.
“→”你可以在代码中看到一个html代码来插入这样的箭头(→)。当我把它编织成html时,我使用了这段代码。