Omry Atia
Omry Atia

Reputation: 2443

Re-cast column types to a data frame which has already been read

I have a data frame df1 (with many columns) which I want to join with another data frame df2 that is supposed to have the same column types. However, for some reason when written and re-read they have acquired different types.

When I want to join these data frames, due to some of the columns which do not have the same type (but should have had), it refuses to join.

How can I force R to re-cast the classes of df2 to those of df1?

For example:

df1 <- data.frame(x = c(NA, NA, "3", "3"), y = c(NA, NA, "a", "b"))
df1_class <- sapply(df1, class) #first, determine the different classes of df1
df2 <- data.frame(x = c(NA, NA, 3, 3), y = c(NA, NA, "a", "b")) # df2 is 
# equal to df1 but has a different class in column x

# now cast column x of df2 as class "character" - but do this for all 
# columns together because there are many columns....

Upvotes: 0

Views: 168

Answers (2)

markus
markus

Reputation: 26343

You could change the ?mode of each column using "mode<-" via Map.

df2[] <- Map(f = "mode<-", x = df2, value = df1_class)
df2
# A tibble: 4 x 3
#  x     y         z
#  <chr> <chr> <dbl>
#1 NA    NA        2
#2 NA    NA        2
#3 3     a         2
#4 3     b         2

Your data extended by a third column for illustration.

data

library(tibble)
df1 <- data_frame(x = c(NA, NA, "3", "3"), y = c(NA, NA, "a", "b"), z = 1)
df2 <- data_frame(x = c(NA, NA, 3, 3), y = c(NA, NA, "a", "b"), z = 2L)
(df1_class <- sapply(df1, class))
#          x           y           z 
#"character" "character"   "numeric"

Upvotes: 1

Augusto Fadel
Augusto Fadel

Reputation: 319

Using the purrrpackage the following will update df2 to match df1 classes:

df1_class <- sapply(df1, class)
df2 <- 
  purrr::map2_df(
  df2,
  df1_class,
  ~ do.call(paste0('as.', .y), list(.x))
)

Upvotes: 1

Related Questions