gpt4 book ai didi

r - 合并具有不同行数和不同列的数据框

转载 作者:行者123 更新时间:2023-12-04 11:22:06 25 4
gpt4 key购买 nike

我有两个具有不同列数和行数的数据框。我想将它们组合成一个数据框。

> month.saf
Name NCDC Year Month Day HrMn Temp Q
244 AP 99999 2014 2 1 0 12 1
245 AP 99999 2014 2 1 300 12.2 1
246 AP 99999 2014 2 1 600 14.4 1
247 AP 99999 2014 2 1 900 18.6 1
248 AP 99999 2014 2 1 1200 18 1
249 AP 99999 2014 2 1 1500 13.6 1
250 AP 99999 2014 2 1 1800 11.8 1
251 AP 99999 2014 2 1 2100 10.8 1
252 AP 99999 2014 2 2 0 8.4 1
253 AP 99999 2014 2 2 300 8.6 1
254 AP 99999 2014 2 2 600 19.8 2
255 AP 99999 2014 2 2 900 22.8 1
256 AP 99999 2014 2 2 1200 20.8 1
257 AP 99999 2014 2 2 1500 16.4 1
258 AP 99999 2014 2 2 1800 13.4 1
259 AP 99999 2014 2 2 2100 12.4 1
> T2Mdf
V1 V2
0 293.494262695312 291.642639160156
300 294.003479003906 292.375091552734
600 296.809997558594 295.207885742188
900 298.287811279297 297.181549072266
1200 298.317565917969 297.725708007813
1500 298.134002685547 296.226165771484
1800 296.006805419922 293.354248046875
2100 293.785491943359 293.547210693359
0.1 294.638732910156 293.019866943359
300.1 292.179992675781 291.256958007812

我想要的输出是这样的:
    Name    NCDC    Year    Month   Day HrMn    Temp    Q   V1          V2
244 AP 99999 2014 2 1 0 12 1 293.4942627 291.6426392
245 AP 99999 2014 2 1 300 12.2 1 294.003479 292.3750916
246 AP 99999 2014 2 1 600 14.4 1 296.8099976 295.2078857
247 AP 99999 2014 2 1 900 18.6 1 298.2878113 297.1815491
248 AP 99999 2014 2 1 1200 18 1 298.3175659 297.725708
249 AP 99999 2014 2 1 1500 13.6 1 298.1340027 296.2261658
250 AP 99999 2014 2 1 1800 11.8 1 296.0068054 293.354248
251 AP 99999 2014 2 1 2100 10.8 1 293.7854919 293.5472107
252 AP 99999 2014 2 2 0 8.4 1 294.6387329 293.0198669
253 AP 99999 2014 2 2 300 8.6 1 292.1799927 291.256958
254 AP 99999 2014 2 2 600 19.8 2 292.2477417 291.3471069
255 AP 99999 2014 2 2 900 22.8 1 294.2276306 294.2766418
256 AP 99999 2014 2 2 1200 20.8 1 NA NA
257 AP 99999 2014 2 2 1500 16.4 1 NA NA
258 AP 99999 2014 2 2 1800 13.4 1 NA NA
259 AP 99999 2014 2 2 2100 12.4 1 NA NA

我试过 cbind但它给了我一个错误

Error in data.frame(..., check.names = FALSE) : arguments imply differing number of rows: 216, 220



并使用 rbind.fill()但它给了我类似的东西
V1               V2                     Name        USAF  NCDC Year Month Day HrMn  I   Type QCP Temp  Q
1 293.494262695312 291.642639160156 <NA> NA NA NA NA NA NA NA <NA> NA <NA> NA
2 294.003479003906 292.375091552734 <NA> NA NA NA NA NA NA NA <NA> NA <NA> NA
3 296.809997558594 295.207885742188 <NA> NA NA NA NA NA NA NA <NA> NA <NA> NA
4 298.287811279297 297.181549072266 <NA> NA NA NA NA NA NA NA <NA> NA <NA> NA
5 298.317565917969 297.725708007813 <NA> NA NA NA NA NA NA NA <NA> NA <NA> NA
6 <NA> <NA> AP 421820 99999 2014 2 1 0 4 FM-12 NA 12 1
7 <NA> <NA> AP 421820 99999 2014 2 1 300 4 FM-12 NA 12.2 1
8 <NA> <NA> AP 421820 99999 2014 2 1 600 4 FM-12 NA 14.4 1
9 <NA> <NA> AP 421820 99999 2014 2 1 900 4 FM-12 NA 18.6 1
10 <NA> <NA> AP 421820 99999 2014 2 1 1200 4 FM-12 NA 18 1

如何在 R 中做到这一点?

最佳答案

如果 A 和 B 是两个输入数据帧,这里有一些解决方案:

1) 合并 无论 A 或 B 是否有更多行,此解决方案都有效。

merge(data.frame(A, row.names=NULL), data.frame(B, row.names=NULL), 
by = 0, all = TRUE)[-1]

如果 A 和 B 具有默认行名,即 1、2、...,或者它们具有一致的行名,则前两个参数可以分别替换为 A 和 B。即, merge(A, B, by = 0, all = TRUE)[-1] .

例如,如果我们有这个输入:
# test inputs
A <- data.frame(BOD, row.names = letters[1:6])
B <- setNames(2 * BOD[1:2, ], c("X", "Y"))

然后:
merge(data.frame(A, row.names=NULL), data.frame(B, row.names=NULL), 
by = 0, all = TRUE)[-1]

给出:
  Time demand  X    Y
1 1 8.3 2 16.6
2 2 10.3 4 20.6
3 3 19.0 NA NA
4 4 16.0 NA NA
5 5 15.6 NA NA
6 7 19.8 NA NA

1a) 一个等效的变化是:
do.call("merge", c(lapply(list(A, B), data.frame, row.names=NULL), 
by = 0, all = TRUE))[-1]

2) cbind.zoo 此解决方案假定 A 有更多行并且 B 的条目都是相同类型的,例如都是数字。 A 不受限制。这些条件在问题的数据中成立。
library(zoo)
data.frame(A, cbind(zoo(, 1:nrow(A)), as.zoo(B)))

关于r - 合并具有不同行数和不同列的数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35202380/

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