1

The csv file contains comma (,) in the quotation. read_csv function convert them into numeric numbers which suppose to keep as character.

library(readr)
read_csv('"Name","V1","V2"\n
"A","0,20","300,200"\n
"B","0,20","300,200"')

The results look like

# A tibble: 2 x 3
  Name  V1        V2
  <chr> <chr>  <dbl>
1 A     0,20  300200
2 B     0,20  300200

I expect the column V2 keeps the same as character.

How should I fix it?

My Session Info

> sessionInfo()
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_Australia.1252 
[2] LC_CTYPE=English_Australia.1252   
[3] LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C                      
[5] LC_TIME=English_Australia.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets 
[6] methods   base     

other attached packages:
[1] readr_1.4.0

loaded via a namespace (and not attached):
 [1] fansi_0.5.0     utf8_1.2.2      crayon_1.4.1   
 [4] R6_2.5.0        lifecycle_1.0.0 magrittr_2.0.1 
 [7] pillar_1.6.1    rlang_0.4.11    cli_3.0.1      
[10] rstudioapi_0.13 vctrs_0.3.8     ellipsis_0.3.2 
[13] tools_4.1.0     hms_1.1.0       compiler_4.1.0 
[16] pkgconfig_2.0.3 tibble_3.1.3
Bangyou
  • 9,462
  • 16
  • 62
  • 94

1 Answers1

2

Two options -

  1. Pass the grouping_mark in locale to something which is not present in the data.
library(readr)

read_csv('"Name","V1","V2"\n
"A","0,20","300,200"\n
"B","0,20","300,200"', locale = locale(grouping_mark = "@"))

#  Name  V1    V2     
#  <chr> <chr> <chr>  
#1 A     0,20  300,200
#2 B     0,20  300,200
  1. Pass the class of columns explicitly.
read_csv('"Name","V1","V2"\n
"A","0,20","300,200"\n
"B","0,20","300,200"', col_types = 'ccc')
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213