Patrick Giraudoux
2025-Sep-14 16:38 UTC
[R] trouble with exporting a data.frame with " (quotation mark) in some columns into a tab delimited file, then importing the file
Dear listers,
I encountered an issue with a CSV file that was imported correctly but
could not be re-imported correctly after being written with R. This is
probably because geographical coordinates were imported? as character?in
degrees, minutes and seconds (DMS), which includes " (quotation mark)
for the seconds.
Below a reproducible example:
db <- structure(list(lon = c(6.228561, 6.22532, 6.2260499999999999,
6.2267789999999996, 6.2224659999999998, 6.2209430000000001), latdms =
c("47?12'28.36\"N", "47?12'33.46\"N",
"47?12'28.37\"N",
"47?12'27.48\"N", "47?12'31.31\"N",
"47?12'33.15\"N"), londms =
c("6?13'42.82\"E", "6?13'31.15\"E",
"6?13'33.78\"E", "6?13'36.40\"E",
"6?13'20.88\"E", "6?13'15.39\"E"), fusutmn
= c(32L, 32L, 32L, 32L, 32L,
32L)), row.names = c(NA, 6L), class = "data.frame") > db lon latdms
londms fusutmn 1 6.228561 47?12'28.36"N 6?13'42.82"E 32 2
6.225320
47?12'33.46"N 6?13'31.15"E 32 3 6.226050
47?12'28.37"N 6?13'33.78"E 32 4
6.226779 47?12'27.48"N 6?13'36.40"E 32 5 6.222466
47?12'31.31"N
6?13'20.88"E 32 6 6.220943 47?12'33.15"N 6?13'15.39"E
32
write.table(db, file = "db.txt", row.names = FALSE, quote = FALSE, sep
=
"\t") db_import<-read.delim("db.txt") > db_import lon
latdms londms
fusutmn 1 6.228561 47?12'28.36N\t6?13'42.82E 32 NA 2 6.225320
47?12'33.46N\t6?13'31.15E 32 NA 3 6.226050
47?12'28.37N\t6?13'33.78E 32
NA 4 6.226779 47?12'27.48N\t6?13'36.40E 32 NA 5 6.222466
47?12'31.31N\t6?13'20.88E 32 NA 6 6.220943
47?12'33.15N\t6?13'15.39E 32 NA
As you can see it, latdms and londms are now collapsed and all the
columns on the right? have shifted to the left. I get the same issue
with ; as a separator.
I could not find a workaround...
Any hint appreciated,
Patrick
[[alternative HTML version deleted]]