I´m unsuccessfuly trying to read a txt file separated with pipes “|” using readr::read_delim() function, it especificly fails reading the row number 77410 and add a missing value (NA) in a column and moves de values one column.
At lines 77408 to 77411 the original txt file contains this info:
*
3503|c^^ZVSS|240|2|A1|240|19.3965|0.0|0.0|0.0|0.0|0.0|18588.8|7|7|7|9|1|2022-12-02 09:57:02|
3995|c^^]SS|240|2|A1|240|19.3965|0.0|0.0|0.0|0.0|0.0|100300.0|6|6|6|9|1|2022-12-02 12:38:08|
3430|cWUTPK|240|1|A1|240|19.3965|6789.0|0.0|0.0|776.0|0.0|101.153|7|7|7|9|1|2022-12-02 14:57:01|
3172|fa^YWY|300|1|A1|300|19.3965|0.0|0.0|0.0|0.0|0.0|0.299|7|7|7|9|1|2022-12-02 18:01:01|
*
This is the code I’ve used
dic_501 <- read_delim("t_501.txt",
delim = "|",
col_names = FALSE,
col_select = c(1:5, 18, 19, 20),
col_types = cols(X1 = col_integer(),
X2 = col_character(),
X3 = col_integer(),
X4 = col_character(),
X5 = col_character(),
X18 = col_integer(),
X19 = col_character(),
X20 = col_character()
),
escape_backslash = FALSE,
escape_double = TRUE,
skip_empty_rows = TRUE
)
This is what I get
X1 | X2 | X3 | X4 | X5 | X18 | X19 | X20 | |
---|---|---|---|---|---|---|---|---|
77408 | 3503 | c^^ZVSS | 240 | 2 | A1 | 1 | 2022-12-02 09:57:02 | NA |
77409 | 3995 | c^^]SS | 240 | 2 | A1 | 1 | 2022-12-02 12:38:08 | NA |
77410 | NA | 240 | 1 | A1 | 240 | NA | NA | NA |
77411 | NA | 300 | 1 | A1 | 300 | NA | NA | NA |
This is what I expect
X1 | X2 | X3 | X4 | X5 | X18 | X19 | X20 | |
---|---|---|---|---|---|---|---|---|
77408 | 3503 | c^^ZVSS | 240 | 2 | A1 | 1 | 2022-12-02 09:57:02 | NA |
77409 | 3995 | c^^]SS | 240 | 2 | A1 | 1 | 2022-12-02 12:38:08 | NA |
77410 | 3430 | _cWUTPK | 240 | 1 | A1 | 1 | 2022-12-02 14:57:01 | NA |
77411 | 3172 | fa_^YWY | 300 | 1 | A1 | 1 | 2022-12-02 18:01:01 | NA |
I use read.table() to solve this issue:
T_501 <- read.table("t_501.txt",
sep = "|",
header = FALSE,
stringsAsFactors = FALSE,
fill = TRUE,
quote = "",
skipNul = TRUE,
colClasses = c("V3" = 'character', "V6" = "character"))
Eduardo López is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.