为什么 dplyr 中的 inner_join 会更改列的数据类型?
Why does inner_join from dplyr change the data type of column?
我正在尝试使用 inner_join
执行 SQL 内部联接
在 R 中的两个数据帧上。
我的数据框的其中一列是数据类型 chron
。
不幸的是, inner_join
的结果
将列的数据类型更改为 num
。
为什么会这样?
MWE
require(chron)
require(dplyr)
df1 <- data.frame(name = c('Alice', 'Bob'),
id = 1:2)
print(str(df1))
# 'data.frame': 2 obs. of 2 variables:
# $ name: Factor w/ 2 levels "Alice","Bob": 1 2
# $ id : int 1 2
df2 <- data.frame(id = 1:2,
birth_chron = as.chron(c('2010-01-01 00:11:22',
'2010-01-01 00:11:22')))
print(df2)
# id birth_chron
# 1 1 (01/01/10 00:11:22)
# 2 2 (01/01/10 00:11:22)
print(str(df2))
# 'data.frame': 2 obs. of 2 variables:
# $ id : int 1 2
# $ birth_chron:Classes 'chron', 'dates', 'times' atomic [1:2] 14610 14610
# .. ..- attr(*, "format")= Named chr [1:2] "m/d/y" "h:m:s"
# .. .. ..- attr(*, "names")= chr [1:2] "dates" "times"
# .. ..- attr(*, "origin")= Named num [1:3] 1 1 1970
# .. .. ..- attr(*, "names")= chr [1:3] "month" "day" "year"
result <- inner_join(df1, df2, by = 'id')
print(str(result))
# 'data.frame': 2 obs. of 3 variables:
# $ name : Factor w/ 2 levels "Alice","Bob": 1 2
# $ id : int 1 2
# $ birth_chron: num 14610 14610
print(result)
# name id birth_chron
# 1 Alice 1 14610.01
# 2 Bob 2 14610.01
回复评论
我正在使用 dplyr 版本 0.4.1.
基于 this github 问题,我推测他们只是还没有建立对 chron 列的支持,至少现在还没有。
我正在尝试使用 inner_join
执行 SQL 内部联接
在 R 中的两个数据帧上。
我的数据框的其中一列是数据类型 chron
。
不幸的是, inner_join
的结果
将列的数据类型更改为 num
。
为什么会这样?
MWE
require(chron)
require(dplyr)
df1 <- data.frame(name = c('Alice', 'Bob'),
id = 1:2)
print(str(df1))
# 'data.frame': 2 obs. of 2 variables:
# $ name: Factor w/ 2 levels "Alice","Bob": 1 2
# $ id : int 1 2
df2 <- data.frame(id = 1:2,
birth_chron = as.chron(c('2010-01-01 00:11:22',
'2010-01-01 00:11:22')))
print(df2)
# id birth_chron
# 1 1 (01/01/10 00:11:22)
# 2 2 (01/01/10 00:11:22)
print(str(df2))
# 'data.frame': 2 obs. of 2 variables:
# $ id : int 1 2
# $ birth_chron:Classes 'chron', 'dates', 'times' atomic [1:2] 14610 14610
# .. ..- attr(*, "format")= Named chr [1:2] "m/d/y" "h:m:s"
# .. .. ..- attr(*, "names")= chr [1:2] "dates" "times"
# .. ..- attr(*, "origin")= Named num [1:3] 1 1 1970
# .. .. ..- attr(*, "names")= chr [1:3] "month" "day" "year"
result <- inner_join(df1, df2, by = 'id')
print(str(result))
# 'data.frame': 2 obs. of 3 variables:
# $ name : Factor w/ 2 levels "Alice","Bob": 1 2
# $ id : int 1 2
# $ birth_chron: num 14610 14610
print(result)
# name id birth_chron
# 1 Alice 1 14610.01
# 2 Bob 2 14610.01
回复评论 我正在使用 dplyr 版本 0.4.1.
基于 this github 问题,我推测他们只是还没有建立对 chron 列的支持,至少现在还没有。