简体   繁体   English

在 R 中使用合并 function 时,行数加倍

[英]When using merge function in R the number of rows doubles

I am trying to merge two datasets using this function:我正在尝试使用此 function 合并两个数据集:

USA<-merge(USAdata,dow,by="Date",all=T)美国<-merge(USAdata,dow,by="Date",all=T)

When running it I run into the problem that the merge happens but the number of rows double, I've already successfully used this function to merge other data sets without any problems, and always by="Date".运行它时,我遇到了合并发生但行数加倍的问题,我已经成功地使用这个 function 来合并其他数据集,没有任何问题,并且总是按“日期”。

Can someone help please?有人可以帮忙吗?

Here are the datasets: https://drive.google.com/drive/folders/1KoG85vzZQ1R_C3mjXyRHyJJb4Cy6krKj?usp=sharing以下是数据集: https://drive.google.com/drive/folders/1KoG85vzZQ1R_C3mjXyRHyJJb4Cy6krKj?usp=sharing

PS I'm a total beginner PS我是一个完全的初学者

You have Date class as character and in different format in dow .您有Date class 作为字符,并且在dow中有不同的格式。 Change it to date class and then merge the data.将其更改为日期 class 然后merge数据。

dow$Date <- as.Date(dow$Date, '%m/%d/%Y')
USA <- merge(USAdata, dow, by="Date", all=TRUE)

We can use tidyverse我们可以使用tidyverse

library(dplyr)
library(lubridate)
USAdata %>%
      full_join(dow %>% mutate(Date = mdy(Date)), by = 'Date')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM