gpt4 book ai didi

R - 合并两个数据帧 - sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list' 中出现错误

转载 作者:行者123 更新时间:2023-12-04 06:19:28 33 4
gpt4 key购买 nike

我正在尝试按每个数据帧中设置的 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")

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

最佳答案

df2$key_data 的类别与 df1

不同
#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)

编辑:

“AEST”和“GMT”中的日期与两个 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

关于R - 合并两个数据帧 - sort.list(bx[m$xi]) : 'x' must be atomic for 'sort.list' 中出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46943684/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com