简体   繁体   English

r knit html如何创建一个简单的频率表,就像我们在书上看到的那样?

[英]r knit html How can I create a simple frequency table like the ones we see on books?

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: 我的预期结果如下: 一个统计表,如书中的统计表,有10列

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名班级学生一起使用,以显示问题的答案,这就是为什么我需要显示所有这些列,因为他们可以仔细检查他们的结果。

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])," &rarr; ", 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;" “&rarr;” you can see in code is a html code to insert an arrow like this (→). 你可以在代码中看到一个html代码来插入这样的箭头(→)。 As I am knitting it to html, I used this code. 当我把它编织成html时,我使用了这段代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM