R语言合并DataFrame后出现重复条目问题求助
Hey there! Let's break down why you're seeing duplicate entries after merging your DataFrames, and how to fix it.
Why Duplicates Happen
From your code, I suspect two main culprits:
- Non-unique dates in
U.NO2.ab.03: Usingunique(NO2.ab.03)only removes rows where all columns are identical. IfNO2.ab.03has multiple rows with the sameDate.Localbut different values in other columns,unique()will keep all those rows. When you merge, this creates a cartesian product for each matching date, leading to duplicates. - Implicit merge key: You didn't specify the
byargument inmerge(). While R will auto-match columns with the same name (hereDate.Local), this can lead to unexpected behavior if there are hidden duplicates in either DataFrame's key column.
Step-by-Step Fix
Ensure Unique Dates in Your NO2 Data
First, cleanNO2.ab.03to make sure eachDate.Localhas only one corresponding value. Choose an aggregation method that makes sense for your data (e.g.,first(),mean(), orsum()):# Using dplyr for cleaner aggregation (install if needed: install.packages("dplyr")) library(dplyr) U.NO2.ab.03 <- NO2.ab.03 %>% group_by(Date.Local) %>% summarise(across(everything(), first)) # Replace `first()` with sum/mean if neededIf you don't want to use dplyr, base R works too:
# Keep the first occurrence of each Date.Local U.NO2.ab.03 <- NO2.ab.03[!duplicated(NO2.ab.03$Date.Local), ]Merge Explicitly with Clean Data
Now merge with a clearbyparameter, and useall.x = TRUEto keep all dates fromFiresNearLA.ab.03(your original left join intent):# Explicitly merge on Date.Local, keep all rows from FiresNearLA.ab.03 ind <- merge(FiresNearLA.ab.03, U.NO2.ab.03, by = "Date.Local", all.x = TRUE) # Replace NA values with 0 ind[is.na(ind)] <- 0Verify No Duplicates Remain
If you still see duplicates, check which dates are causing the issue:# Find duplicate dates dup_dates <- ind$Date.Local[duplicated(ind$Date.Local)] print(dup_dates) # Inspect those rows to trace the source ind[ind$Date.Local %in% dup_dates, ]This will help you see if duplicates are coming from
FiresNearLA.ab.03instead, and you can clean that DataFrame similarly.
内容的提问来源于stack exchange,提问作者JaElf

