这是我努力重建昨天出现的问题,我早上大部分时间都在努力解决这个问题但却无法再找到问题。 2个数据集df1和一个较小的df2提供了相同的列名,请求仅替换date
列匹配的行中的NA值。我想合并本可以做到并且可能不那么繁重,但我正在寻求match()
和索引策略并最终找到一个:
df1 <- structure(list(date = c(20040101L, 20040115L, 20040131L, 20040205L,
20040228L, 20040301L, 20040315L, 20040331L), X11A = c(100L, 200L,
NA, NA, NA, 150L, NA, NA), X11A.1 = c(150L, NA, 165L, NA, NA,
155L, NA, NA), X21B = c(NA, 200L, 180L, NA, NA, 170L, 180L, NA
), X3CC = c(NA, NA, 190L, NA, NA, 150L, 190L, 175L), X3CC.1 = c(140L,
NA, 190L, NA, NA, 160L, 200L, 180L)), .Names = c("date", "X11A",
"X11A.1", "X21B", "X3CC", "X3CC.1"), class = "data.frame", row.names = c(NA,
-8L))
df2 <- structure(list(date = c(20040228L, 20040131L, 20040331L), X11A = c(140L,
170L, NA), X11A.1 = c(145L, NA, 145L), X21B = c(165L, NA, 160L
), X3CC = c(150L, NA, NA), X3CC.1 = c(155L, NA, NA)), .Names = c("date",
"X11A", "X11A.1", "X21B", "X3CC", "X3CC.1"), class = "data.frame", row.names = c(NA,
-3L))
实际提供的内容:
DF1:
date 11A 11A 21B 3CC 3CC
20040101 100 150 NA NA 140
20040115 200 NA 200 NA NA
20040131 NA 165 180 190 190
20040205 NA NA NA NA NA
20040228 NA NA NA NA NA
20040301 150 155 170 150 160
20040315 NA NA 180 190 200
20040331 NA NA NA 175 180
DF2:
date 11A 11A 21B 3CC 3CC
20040228 140 145 165 150 155
20040131 170 NA NA NA NA
20040331 NA 145 160 NA NA
答案 0 :(得分:4)
is.na
功能可以创建一个&#34;模板&#34;来自dataframe参数的逻辑。我的目标是创建这样的模板,然后仅选择两个match
列之间具有date
结果的行。使用which
和arr.ind = TRUE会给出一个两列矩阵,可以用作[<-
或[
的单个参数:
valpos <- which(is.na(df1)[match(df2$date, df1$date), ], arr.ind=TRUE)
接下来的任务是转换第一列(名为&#34; row&#34;),以便用正确的行代替&#34; target&#34;数据帧:
targpos <- cbind( match(df2$date, df1$date)[ valpos[,'row'] ] ,
valpos[,'col'])
然后它只是:
> df1[targpos] <- df2[valpos]
> df1
date X11A X11A.1 X21B X3CC X3CC.1
1 20040101 100 150 NA NA 140
2 20040115 200 NA 200 NA NA
3 20040131 170 165 180 190 190
4 20040205 NA NA NA NA NA
5 20040228 140 145 165 150 155
6 20040301 150 155 170 150 160
7 20040315 NA NA 180 190 200
8 20040331 NA 145 160 175 180
当我把订单拖到日期时,我确实让问题变得更加困难。我认为这种逻辑也很难解决这个问题。
答案 1 :(得分:0)
以下解决方案根据df2
列预先计算(1)从df1
到date
的行映射,以及(2)两个data.frame之间的公共数据列名称。然后迭代通用列,并为每个列测试df1
列中的哪些单元都映射到df2
并且值为NA,然后从可用的任何值中分配这些单元格。 df2
。
优点:
df1
的{{1}}单元进行NA测试,并仅分配NA单元。df2
具有异构列类型,则此操作不会破坏这些类型。df1
rms <- match(df2$date,df1$date);
cms <- intersect(names(df1)[-1L],names(df2)[-1L]);
for (cm in cms) { n <- is.na(df1[[cm]][rms]); df1[[cm]][rms][n] <- df2[[cm]][n]; };
df1;
## date X11A X11A.1 X21B X3CC X3CC.1
## 1 20040101 100 150 NA NA 140
## 2 20040115 200 NA 200 NA NA
## 3 20040131 170 165 180 190 190
## 4 20040205 NA NA NA NA NA
## 5 20040228 140 145 165 150 155
## 6 20040301 150 155 170 150 160
## 7 20040315 NA NA 180 190 200
## 8 20040331 NA 145 160 175 180
library(microbenchmark);
`42` <- function(df1,df2) { valpos <- which(is.na(df1)[match(df2$date,df1$date),],arr.ind=TRUE); targpos <- cbind(match(df2$date,df1$date)[valpos[,'row']],valpos[,'col']); df1[targpos] <- df2[valpos]; df1; };
bgoldst <- function(df1,df2) { rms <- match(df2$date,df1$date); cms <- intersect(names(df1)[-1L],names(df2)[-1L]); for (cm in cms) { n <- is.na(df1[[cm]][rms]); df1[[cm]][rms][n] <- df2[[cm]][n]; }; df1; };
identical(`42`(df1,df2),bgoldst(df1,df2));
## [1] TRUE
microbenchmark(`42`(df1,df2),bgoldst(df1,df2));
## Unit: microseconds
## expr min lq mean median uq max neval
## `42`(df1, df2) 297.219 309.1935 340.1425 319.0295 333.9975 1236.771 100
## bgoldst(df1, df2) 175.766 181.7530 192.9317 188.1670 198.2180 316.463 100