'How can I read values from a text file with values that have an apostrophe separating the thousands?
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?
Solution 1:[1]
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.
Solution 2:[2]
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')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | guzbrush |
| Solution 2 | I_O |
