FIXED: Rounding errors

master
Marc Gauch 2022-11-25 16:44:01 +01:00
parent 1871414592
commit a5474df9b3
2 changed files with 16 additions and 10 deletions

View File

@ -1519,13 +1519,16 @@ ordered from wish.com</h2>
valid_percent &lt;- round(p * 100, digits = rounded_digits) valid_percent &lt;- round(p * 100, digits = rounded_digits)
na_percent &lt;- round(na_count / length(data) * 100, digits = rounded_digits) na_percent &lt;- round(na_count / length(data) * 100, digits = rounded_digits)
percent &lt;- round(frequency/total_count*100, digits = rounded_digits) percent_raw &lt;- frequency/total_count*100
percent &lt;- round(percent_raw, digits = rounded_digits)
cumulative_percent &lt;- round(cumsum(p) * 100, digits = rounded_digits) cumulative_percent &lt;- round(cumsum(p) * 100, digits = rounded_digits)
freq_table &lt;- cbind(frequency, percent, valid_percent, cumulative_percent) freq_table &lt;- cbind(frequency, percent, percent_raw, valid_percent, cumulative_percent)
valid_percent_sum &lt;- sum(as.data.frame(freq_table)$percent) valid_percent_sum &lt;- round(sum(as.data.frame(freq_table)$percent_raw), digits = rounded_digits)
Valid_Total &lt;- c(valid_count, valid_percent_sum, 100, NaN) Valid_Total &lt;- c(valid_count, valid_percent_sum, 100, NaN)
freq_table &lt;- subset(freq_table, select = -c(percent_raw))
NAs &lt;- c(na_count, na_percent, NaN, NaN) NAs &lt;- c(na_count, na_percent, NaN, NaN)
Total &lt;- c(total_count, 100, NaN, NaN) Total &lt;- c(total_count, 100, NaN, NaN)
print(rbind(freq_table, Valid_Total, NAs, Total)) print(rbind(freq_table, Valid_Total, NAs, Total))
@ -2420,7 +2423,7 @@ age &lt;- 2021 - birthyears</code></pre>
## 48 1 0.94 0.94 98.11 ## 48 1 0.94 0.94 98.11
## 49 1 0.94 0.94 99.06 ## 49 1 0.94 0.94 99.06
## 50 1 0.94 0.94 100.00 ## 50 1 0.94 0.94 100.00
## Valid_Total 106 99.98 100.00 NaN ## Valid_Total 106 100.00 100.00 NaN
## NAs 0 0.00 NaN NaN ## NAs 0 0.00 NaN NaN
## Total 106 100.00 NaN NaN</code></pre> ## Total 106 100.00 NaN NaN</code></pre>
<pre class="r"><code># with NA <pre class="r"><code># with NA
@ -2449,7 +2452,7 @@ freq(2021 - litdata$D7)</code></pre>
## 48 1 0.32 0.94 98.11 ## 48 1 0.32 0.94 98.11
## 49 1 0.32 0.94 99.06 ## 49 1 0.32 0.94 99.06
## 50 1 0.32 0.94 100.00 ## 50 1 0.32 0.94 100.00
## Valid_Total 106 33.89 100.00 NaN ## Valid_Total 106 33.87 100.00 NaN
## NAs 207 66.13 NaN NaN ## NAs 207 66.13 NaN NaN
## Total 313 100.00 NaN NaN</code></pre> ## Total 313 100.00 NaN NaN</code></pre>
</div> </div>

View File

@ -45,13 +45,16 @@ freq <- function(data, rounded_digits = 2) {
valid_percent <- round(p * 100, digits = rounded_digits) valid_percent <- round(p * 100, digits = rounded_digits)
na_percent <- round(na_count / length(data) * 100, digits = rounded_digits) na_percent <- round(na_count / length(data) * 100, digits = rounded_digits)
percent <- round(frequency/total_count*100, digits = rounded_digits) percent_raw <- frequency/total_count*100
percent <- round(percent_raw, digits = rounded_digits)
cumulative_percent <- round(cumsum(p) * 100, digits = rounded_digits) cumulative_percent <- round(cumsum(p) * 100, digits = rounded_digits)
freq_table <- cbind(frequency, percent, valid_percent, cumulative_percent) freq_table <- cbind(frequency, percent, percent_raw, valid_percent, cumulative_percent)
valid_percent_sum <- sum(as.data.frame(freq_table)$percent) valid_percent_sum <- round(sum(as.data.frame(freq_table)$percent_raw), digits = rounded_digits)
Valid_Total <- c(valid_count, valid_percent_sum, 100, NaN) Valid_Total <- c(valid_count, valid_percent_sum, 100, NaN)
freq_table <- subset(freq_table, select = -c(percent_raw))
NAs <- c(na_count, na_percent, NaN, NaN) NAs <- c(na_count, na_percent, NaN, NaN)
Total <- c(total_count, 100, NaN, NaN) Total <- c(total_count, 100, NaN, NaN)
print(rbind(freq_table, Valid_Total, NAs, Total)) print(rbind(freq_table, Valid_Total, NAs, Total))