簡體   English   中英

r knit html如何創建一個簡單的頻率表,就像我們在書上看到的那樣?

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

我需要創建一個簡單的頻率表,就像我們在統計書上看到的那樣。 我正在處理這些數據:

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)

到目前為止我做到了這一點:

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))

但我無法相信沒有更簡單的方法,因為它需要付出太多努力。 所以我相信我不知道一個能以更簡單的方式做到這一點的功能。 我的預期結果如下: 一個統計表,如書中的統計表,有10列

我的主要痛苦是顯示第二列並按照它的建議進行數據聚合。 這個表將與101名班級學生一起使用,以顯示問題的答案,這就是為什么我需要顯示所有這些列,因為他們可以仔細檢查他們的結果。

經過一番嘗試,我終於想出了一個解決方案。 不漂亮,但它像一個魅力,所以我用它。 無論如何,如果有人知道更好的解決方案,請隨時在此發布。

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)

“&rarr;” 你可以在代碼中看到一個html代碼來插入這樣的箭頭(→)。 當我把它編織成html時,我使用了這段代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM