r - combine data frames after for loop -
i have list. start loop, in loop first convert variable names strings , assign list contend strings right name right content. after loop has finished want combine item#1 #4 list, item#2 , #5 ect. when combining items following:
error in [.data.frame(loopvariable, , "f_corrected_normed_error") : undefined columns selected
later 1 when want calculations inorganicnostdcondition
here code example:
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 string later usage in filename 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 ) )
if wanted change names
of residuallist
, do:
names(residuallist) <- paste0(names(residuallist), 'residuals')
if want use for
loop
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"
for combining list elements 1 5, 2 6 etc.
res <- lapply(1:4, function(i) do.call(`cbind`,residuallist[c(i, i+4)]))
it not clear how want column names named in each list
element of res
data
set.seed(29) iaea_c2_nostdcondition <- data.frame(f_corrected_normed=rnorm(10), f_corrected_normed_error=runif(10)) iaea_c2_eastd <- data.frame(f_corrected_normed=rnorm(10), f_corrected_normed_error=runif(10)) iaea_c2_ststd <- data.frame(f_corrected_normed=rnorm(10), f_corrected_normed_error=runif(10)) iaea_c2_bothstd <- data.frame(f_corrected_normed=rnorm(10), f_corrected_normed_error=runif(10)) tiri_i_nostdcondition <- data.frame(f_corrected_normed=rnorm(10), f_corrected_normed_error=runif(10)) tiri_i_eastd <- data.frame(f_corrected_normed=rnorm(10), f_corrected_normed_error=runif(10)) tiri_i_ststd <- data.frame(f_corrected_normed=rnorm(10), f_corrected_normed_error=runif(10)) tiri_i_bothstd <- data.frame(f_corrected_normed=rnorm(10), f_corrected_normed_error=runif(10)) 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 )
Comments
Post a Comment