调用排列后合并两个数据帧时出错

时间:2014-05-10 00:39:29

标签: r merge dataframe

我有两个数据框叫做#34; a1"和" a2"我通过read.csv引入了一个名为" time"的列。在每个我使用strptime将类设置为POSIXlt。

在尝试合并数据框之前,我调用arrange(a1, time)arrange(a2, time)来获取最旧到最新的订单。然后我试着打电话给ahu1 <- merge(a1, a2)开始把它们放在一起并得到以下错误:

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

调用str(a1)str(a2)验证两者实际上都是数据框,并且它们具有相同的观察总数。我错过了什么或做错了什么?

structure(list(time = structure(list(sec = c(0, 0, 0, 0, 0, 0), min = c(0L, 15L, 30L, 45L, 0L, 15L), hour = c(10L, 10L, 10L, 10L, 11L, 11L), mday = c(24L, 24L, 24L, 24L, 24L, 24L), mon = c(1L, 
1L, 1L, 1L, 1L, 1L), year = c(114L, 114L, 114L, 114L, 114L, 114L), wday = c(1L, 1L, 1L, 1L, 1L, 1L), yday = c(54L, 54L, 54L, 54L, 54L, 54L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L), zone = c("MST", 
"MST", "MST", "MST", "MST", "MST"), gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec", "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), status.sf = c("TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE"), status.rf = c("TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE"), cfm.oa = c(1397.6, 343, 1265, 1500, 744.5, 1261.6), cfm.ra = c("2364.8", "3067.4", "2073.9", "2727.4", "2561.2", "2032.4"), temp.ra = c(68.6, 67.6, 69.1, 69.7, 67.9, 69), temp.ma = c(65.6, 65.8, 66.8, 67.9, 66.5, 68.2), temp.sa = c(51.1, 66.4, 68.6, 51.2, 67.3, 68), ductstatic = c(0.25, 0.25, 0.25, 0.47, 0.27, 0.25), fault.hum = c("FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE"), fltpress = c(0.15, 0.12, 0.12, 0.22, 0.13, 0.13), fault.smoke = c("FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE")), .Names = c("time", "status.sf", "status.rf", "cfm.oa", "cfm.ra", "temp.ra", "temp.ma", "temp.sa", 
"ductstatic", "fault.hum", "fltpress", "fault.smoke"), row.names = c(NA, 6L), class = "data.frame")



structure(list(time = structure(list(sec = c(0, 0, 0, 0, 0, 0), min = c(0L, 15L, 30L, 45L, 0L, 15L), hour = c(10L, 10L, 10L, 10L, 11L, 11L), mday = c(24L, 24L, 24L, 24L, 24L, 24L), mon = c(1L, 1L, 1L, 1L, 1L, 1L), year = c(114L, 114L, 114L, 114L, 114L, 114L), wday = c(1L, 1L, 1L, 1L, 1L, 1L), yday = c(54L, 54L, 54L, 54L, 54L, 54L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L), zone = c("MST", "MST", "MST", "MST", "MST", "MST"), gmtoff = c(NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec", "min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), occpt = c("TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE"), max.dmprpos = c(77, 63, 71, 93, 63, 71), mode.ecm = c("TRUE", "TRUE", "TRUE", "TRUE", "TRUE", "TRUE"), mode.dehum = c("TRUE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE"), spt.sa = c("53", "66", "66", "66", "66", "66"
), spt.ductstatic = c("0.27", "0.25", "0.25", "0.57", "0.25", "0.25"), mode.co2 = c("FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE"), mode.hum = c("FALSE", "FALSE", "FALSE", "FALSE", "FALSE", "FALSE"), max.runreqt = c("0", "0", "0", "0", "0", "0"), max.vavco2 = c("427", "429", "427", "414", "417", "406"), max.vavrhv = c("59", "56.4", "54.9", "56.1", "53.3", "50.1"), cfm.vavtot = c("2711", "2383.6", 
"2395.8", "3702.9", "2449.8", "2388.4"), spt.cfm.ra = c("2461", "2133", "2144.3", "3433.7", "2198.8", "2139.3")), .Names = c("time", "occpt", "max.dmprpos", "mode.ecm", "mode.dehum", "spt.sa", "spt.ductstatic", "mode.co2", "mode.hum", "max.runreqt", "max.vavco2", "max.vavrhv", "cfm.vavtot", "spt.cfm.ra"), row.names = c(NA, 6L), class = "data.frame")

1 个答案:

答案 0 :(得分:0)

目前,两个数据对象具有完全相同的数量,并且日期时间相同。因此,使用cbind()只是一个简单的步骤。 time列没有匹配。

 cbind(a1, a2[-1])

                 time status.sf status.rf cfm.oa cfm.ra temp.ra temp.ma temp.sa
1 2014-02-24 10:00:00      TRUE      TRUE 1397.6 2364.8    68.6    65.6    51.1
2 2014-02-24 10:15:00      TRUE      TRUE  343.0 3067.4    67.6    65.8    66.4
3 2014-02-24 10:30:00      TRUE      TRUE 1265.0 2073.9    69.1    66.8    68.6
4 2014-02-24 10:45:00      TRUE      TRUE 1500.0 2727.4    69.7    67.9    51.2
5 2014-02-24 11:00:00      TRUE      TRUE  744.5 2561.2    67.9    66.5    67.3
6 2014-02-24 11:15:00      TRUE      TRUE 1261.6 2032.4    69.0    68.2    68.0
  ductstatic fault.hum fltpress fault.smoke occpt max.dmprpos mode.ecm
1       0.25     FALSE     0.15       FALSE  TRUE          77     TRUE
2       0.25     FALSE     0.12       FALSE  TRUE          63     TRUE
3       0.25     FALSE     0.12       FALSE  TRUE          71     TRUE
4       0.47     FALSE     0.22       FALSE  TRUE          93     TRUE
5       0.27     FALSE     0.13       FALSE  TRUE          63     TRUE
6       0.25     FALSE     0.13       FALSE  TRUE          71     TRUE
  mode.dehum spt.sa spt.ductstatic mode.co2 mode.hum max.runreqt max.vavco2
1       TRUE     53           0.27    FALSE    FALSE           0        427
2      FALSE     66           0.25    FALSE    FALSE           0        429
3      FALSE     66           0.25    FALSE    FALSE           0        427
4      FALSE     66           0.57    FALSE    FALSE           0        414
5      FALSE     66           0.25    FALSE    FALSE           0        417
6      FALSE     66           0.25    FALSE    FALSE           0        406
  max.vavrhv cfm.vavtot spt.cfm.ra
1         59       2711       2461
2       56.4     2383.6       2133
3       54.9     2395.8     2144.3
4       56.1     3702.9     3433.7
5       53.3     2449.8     2198.8
6       50.1     2388.4     2139.3