ฉันมีสองdata.frame
s มีคอลัมน์ร่วมกันหลาย ๆ (ที่นี่: date
, city
, ctry
และ ( other_
) number
)
ตอนนี้ฉันต้องการรวมไว้ในคอลัมน์ด้านบน แต่ยอมรับระดับความแตกต่าง:
threshold.numbers <- 3
threshold.date <- 5 # in days
หากความแตกต่างระหว่างdate
รายการคือ> threshold.date
( เป็นวัน) หรือ > threshold.numbers
ฉันไม่ต้องการรวมบรรทัด ในทำนองเดียวกันถ้ารายการในcity
เป็นสตริงย่อยของรายการอื่นdf
ในcity
คอลัมน์ฉันต้องการให้บรรทัดถูกรวมเข้าด้วยกัน [ถ้าใครมีความคิดที่ดีในการทดสอบสำหรับชื่อเมืองที่เกิดขึ้นจริงคล้ายคลึงกันผมยินดีที่จะได้ยินเกี่ยวกับมัน.] (และให้คนแรกdf
ของรายการของdate
, city
และcountry
แต่ทั้งสอง ( other_
) number
คอลัมน์และคอลัมน์อื่น ๆ df
ทั้งหมดใน
ลองพิจารณาตัวอย่างต่อไปนี้:
df1 <- data.frame(date = c("2003-08-29", "1999-06-12", "2000-08-29", "1999-02-24", "2001-04-17",
"1999-06-30", "1999-03-16", "1999-07-16", "2001-08-29", "2002-07-30"),
city = c("Berlin", "Paris", "London", "Rome", "Bern",
"Copenhagen", "Warsaw", "Moscow", "Tunis", "Vienna"),
ctry = c("Germany", "France", "UK", "Italy", "Switzerland",
"Denmark", "Poland", "Russia", "Tunisia", "Austria"),
number = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100),
col = c("apple", "banana", "pear", "banana", "lemon", "cucumber", "apple", "peach", "cherry", "cherry"))
df2 <- data.frame(date = c("2003-08-29", "1999-06-12", "2000-08-29", "1999-02-24", "2001-04-17", # all identical to df1
"1999-06-29", "1999-03-14", "1999-07-17", # all 1-2 days different
"2000-01-29", "2002-07-01"), # all very different (> 2 weeks)
city = c("Berlin", "East-Paris", "near London", "Rome", # same or slight differences
"Zurich", # completely different
"Copenhagen", "Warsaw", "Moscow", "Tunis", "Vienna"), # same
ctry = c("Germany", "France", "UK", "Italy", "Switzerland", # all the same
"Denmark", "Poland", "Russia", "Tunisia", "Austria"),
other_number = c(13, 17, 3100, 45, 51, 61, 780, 85, 90, 101), # slightly different to very different
other_col = c("yellow", "green", "blue", "red", "purple", "orange", "blue", "red", "black", "beige"))
ตอนนี้ฉันต้องการผสานdata.frames
และรับdf
บรรทัดที่รวมกันหากตรงตามเงื่อนไขข้างต้น
(คอลัมน์แรกมีไว้เพื่อความสะดวกของคุณเท่านั้น: หลังตัวเลขตัวแรกซึ่งแสดงถึงตัวพิมพ์ใหญ่มันจะแสดงว่าบรรทัดที่ผสาน ( .
) หรือแสดงว่ามาจากdf1
( 1
) หรือdf2
( 2
)
date city ctry number other_col other_number other_col2 #comment
1. 2003-08-29 Berlin Germany 10 apple 13 yellow # matched on date, city, number
2. 1999-06-12 Paris France 20 banana 17 green # matched on date, city similar, number - other_number == threshold.numbers
31 2000-08-29 London UK 30 pear <NA> <NA> # not matched: number - other_number > threshold.numbers
32 2000-08-29 near London UK <NA> <NA> 3100 blue #
41 1999-02-24 Rome Italy 40 banana <NA> <NA> # not matched: number - other_number > threshold.numbers
42 1999-02-24 Rome Italy <NA> <NA> 45 red #
51 2001-04-17 Bern Switzerland 50 lemon <NA> <NA> # not matched: cities different (dates okay, numbers okay)
52 2001-04-17 Zurich Switzerland <NA> <NA> 51 purple #
6. 1999-06-30 Copenhagen Denmark 60 cucumber 61 orange # matched: date difference < threshold.date (cities okay, dates okay)
71 1999-03-16 Warsaw Poland 70 apple <NA> <NA> # not matched: number - other_number > threshold.numbers (dates okay)
72 1999-03-14 Warsaw Poland <NA> <NA> 780 blue #
81 1999-07-16 Moscow Russia 80 peach <NA> <NA> # not matched: number - other_number > threshold.numbers (dates okay)
82 1999-07-17 Moscow Russia <NA> <NA> 85 red #
91 2001-08-29 Tunis Tunisia 90 cherry <NA> <NA> # not matched: date difference < threshold.date (cities okay, dates okay)
92 2000-01-29 Tunis Tunisia <NA> <NA> 90 black #
101 2002-07-30 Vienna Austria 100 cherry <NA> <NA> # not matched: date difference < threshold.date (cities okay, dates okay)
102 2002-07-01 Vienna Austria <NA> <NA> 101 beige #
ฉันลองการใช้งานที่แตกต่างกันของการผสานพวกเขา แต่ไม่สามารถนำเกณฑ์ไปใช้
แก้ไข ขอโทษสำหรับสูตรที่ไม่ชัดเจน - ฉันต้องการเก็บแถวทั้งหมดและรับตัวบ่งชี้ว่าแถวนั้นตรงกันตรงกันหรือไม่และจาก df1 หรือไม่ตรงกันและจาก df2
รหัสหลอกคือ:
if there is a case where abs("date_df2" - "date_df1") <= threshold.date:
if "ctry_df2" == "ctry_df1":
if "city_df2" ~ "city_df1":
if abs("number_df2" - "number_df1") <= threshold.numbers:
merge and go to next row in df2
else:
add row to df1```
.
?