I have several .txt files that I would like to convert into a nice table in RMarkDown. An example of one would be a file containing the following text:
|name|Tin|Tout|Hin|Hout|DT min/2|alpha|
|-----|-----|-----|-----|-----|-----|-----|
|airHEATER_1|3.09|55|0|834|5|1|
|airHEATER_2|4.35|60|0|894.47|5|1|
|airHEATER_3|35.63|70|0|553.51|5|1|
|airHEATER_4|59.78|75|0|245.52|5|1|
|airHEATER_5|78.49|85|0|104.97|5|1|
I have tried everything. The kable function returns nonsense, I have also tried readLines() and read.table() but no luck. I tried converting the .txt file contents into a dataframe with the as.data.drame() function but I get a one-column dataframe with the .txt whole lines as rows, instead of 7 columns.
How would you do it ? I ran out of options here :/
CodePudding user response:
Using read.table
with sep="|"
and some data cleaning you could do:
---
output: html_document
date: "2022-11-09"
---
```{r}
dat <- read.table(text = "|name|Tin|Tout|Hin|Hout|DT min/2|alpha|
|-----|-----|-----|-----|-----|-----|-----|
|airHEATER_1|3.09|55|0|834|5|1|
|airHEATER_2|4.35|60|0|894.47|5|1|
|airHEATER_3|35.63|70|0|553.51|5|1|
|airHEATER_4|59.78|75|0|245.52|5|1|
|airHEATER_5|78.49|85|0|104.97|5|1|", sep = "|", header = TRUE, check.names = FALSE)
```
```{r}
dat <- dat[-c(1, ncol(dat))]
dat <- dat[!grepl("^-", dat[[1]]), ]
```
```{r}
knitr::kable(dat)
```