for循环后组合数据帧

时间:2014-12-02 03:46:06

标签: r for-loop dataframe

我有一份清单。然后我开始一个for循环,在for循环中我首先将变量名称转换为字符串然后将列表竞争分配给字符串,以便我得到正确的名称和正确的内容。 循环结束后,我确实希望将项目#1与列表中的#4和项目#2和#5等结合起来。 但是当组合这些项目时,我得到以下内容:

Error in [.data.frame(LoopVariable, , "F_corrected_normed_error") : undefined columns selected

稍后我想用InorganicNoStdCondition

进行计算

这是我的代码示例:

ResidualList = list(IAEA_C2_NoStdCondition = IAEA_C2_NoStdCondition,
                    IAEA_C2_EAstd = IAEA_C2_EAstd,
                    IAEA_C2_STstd = IAEA_C2_STstd,
                    IAEA_C2_Bothstd = IAEA_C2_Bothstd,
                    TIRI_I_NoStdCondition = TIRI_I_NoStdCondition,
                    TIRI_I_EAstd = TIRI_I_EAstd,
                    TIRI_I_STstd = TIRI_I_STstd,
                    TIRI_I_Bothstd = TIRI_I_Bothstd
                    )          

C = 8

for(j in 1:C) {

#convert list Variable to string for later usage in filename as unique identifier!!    

SubNameString = names(ResidualList)[j]
SubNameString = paste0(SubNameString, "Residuals")

#print(SubNameString)
SubNameString = ResidualList[[j]]



SubNameString[ ,"F_corrected_normed"] = round(SubNameString[ ,"F_corrected_normed"] /       mean(SubNameString[ ,"F_corrected_normed"]),
                                              digit = 5
                                              )

SubNameString[ ,"F_corrected_normed_error"] = round(SubNameString[ ,"F_corrected_normed_error"] / mean(SubNameString[ ,"F_corrected_normed_error"]),
                                                    digit = 5
                                                    )
#View(SubNameString[ ,"F_corrected_normed"])
#print(SubNameString)

}
      View(SubNameString)
      InorganicNoStdCondition = data.frame(cbind(IAEA_C2_NoStdConditionResiduals, 
                                                 TIRI_I_NoStdConditionResiduals
                                                 )
                                           )

1 个答案:

答案 0 :(得分:0)

如果您想更改names的{​​{1}},可以执行以下操作:

ResidualList

如果您想使用 names(ResidualList) <- paste0(names(ResidualList), 'Residuals') 循环

for

用于将列表元素1与5,2,6等组合。

for(j in 1:C){
   names(ResidualList)[j] <- paste0(names(ResidualList[j]), 'Residuals')
   ResidualList[[j]][ ,"F_corrected_normed"] <- round(ResidualList[[j]][
      ,"F_corrected_normed"] / mean(ResidualList[[j]][ ,"F_corrected_normed"]),
                                           digit = 5)
   ResidualList[[j]][ ,"F_corrected_normed_error"] <- round(ResidualList[[j]][
    ,"F_corrected_normed_error"] / mean(ResidualList[[j]][
    ,"F_corrected_normed_error"]),digit = 5)                                     
 } 

  names(ResidualList)
  #[1] "IAEA_C2_NoStdConditionResiduals" "IAEA_C2_EAstdResiduals"         
  #[3] "IAEA_C2_STstdResiduals"          "IAEA_C2_BothstdResiduals"       
  #[5] "TIRI_I_NoStdConditionResiduals"  "TIRI_I_EAstdResiduals"          
  #[7] "TIRI_I_STstdResiduals"           "TIRI_I_BothstdResiduals"    

目前尚不清楚如何在res <- lapply(1:4, function(i) do.call(`cbind`,ResidualList[c(i, i+4)])) 的每个list元素中命名列名称

数据

res