我有具有相同列名的不同列。
在第一个数据中;每列有16行。
longdata<-structure(list(X2012 = c(0, 0.4, 0.8, 0.2, 0, 0, 0.2, 1.8, 0.8,
3.6, 0.2, 0, 0.8, 4, 5.6, 4.2), X2013 = c(0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), X2014 = c(0.4, 0.6,
0.6, 0, 0, 0, 0.4, 0.2, 0.2, 0, 0.2, 0, 0, 0.2, 0, 0)), .Names = c("X2012",
"X2013", "X2014"), class = "data.frame", row.names = c(NA, -16L
))
在第二个数据中,每列有6行。
shortdata<-structure(list(X2012 = c(0.2, 0.4, 0.6, 0.2, 0.2, 0.2), X2013 = c(0,
0.2, 0, 0, 0.2, 0), X2014 = c(0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("X2012",
"X2013", "X2014"), class = "data.frame", row.names = c(NA, -6L
))
这就是我想做的;
finaldata<-structure(list(X2012 = c(0, 0.4, 0.8, 0.2, 0, 0, 0.2, 1.8, 0.8,
3.6, 0.2, 0, 0.8, 4, 5.6, 4.2), X2012.1 = c(0.2, 0.4, 0.6, 0.2,
0.2, 0.2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), X2013 = c(0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
X2013.1 = c(0, 0.2, 0, 0, 0.2, 0, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA), X2014 = c(0.4, 0.6, 0.6, 0, 0, 0, 0.4, 0.2,
0.2, 0, 0.2, 0, 0, 0.2, 0, 0), X2014.1 = c(0L, 0L, 0L, 0L,
0L, 0L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("X2012",
"X2012.1", "X2013", "X2013.1", "X2014", "X2014.1"), class = "data.frame", row.names = c(NA,
-16L))
答案 0 :(得分:4)
一种dplyr
可能是:
longdata %>%
rowid_to_column() %>%
left_join(shortdata %>%
rowid_to_column(), by = c("rowid" = "rowid"), suffix = c("", ".1")) %>%
select(-rowid)
此处根据行ID执行左连接。
X2012 X2013 X2014 X2012.1 X2013.1 X2014.1
1 0.0 0 0.4 0.2 0.0 0
2 0.4 0 0.6 0.4 0.2 0
3 0.8 0 0.6 0.6 0.0 0
4 0.2 0 0.0 0.2 0.0 0
5 0.0 0 0.0 0.2 0.2 0
6 0.0 0 0.0 0.2 0.0 0
7 0.2 0 0.4 NA NA NA
8 1.8 0 0.2 NA NA NA
9 0.8 0 0.2 NA NA NA
10 3.6 0 0.0 NA NA NA
11 0.2 0 0.2 NA NA NA
12 0.0 0 0.0 NA NA NA
13 0.8 0 0.0 NA NA NA
14 4.0 0 0.2 NA NA NA
15 5.6 0 0.0 NA NA NA
16 4.2 0 0.0 NA NA NA
或与base R
相同的想法:
longdata$rowid <- seq_along(longdata$X2012)
shortdata$rowid <- seq_along(shortdata$X2012)
merge(longdata, shortdata, by = "rowid", all = TRUE, suffixes = c("", ".1"))[, -1]
或更简化(由@Ronak Shah提出):
merge(longdata, shortdata, by = "row.names", all = TRUE, suffixes = c("", ".1"))[, -1]
答案 1 :(得分:1)
我们可以使用rowr::cbind.fill
,使用make.unique
和order
来使名称唯一。
df1 <- rowr::cbind.fill(longdata, shortdata, fill = NA)
names(df1) <- make.unique(names(df1))
df1[order(names(df1))]
# X2012 X2012.1 X2013 X2013.1 X2014 X2014.1
#1 0.0 0.2 0 0.0 0.4 0
#2 0.4 0.4 0 0.2 0.6 0
#3 0.8 0.6 0 0.0 0.6 0
#4 0.2 0.2 0 0.0 0.0 0
#5 0.0 0.2 0 0.2 0.0 0
#6 0.0 0.2 0 0.0 0.0 0
#7 0.2 NA 0 NA 0.4 NA
#8 1.8 NA 0 NA 0.2 NA
#9 0.8 NA 0 NA 0.2 NA
#10 3.6 NA 0 NA 0.0 NA
#11 0.2 NA 0 NA 0.2 NA
#12 0.0 NA 0 NA 0.0 NA
#13 0.8 NA 0 NA 0.0 NA
#14 4.0 NA 0 NA 0.2 NA
#15 5.6 NA 0 NA 0.0 NA
#16 4.2 NA 0 NA 0.0 NA
在基数R中,您可以使用match
行号和cbind
cbind(longdata,shortdata[match(seq_len(nrow(longdata)), seq_len(nrow(shortdata))),])
在这种情况下,您可能还需要使用make.unique
来使名称唯一。