2014-06-15 53 views
0

合併2個數據幀時,出現「名稱與先前名稱不匹配」錯誤。但是,他們都有完全相同的名字。我把**放在我定義數據幀的兩個地方,但它們是相同的。到底是怎麼回事?謝謝!合併R中的數據幀導致「名稱與先前名稱不匹配」

確切錯誤:錯誤match.names(clabs,姓名(十一)):名稱不匹配以前的名字

corr <- function(directory, threshold = 0) { 
    #store data frame that holds sulfate amount and nitrate amount that meet threshold and are complete cases 
    **data <- data.frame(sulfate = numeric(0), nitrate = numeric(0))** 

    #set working directory 
    setwd(directory) 

    #get file names 
    myfiles <- list.files(pattern = "csv") 

    #loop through files 
    for(i in 1:332) { 

     #read each file 
     current_dataset <- read.csv(myfiles[i]) 

     #check if there are enough compelte cases to meet threshold 
     if(sum(complete.cases(current_dataset)) > threshold) { 

      #get complete cases 
      complete_cases <- current_dataset[complete.cases(current_dataset), ] 

      #add sulfate and nitrate info to table 
      **data <- rbind(data, data.frame(sulfate = complete_cases$sulfate[i], nitrate = complete_cases$nitrate)[i])** 
     } 
    } 
    #get correlation 
    cor(data) 
} 
+0

是否所有的括號中的心病直接放置在下面的行? 'data < - rbind(data,data.frame(sulfate = complete_cases $ sulfate [i],nitrate = complete_cases $ nitrate)[i]) – userNaN

+0

我的第二個userNaNs評論 – miles2know

回答

0

您還沒有給出一個可重複的例子,所以我猜,但我會嘗試

... 
for(i in seq_along(myfiles)) { 
    #read each file 
    current_dataset <- read.csv(myfiles[i]) 
    #check if there are enough complete cases to meet threshold 
    if(sum(cc <- complete.cases(current_dataset)) > threshold) { 
     #get complete cases 
     complete_cases <- current_dataset[cc, ] 
     #add sulfate and nitrate info to table 
     data <- rbind(data, complete_cases[,c("sulfate","nitrate")]) 
    } 
} 

alldat <- lapply(myfiles,read.csv) 
ccdat <- lapply(alldat,function(x) x[complete.cases(x),]) 
ccdat2 <- ccdat[sapply(ccdat,nrow)>threshold] 
ccdat3 <- lapply(ccdat2,function(x) x[,c("sulfate","nitrate")]) 
final <- do.call(rbind,ccdat3) 
相關問題