Home > OS >  How can I read values from a text file with values that have an apostrophe separating the thousands?
How can I read values from a text file with values that have an apostrophe separating the thousands?

Time:05-02

I have a dataset with values that are separated with an apostrophe at the thousands, f.e. 3'203.12. I can read those values with read.table, but when I want to plot them, the values above 1000 are converted to NAs, because of the apostrophe. How can I prevent this, or alternatively how can I remove all apostrophes in a text file?

CodePudding user response:

Open the file in a text editor (e.g. to open with Notepad on Windows: right-click on the file and then choose Open With and select Notepad) and replace all apostrophes by nothing (Ctrl-H in Notepad, then put ' under Find What and leave Replace With empty; then click on Replace All). Save this file under a different name (e.g. if the file was called dummy.csv save as dummy_mod.csv) and then use read.table to upload dummy_mod.csv.

If this does not help you then please edit your answer and provide a sample of the file you try to upload and the R code that you wrote to upload the file.

CodePudding user response:

if you want to remove the apostrophes from within R:

infile <- file('name-of-original-file.csv')
outfile <- file('apostrophes-gone.csv')

readLines(infile) |>
    (\(line_in) gsub("'", "", line_in))() |>
    (\(line_out) writeLines(line_out, outfile))()

close(infile)
close(outfile)

Then, read in the cleaned data file with the tool of your choice. I find import of package {rio} very convenient: df <- import('apostrophes-gone.csv')

  •  Tags:  
  • r
  • Related