2017-07-06 54 views
2

我在嘗試合併具有不同名稱但具有相同變量標籤(從SPSS文件導入)的所有列。我試圖去解決這個問題的方法是運行一些檢查以確保列既不是NA也不是相同的,然後粘貼ji並刪除j。然而,這似乎並沒有改變我的數據框。我在這裏做錯了什麼?合併和刪除R中的列

note-- mergedSet是從set1和set2綁定在一起的行,每個行都包含標籤。

for(i in colnames(set1)) { 
    for(j in colnames(set2)){ 
     if(!is.na(attributes(set1)$variable.labels[i]) && 
      !is.na(attributes(set2)$variable.labels[j])) { 
       if(attributes(set1)$variable.labels[i] == 
        attributes(set2)$variable.labels[j]) { 
        if(i != j) { 
         mergedSet <- within(mergedSet, i <- paste(i,j)) 
         mergedSet <- within(mergedSet, rm(j)) 
         } 
        } 
      } 

     } 
    } 
+0

您試過合併(df1,imported_df,by =「key variable」,all.x = TRUE)? –

+0

我同意@CristóbalAlcázar,因爲必須有更好的解決方案。此外,爲什麼不使用「normal」R語法來分配變量,即'mergedSet $ i < - paste(mergedSet $ i,mergedSet $ j)'和'mergedSet $ j < - NULL'?你是否包含了一個支票,你實際上「到達」了代碼塊?例如。您可以在兩條關鍵線之前添加一個打印(「這裏」),以確定您沒有被任何檢查踢出(如果沒有玩具數據集,就很難檢查!)。 – friep

+0

我想你的問題是存在許多列,你不能直接說col x_2 id等於col y_23。也許對於每個分類變量(首先減少問題)迭代所有其他數據分類變量,並且如果唯一標籤相同,則創建一對列。然後規範名稱。 –

回答

0

如果我理解你的問題正確的代碼應該合併基於具有匹配variable.labels和不匹配的列名的列列。

mergedSet <- data.frame(c(1,3,5),c("a","b","c")) 
mergedSet <- data.frame(mergedSet,c("s","","h")) 
attributes(mergedSet)$variable.labels["gas"] <- "three" 
attributes(mergedSet)$variable.labels["xhs"] <- "three" 
attributes(mergedSet)$variable.labels["hhh"] <- "notSame" 
names(mergedSet) <- c("gas","hhh","xhs") 


set1 <- data.frame(c(2),c(4)) 
names(set1) <- c("gas","factpr") 
attributes(set1)$variable.labels["gas"] <- "three" 
attributes(set1)$variable.labels["factpr"] <- "nah" 


set2 <- data.frame(c("asd"),c("pqr")) 
names(set2) <- c("non","hhh") 
attributes(set2)$variable.labels["non"] <- "something" 
attributes(set2)$variable.labels["hhh"] <- "three" 


for(i in colnames(set1)) { 
    for(j in colnames(set2)){ 
    if(!is.na(attributes(set1)$variable.labels[i]) && 
     !is.na(attributes(set2)$variable.labels[j])) { 
     if(attributes(set1)$variable.labels[i] == 
     attributes(set2)$variable.labels[j]) { 
     if(i != j) { 
      mergedSet[, i] <- paste(mergedSet[,i], mergedSet[,j]) 
      mergedSet[, j] <- NULL 
     } 
     } 
    } 
    } 
} 

mergedSet 
# gas xhs 
# 1 1 a s 
# 2 3 b  
# 3 5 c h