将列类型重铸到已经读取的数据帧中

时间:2018-09-23 09:44:58

标签: r dataframe casting

我有一个数据框df1(有很多列),我想与另一个应该具有相同列类型的数据框df2联接。但是,由于某种原因,在编写和重新阅读它们时,它们获得了不同的类型。

当我想加入这些数据框时,由于某些类型不相同(但应该有)的列,它拒绝加入。

如何强制R将df2的类重新转换为df1的类?

例如:

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....

2 个答案:

答案 0 :(得分:1)

您可以通过?mode使用"mode<-"来更改每一列的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

您的数据在第三列进行了扩展。

数据

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"

答案 1 :(得分:1)

使用purrr软件包,以下命令将更新df2以匹配df1类:

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