R - 合并两个数据帧 - sort.list中的错误(bx [m $ xi]):' x'对于' sort.list'必须是原子的

时间:2017-10-25 23:45:01

标签: r dataframe

我正在尝试通过在每个数据集中设置的key_date合并到数据框:

> merge <- merge(x = df1, y = df2, by = "key_date", all.x = TRUE)

错误:

Error in sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?

df1的结构:

'data.frame':   192 obs. of  8 variables:
 $ date    : POSIXct, format: "2017-02-03 00:00:00" ...
 $ NO2     : num  7.4 6.7 7 6.1 5.8 8.8 9 8.5 8.1 7.6 ...
 $ key_date: POSIXlt, format: "2017-02-03" ...
 $ site    : chr  "anon1" "anon1" "anon1" "anon1" ...
 $ code    : chr  "anon1" "anon1" "anon1" "anon1" ...

df2的结构:

'data.frame':   2 obs. of  6 variables:
 $ airport : chr  "LCY" "LCY"
 $ key_date: chr  "2017-02-04" "2017-02-03"
 $ ws      : num  4 13
 $ wd      : num  161 185
 $ tempi   : num  7 8
 $ humidity: num  85 78

> dput(df2)
structure(list(airport = c("LCY", "LCY"), key_date = c("2017-02-04", 
"2017-02-03"), ws = c(4, 13), wd = c(161, 185), tempi = c(7, 
8), humidity = c(85, 78)), .Names = c("airport", "key_date", 
"ws", "wd", "tempi", "humidity"), row.names = c(NA, -2L), class = "data.frame")

出了什么问题?有什么想法吗?

2 个答案:

答案 0 :(得分:3)

df2$key_datadf1

的类不同
#df2$key_date <- as.Date(df2$key_date, "%Y-%M-%d")
df2$key_date <- as.POSIXct(df2$key_date)
mdf <- merge(x = df1, y = df2, by = "key_date", all.x = TRUE)

编辑:

&#34; AEST&#34;和&#34; GMT&#34;与两个df的时间。

df2$key_date <- as.POSIXct(df2$key_date)

df2$key_date
# [1] "2017-02-04 AEST" "2017-02-03 AEST"

df1$key_date
#[1] "2017-02-03 GMT" "2017-02-03 GMT" "2017-02-03 GMT" "2017-02-03 GMT"
#[5] "2017-02-03 GMT" "2017-02-03 GMT"

df2$key_date <- as.character(df2$key_date)
df1$key_date <- as.character(df1$key_date)
merge(x = df1, y = df2, by = "key_date", all.x = TRUE)

# key_date                date NO2 PM10_1 PM10_2 PM10_3  site  code
# 1 2017-02-03 2017-02-03 10:00:00 7.4   18.3     NA     NA anon1 anon1
# 2 2017-02-03 2017-02-03 10:15:00 6.7     NA   27.5     NA anon1 anon1
# 3 2017-02-03 2017-02-03 10:30:00 7.0     NA   27.9     NA anon1 anon1
# 4 2017-02-03 2017-02-03 10:45:00 6.1     NA   28.9     NA anon1 anon1
# 5 2017-02-03 2017-02-03 11:00:00 5.8   21.1   28.2     NA anon1 anon1
# 6 2017-02-03 2017-02-03 11:15:00 8.8     NA   28.1     NA anon1 anon1
# airport ws  wd tempi humidity
# 1     LCY 13 185     8       78
# 2     LCY 13 185     8       78
# 3     LCY 13 185     8       78
# 4     LCY 13 185     8       78
# 5     LCY 13 185     8       78
# 6     LCY 13 185     8       78

答案 1 :(得分:2)

我认为这是因为两个数据框中关键日期的格式不一样,一个是字符,另一个是POSIXct 将它们转换为相同的格式,然后重试。