2

I have squared data in a txt file with "|" separator data with value like this

no| value
1|  3,123.00
2|  1,122.75

import it with this code:
library(readr)
data <- read_delim("file.txt", "|", trim_ws = TRUE, locale = locale(decimal_mark = "."), col_types = cols(no = col_double(),
value = col_double()))

Warning: 2 parsing failures.
row    col  expected               actual   file
  1 value   no trailing characters ,123.00 'file.txt'
  2 value   no trailing characters ,122.75 'file.txt'

This make the data imported to be NA, I already spesified the local to point decimal mark.

Why are my values read like this: ,123.00 ?, the first number before comma is missing If i specify col_types with col_double. it works without col_types spesified but i really need to specify the col_types

1 Answers1

0

You could do it in two steps: read value as string and then cast to numeric.

library("tidyverse")
library("readr")

file <- "
no| value
1|  3,123.00
2|  1,122.75
"

x <- read_delim(
  file, "|", trim_ws = TRUE,
  col_types = cols(value = col_character())) %>%
  mutate_at(vars(value), parse_number)
x
#> # A tibble: 2 x 2
#>      no value
#>   <dbl> <dbl>
#> 1     1 3123 
#> 2     2 1123.

# Are fractions missing? No they are not.
x$value
#> [1] 3123.00 1122.75

Created on 2019-03-26 by the reprex package (v0.2.1)

dipetkov
  • 3,380
  • 1
  • 11
  • 19