2017-06-19 64 views
0

我試圖通過比較列x的值與不同數據框(df2)中列y的值在現有數據框(df1)中創建一個新列。基於兩列之間的匹配的數據幀中的新列

結果應該看起來像df_end。如果有匹配,則應返回第x列的值。如果沒有匹配,則應退還NA。

df1 <- data.frame(x = c("blue2", "blue6", "green9", "green7")) 
df2 <- data.frame(y = c("blue2", "green9")) 

df_end <- data.frame(x = c("blue2", "blue6", "green9", "green7"), 
        match = c("blue2", NA, "green9", NA)) 

我已經試驗過merge,match和if,else語句,但我無法弄清楚。有人對我有一個建議嗎?

#Attempt 1: Merge 
df1$match <- merge(df1, df2, by.x = x, all = TRUE) 

這不起作用,因爲df1和df2長度不同。

+0

使用'和調整' by.x'和'by.y'參數來匹配各自的變量名稱。 – lmo

+2

'df2 $ y [match(df1 $ x,df2 $ y)]' – Sotos

回答

1

我做了以下內容:

df1 <- data.frame(x = c("blue2", "blue6", "green9", "green7")) 
df2 <- data.frame(y = c("blue2", "green9")) 

end <- sapply(df1$x, function(x) { # for each value in df1$x 
    j <- which(df2$y == x) # check if df2$y has a match 
    ifelse(length(j) > 0, j, NA) # if there is, give the location in the vector 
}) # if not give NA 

cbind(df1,df2, match = df2$y[end]) # subset the df2 with the location to get the characters 

#  x  y match 
#1 blue2 blue2 blue2 
#2 blue6 green9 <NA> 
#3 green9 blue2 green9 
#4 green7 green9 <NA> 

編輯: 看到索托斯的最佳答案評論:merge`與`所有= TRUE`參數df2$y[match(df1$x, df2$y)]

+0

此解決方案有效。最終我用了sotos解決方案,因爲它更簡潔一些:) – SHW

+0

是的,我不知道爲什麼我在匹配函數XD上空白 –

相關問題