r - Find error in columns for every dataframe in list -
i have list of dataframes stored in list c. find differences between columns, example (actual-pred1.a, actual-pred2.a, on) every dataframe in list. have 100 dataframes in list have same. have written following example data , code below find it.
a<-data.frame(pred1.a=c(4,3,32,2,3), pred2.a=c(5,3,2,6,22),pred3.a=c(3,2,7,1,23),actual=c(8,9,2,6,21)) b<-data.frame(pred1.b=c(3,6,6,2,5), pred2.b=c(2,7,8,4,23),pred3.b=c(1,4,7,3,22),actual=c(9,2,1,3,25)) c<-list(a,b) i3<-1:3 error <- list() (iter in 1:length(c)){ (i in seq_along(i3)){ error<-sqrt(mean((c[[iter]][4]-c[[iter]][i])^2)) } }
i want error value such error[[1]][1](error value dataframe1:actual-pred1.a),error[[1]][2],error[[1]][3], error[[2]][1].
and there way unlist? because have list contains 100 dataframes.
making minor changes @richscriven's suggestion it's easy errors in separate columns, keeping originals intact:
df_list <- list(a,b) lapply(df_list, function(df) { errors = df$actual - df[1:3] colnames(errors) = paste0("err_", colnames(df)[1:3]) cbind(df, errors) } )
output:
[[1]] pred1.a pred2.a pred3.a actual err_pred1.a err_pred2.a err_pred3.a 1 4 5 3 8 4 3 5 2 3 3 2 9 6 6 7 3 32 2 7 2 -30 0 -5 4 2 6 1 6 4 0 5 5 3 22 23 21 18 -1 -2 [[2]] pred1.b pred2.b pred3.b actual err_pred1.b err_pred2.b err_pred3.b 1 3 2 1 9 6 7 8 2 6 7 4 2 -4 -5 -2 3 6 8 7 1 -5 -7 -6 4 2 4 3 3 1 -1 0 5 5 23 22 25 20 2 3
as mentioned c
not great name variable i've renamed here.
Comments
Post a Comment