請仔細查看示例數據集和所需結果以查看此問題的目的。這不是我正在尋找的合併數據集解決方案。所以我可以在這裏找到答案:How to join (merge) data frames (inner, outer, left, right)?,也沒有在這裏Use apply() to assign value to new column。它指的是一種解決方案,如果它們符合條件,則將值分配給新的名稱。將單元格值分配給for循環中的新列
這裏我願做一個可重複的說明:
Email <- as.factor(c("[email protected]", "[email protected]", "[email protected]","[email protected]", "[email protected]"))
dataset1 <- data.frame(Email)
Code <- as.factor(c("Z001", "Z002", "Z003","Z004","Z005"))
Email <- as.factor(c("[email protected]", "[email protected]", "[email protected]", "[email protected]","[email protected]"))
dataset2 <- data.frame(Code, Email)
這將導致在下面的示例數據集:
Email
1 [email protected]
2 [email protected]
3 [email protected]
4 [email protected]
5 [email protected]
Code Email
1 Z001 [email protected]
2 Z002 [email protected]
3 Z003 [email protected]
4 Z004 [email protected]
5 Z005 [email protected]
所需的輸出:
Email Z002 Z004
1 [email protected] NA 1
2 [email protected] 1 NA
3 [email protected] NA NA
4 [email protected] NA NA
5 [email protected] NA NA
所以我想要編寫一個循環來檢查數據集1中是否出現數據集2的電子郵件,以及此條件是否正確與數據集2中的電子郵件相關聯的代碼被指定爲數據集1的新列名稱,其中該觀察值的單元格值爲1。我試圖完成這個工作,並且期望輸出的一個例子澄清了這個問題。
我自己試圖修復它(我知道這是錯的,但顯示我的本意):
for(i in 1:nrow(dataset2)){
if(dataset2$Email[i] %in% dataset1$Email)
dataset1[,dataset2$Code[i]] <- dataset2$Code[i]
dataset1[,dataset2$Code[i]][i] <- 1
}
將是巨大的,如果有人可以幫助我。
非常感謝!我一直在尋找這個很長的時間,所以你的幫助非常感謝:) – Floris