使用ifelse语句有条件地rbind到dataframe - R.

时间:2014-04-01 02:59:07

标签: r

我尝试构建一个遍历Excel文件目录的for循环,平均数据行,并将平均数据添加到新数据帧。如果条件不满足,我尝试使用ifelse()函数填充该行并显示错误消息。到目前为止,这是我的for循环:

error=rep(">3 Obs",times=55)

  for (i in 1:length(ExcelFileList){
     excel.file=paste(
        excelpath,ExcelFileList[i],sep="")
     tempdf=readWorksheetFromFile(excel.file, sheet=1, 
        startRow=10, endRow=0, startCol=1, header=FALSE)
     names(tempdf)=header   ## header is a vector of 56 character strings created 
                            #  above; the relevant one to this for loop is the first 
                            # one, called "Obs"
     x=round(colMeans(tempdf[,-2]),3)  ## this creates the vector I want to keep in 
                                       # the summary dataframe "Flux", ignoring            
                                       # column 2 which is a character vector
     Flux=ifelse(x["Obs"==2.000],rbind(x,Flux),rbind(error,Flux))  ## When the    
                    # average of Obs is 2, that means there were 3 observations.  In 
                    # this case, I want to add the vector to the summary dataframe 
                    # Flux; if there are more or less than 3, I want to return an error but still fill the row as a place holder.
      )
   }

我不确定ifelse()是否适合用于选择是否将向量添加到数据帧。如果不是,我很乐意学到别的东西!

非常感谢!

0 个答案:

没有答案