Have a dataframe which I want to export to CSV and re-import to dataframe. When importing one column is corrupted -- by removing the colon from the end of the strings, and interpreting them as numeric.
Here a minimal example:
df <- data.frame(integers = c(1:8, NA, 10L),
doubles = as.numeric(paste0(c(1:7, NA, 9, 10), ".1")),
strings = paste0(c(1:10),".")
)
df
str(df) # here the last column is "chr"
write.table(df,
file = "df.csv",
sep = "\t",
na = "NA",
row.names = FALSE,
col.names = TRUE,
fileEncoding = "UTF-8",
)
df <- read.table(file = "df.csv",
header = TRUE,
sep = "\t",
na.strings = "NA",
quote="\"",
fileEncoding = "UTF-8"
)
df
str(df) # here the last column is "num"