我正在尝试使用inner_join
来执行SQL内部联接
在R中的两个数据帧
我的数据框的一列是数据类型chron
。
不幸的是,inner_join
的结果
将列的数据类型更改为num
。
为什么会这样?
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。