Source: https://www.reddit.com/r/Rlanguage/comments/t61r9n/can_somebody_please_help_part_of_a_quality/
base R FunctionsUses only base R
Code is very simple to understand
datFile <- "2022-03-03_010_100_Generate_Freq_Table_Data.txt"
dat2 <- as.numeric(readr::read_lines(datFile))
table(dat2)
## dat2
## 11.6 12.6 12.7 12.8 13.1 13.6 13.8 14.1 14.3 14.6 14.8 15.1 15.6 15.7 15.8 15.9
## 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 2
## 16.1 16.2 16.4 16.5 16.6 17 17.1 17.3 17.4 17.6 17.7 18.1 18.3 18.5 18.8 19.2
## 1 1 1 3 1 1 1 1 2 1 1 1 2 1 1 1
## 20.3
## 1
tidyverse PackagesCoding logic isn’t as simple as that shown in Option 1, but it’s still easy to understand
This code is more rugged than what is shown in Option 1, which has multiple advantages.
The pipeline nature of the tidyverse enables easy extension of the code without worrying about breaking something.
If the code is modified, errors are more likely to be caught &emdash; even without writing explicit checks — because the tidyverse generally has strong type checking.
library(readr)
library(dplyr)
datFile <- "2022-03-03_010_100_Generate_Freq_Table_Data.txt"
dat1 <- readr::read_fwf(
file = datFile,
col_positions = readr::fwf_cols(vals = 4),
col_types = cols(readr::col_double()),
trim_ws = TRUE
)
dat2 <- dat1 |>
group_by(vals) |>
summarise(n = n())
| vals | n |
|---|---|
| 11.6 | 1 |
| 12.6 | 1 |
| 12.7 | 1 |
| 12.8 | 1 |
| 13.1 | 1 |
| 13.6 | 1 |
| 13.8 | 1 |
| 14.1 | 1 |
| 14.3 | 2 |
| 14.6 | 1 |
| 14.8 | 1 |
| 15.1 | 1 |
| 15.6 | 1 |
| 15.7 | 1 |
| 15.8 | 2 |
| 15.9 | 2 |
| 16.1 | 1 |
| 16.2 | 1 |
| 16.4 | 1 |
| 16.5 | 3 |
| 16.6 | 1 |
| 17.0 | 1 |
| 17.1 | 1 |
| 17.3 | 1 |
| 17.4 | 2 |
| 17.6 | 1 |
| 17.7 | 1 |
| 18.1 | 1 |
| 18.3 | 2 |
| 18.5 | 1 |
| 18.8 | 1 |
| 19.2 | 1 |
| 20.3 | 1 |
The contents of the file named 2022-03-03_010_100_Generate_Freq_Table_Data.txt, which contains the raw data, are shown here.
11.6
14.3
15.8
16.5
17.7
12.6
14.3
15.9
16.6
18.1
12.7
14.6
15.9
17.0
18.3
12.8
14.8
16.1
17.1
18.3
13.1
15.1
16.2
17.3
18.5
13.6
15.6
16.4
17.4
18.8
13.8
15.7
16.5
17.4
19.2
14.1
15.8
16.5
17.6
20.3