索引 - 在 R 中使用 dplyr 进行匹配

Index - Match using dplyr in R

这是我的数据:

datex <- c(rep("2021-07-01", 9), rep("2021-07-02", 9))
hourx <- rep(rep(0:2),6)
seller <- c("Se1","Se1","Se1","Se2","Se2","Se2","Se2","Se2","Se2","Se1","Se1","Se1","Se2","Se2","Se2","Se2","Se2","Se2")
product <- c("Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1","Pro1")
detail <- c("De1","De1","De1","De1","De1","De1","De1","De1","De1","De1","De1","De1","De1","De1","De1","De1","De1","De1")
status <- c("St1","St1","St1","St1","St1","St1","St1","St1","St1","St1","St1","St1","St1","St1","St1","St1","St1","St1")
channel <- c("Ch1","Ch1","Ch1","Ch1","Ch1","Ch1","Ch2","Ch2","Ch2","Ch1","Ch1","Ch1","Ch1","Ch1","Ch1","Ch2","Ch2","Ch2")
transaction <- c(16,29,45,23,41,45,47,38,23,39,43,35,10,43,20,43,28,38)
mydata <- data.frame(datex, hourx, seller, product, detail, status, channel, transaction)

我想为这个 mydata.result table 填充 hourx,它包含 NA 值。

datex <- c(rep("2021-07-01", 2), rep("2021-07-02", 2))
hourx <- c(NA, NA, NA, NA)
seller <- c("Se1","Se2","Se1","Se2")
product <- c("Pro1","Pro1","Pro1","Pro1")
detail <- c("De1","De1","De1","De1")
status <- c("St1","St1","St1","St1")
channel <- c("Ch1","Ch1","Ch1","Ch2")
transaction <- c(16,41,35,28)
mydata.result <- data.frame(datex, hourx, seller, product, detail, status, channel, transaction)

如何将“mydata”中的 hourx 填入“mydata.result”?这就像 Excel 中的索引匹配,但我在 R 中需要它。谢谢。

如果我明白了,你想加入data.frames

mydata.result %>% 
  #remove hourx from mydata.result  
  select(-hourx) %>% 
  #join with mydata  
  left_join(mydata)

-输出

Joining,
 by = c("datex", "seller", "product", "detail", "status", "channel", "transaction")


       datex seller product detail status channel transaction hourx
1 2021-07-01    Se1    Pro1    De1    St1     Ch1          16     0
2 2021-07-01    Se2    Pro1    De1    St1     Ch1          41     1
3 2021-07-02    Se1    Pro1    De1    St1     Ch1          35     2
4 2021-07-02    Se2    Pro1    De1    St1     Ch2          28     1