簡體   English   中英

使用合並或匹配函數匹配R中兩個數據幀中的多個列

[英]Matching multiple columns in two dataframes in R using the merge or match function

我有兩個數據框,看起來像這樣:

Date        Shop    Item    ProductKey     Price
2014-09-01  Asda    Apple   0f-7c-32-9c65  2.00
2014-09-01  Tesco   Pear    7c-e9-a0-a11c  1.50

等等,適用於不同的日期,商店,商品,產品密鑰和價格。 數據框2具有相同的結構,但是對於下一年。

我想要做的是匹配兩個不同數據框中相同日期,商店,項目和產品密鑰的項目(稱為2014年9月和2015年9月) - 當它們匹配所有變量以創建價格相對時(即除以2015年)價格按2014年價格計算)。

我嘗試了各種if語句和匹配函數,但似乎沒有得到任何結果。 我知道必須有一個簡單的方法來做到這一點,我完全失蹤了。 任何幫助將不勝感激。 我還查看了合並函數的示例,但我不認為這對我的情況有用。 我在網站上經歷了很多關於匹配和試圖使用一些建議代碼的問題,但在我的案例中似乎沒有任何相關內容。

重新考慮merge方法:

# FIRST DATAFRAME (2014)
txt='Date        Shop    Item    ProductKey     Price
2014-09-01  Asda    Apple   0f-7c-32-9c65  2.00
2014-09-01  Tesco   Pear    7c-e9-a0-a11c  1.50'

df1 <- read.table(text=txt, header=TRUE)
df1$Date <- as.POSIXct(df1$Date)             # CONVERT TO DATE
df1$Month <- format(df1$Date, "%m")          # EXTRACT MONTH (CAN ADJUST FOR MM/DD)

# SECOND DATAFRAME (2015)
txt='Date        Shop    Item    ProductKey     Price
2015-09-01  Asda    Apple   0f-7c-32-9c65  2.25
2015-09-01  Tesco   Pear    7c-e9-a0-a11c  1.75'

df2 <- read.table(text=txt, header=TRUE)
df2$Date <- as.POSIXct(df2$Date)              # CONVERT TO DATE
df2$Month <- format(df2$Date, "%m")           # EXTRACT MONTH (CAN ADJUST FOR MM/DD)

# MERGE AND TRANSFORM FOR NEW COLUMN
finaldf <- transform(merge(df1, df2, by=c("Month", "Shop", "Item", "ProductKey"), suffixes=c("_14", "_15")), 
                     PriceRelative = Price_15 / Price_14)    
finaldf
#   Month  Shop  Item    ProductKey    Date_14 Price_14    Date_15 Price_15 PriceRelative
# 1    09  Asda Apple 0f-7c-32-9c65 2014-09-01      2.0 2015-09-01     2.25      1.125000
# 2    09 Tesco  Pear 7c-e9-a0-a11c 2014-09-01      1.5 2015-09-01     1.75      1.166667

2014年和2015年的價格。請注意,2015年有一個項目在2014年與一個項目不匹配。首先生成哈希作為關鍵字,然后匹配將項目的2014年價格導入2015年數據框。 然后划分:

df2014 <- data.frame(Date = as.Date(c("2014-09-01", "2014-09-01")),
                     Shop = c("Asda", "Tesco"),
                     Item = c("Apple", "Pear"),
                     ProductKey = c("0f-7c-32-9c65","7c-e9-a0-a11c"),
                     Price = c(2.00, 1.50), stringsAsFactors = FALSE)

df2015 <- data.frame(Date = as.Date(c("2015-09-01", "2015-09-01", "2015-09-01")),
                     Shop = c("Asda", "Tesco", "foo"),
                     Item = c("Apple", "Pear", "Orange"),
                     ProductKey = c("0f-7c-32-9c65","7c-e9-a0-a11c", "blah"),
                     Price = c(2.20, 1.70, 3.00), stringsAsFactors = FALSE)

df2014$key <- paste0(strftime(df2014$Date, "%m"),
                     strftime(df2014$Date, "%d"),
                     df2014$Shop,
                     df2014$Item,
                     df2014$ProductKey)

df2015$key <- paste0(strftime(df2015$Date, "%m"),
                     strftime(df2015$Date, "%d"),
                     df2015$Shop,
                     df2015$Item,
                     df2015$ProductKey)

df2015$price_2014 <- df2014$Price[match(df2015$key, df2014$key)]
df2015$price_ratio <- df2015$Price/df2015$price_2014
df2015

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM