'Read file with decimals as comma not working in R cordinates?
I have received a field with XY coordinates that have a decimal as a comma, but I cannot correctly open this file in R.
Here is how my data should look like - note comma as decimal indicator in y coordinates:
"OBJECTID";"x_coord";"y_coord"
"1";"664936,3059";"5582773,2319" # comma as separator
"2";"604996,5803";"5471445,4964"
"3";"772846,82";"5353980,45"
"4";"552181,8639";"5535271,7626"
"5";"604022,9011";"5470134,0649"
But, specifying the dec = ',' in read.csv just reads it as a normal values:
xy <- read.delim(paste(path, "test_decimals2.txt", sep = '/'),
sep = '\t', dec = ",",skip = 0)
Missing comma separator from y coordinates:
OBJECTID x_coord y_coord
1 1 6649363059 55827732319 # not a comma separator anymore
2 2 6049965803 54714454964
3 3 77284682 535398045
4 4 5521818639 55352717626
5 5 6040229011 54701340649
I have tried to convert the data to txt, etc. but still have the same problem. Do someone know how to make sure that dec = ',' will work correctly? Thank you!
(coordintaes are in UTM, that's why they look a bit weird)
Solution 1:[1]
a <- read.csv2("test_decimals2.txt", dec = ",", as.is = TRUE, header = FALSE, skip = 1)
a |> mutate(X = sub(",", ".", V2), Y = sub(",", ".", V3)) |>
select(V1, X, Y)
Solution 2:[2]
I think read.csv2 is the answer for those european style csv-Data:
xy <- read.csv2(file = paste(path, "test_decimals2.txt", sep = '/'))
Your approach might also be correct. The data is correct, it just does not display enough digits in the print.
Try printing your data with:
> print.data.frame(xy, digits = 10)
OBJECTID x_coord y_coord
1 1 664936.3059 5582773.232
2 2 604996.5803 5471445.496
3 3 772846.8200 5353980.450
4 4 552181.8639 5535271.763
5 5 604022.9011 5470134.065
This prints the dataframe with a limit of ten digits.
Solution 3:[3]
Adding to the previous answers. If you want to use read.delim() function you can simply type:
xy <- read.delim(file_path, sep = ';', skip = 0, dec = ",")
OR
xy <- read.delim(file_path, sep = ';', skip = 0, colClasses = "character")
xy$x_coord <- as.numeric(gsub(",", ".", xy$x_coord))
xy$y_coord <- as.numeric(gsub(",", ".", xy$y_coord))
Then, you may change the default display use to print more digits by using:
options(digits = 12)
Than, printing the data.frame would lead to :
OBJECTID x_coord y_coord
1 1 664936.3059 5582773.2319
2 2 604996.5803 5471445.4964
3 3 772846.8200 5353980.4500
4 4 552181.8639 5535271.7626
5 5 604022.9011 5470134.0649
Solution 4:[4]
One possible approach:
testcommas <- read_delim(path,"\t", col_types=cols(.default="c"))
testcommas
# A tibble: 4 x 3
item coord_y coord_x
<chr> <chr> <chr>
1 1 156,158 543,697
2 2 324,678 169,385
3 3 097,325 325,734
4 4 400,211 158,687
With this, all columns will be of type "character". Then you may change them to numeric if/as needed. One way:
testcommas <- data.frame(sapply(testcommas, function(x) as.numeric(sub(",", ".", x, fixed = TRUE))))
testcommas
item coord_y coord_x
1 1 156.158 543.697
2 2 324.678 169.385
3 3 97.325 325.734
4 4 400.211 158.687
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 | Grzegorz Sapijaszko |
| Solution 2 | Sandwichnick |
| Solution 3 | |
| Solution 4 | Dharman |
