preserve numerical class when exporting a data frame with write.table() in R

455 views Asked by At

I have two data frames I merged:

df1 <- read_csv("df1.csv", col_types = cols(Date = col_date(format = "%d/%m/%Y")))
df2 <- read_csv("df2.csv", col_types = cols(Date = col_date(format = "%d/%m/%Y")))
df3 <-left_join(df1, df2, by = 'Date', df1 = TRUE)

write.table(df3, file = "df3.csv", sep = "," )

df3> Q3

before I export the df3, all my columns are numerical but after exporting they are transformed into logical so I do the following:

df3 <- read_csv("df3.csv", col_types = cols(Date = col_date(format = "%d/%m/%Y")))

cols <- c(sapply(df3, is.logical))
Q[cols] <- lapply(df3[cols], as.numeric)

But once I export it one more time, the data frame goes back to logical and that affects my following functions.

My doubt is:

  1. How can I preserve the numerical class of my columns when exporting a data frame.
0

There are 0 answers