R For Loop将列添加到数据框 - 仅限最后一列

时间:2016-01-15 18:02:39

标签: r

我对来自SAS的R来说有点新意,而我所要做的就是从一组标记为mean1-meanN的变量计算一些变量,其中N = numclust在下面的代码中。我可以在SAS中蒙住眼睛,但是经过几天的广泛搜索和研究以及尝试和重试之后,我得知。我需要帮助!

以下是完整代码:

IndiceCalc<-function(filePath,numclust){


  #convert appropriate SAS checkerboard file to R

  fileName <-     paste(filePath,"\\","checkerboard",numclust,".sas7bdat",sep="")
  fromSAS<-read.sas7bdat(file=fileName)


  #keep relevant columns and subset

  meanVars<-paste("mean",1:numclust,sep="")
  vars2keep<-c("groupnum","GroupName","variable","label",meanVars)
  sub.fromSAS<-na.omit(subset(fromSAS,totalmean!=0,select=vars2keep))

  #calculate indices across segment rows

  output<-data.frame(matrix(ncol=numclust,nrow=nrow(sub.fromSAS)))

  for (i in 1:numclust){

    j=numclust+4
    output[,i]<-(sub.fromSAS[,j]-rowAvgs(sub.fromSAS[,meanVars],        na.rm=TRUE))/rowStdevs(sub.fromSAS[,meanVars], na.rm=TRUE)
  }


  colnames(output)<-paste("index",1:numclust,sep="")

  calcDat<-cbind(sub.fromSAS,output)

  head(calcDat)

}

我尝试了许多人推荐的方法,通过创建一个空白的数据框/矩阵并将其填入,然后将其绑定到我的原始数据框。

我的数据帧返回原始数据加上Index1-IndexN列。但是,填充的数据全部来自第N列/迭代的结果。

index1  index2  index3  index4  index5
-0.9384286  -0.9384286  -0.9384286  -0.9384286  -0.9384286
0.481684    0.481684    0.481684    0.481684    0.481684
0.3883754   0.3883754   0.3883754   0.3883754   0.3883754
0.645928    0.645928    0.645928    0.645928    0.645928
1.6327587   1.6327587   1.6327587   1.6327587   1.6327587
-0.3524674  -0.3524674  -0.3524674  -0.3524674  -0.3524674

不知道如何解决这个问题。救命啊!

1 个答案:

答案 0 :(得分:0)

虽然如果我有选择的话,这是我不会在R中做的事情,这里有一个片段可以在你获得sub.fromSAS之后做你想做的事情:

calcDat <- cbind( # bind to the original dataframe
  sub.fromSAS
  , t( # transpose the matrix to get back into original orientation
    # Apply code of anonymous function row-wise
    # on the sub-dataframe with meanX... columns
    apply(sub.fromSAS[, 4+1:numclust] , 1, function(x) {

      # Change the name of columns from meanX to indexX
      names(x) <- paste0("index", 1:numclust)

      # Compute Z scores 
      (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
    }
    )))

如果您需要进一步的帮助,请告诉我。