We can use lapply
after getting all the matrices in a list and get colSums
result <- lapply(mget(paste0('mat', 1:5)), colSums)
result
#$mat1
# [1] 4.40 8.07 5.78 8.72 5.82 8.47 11.06 6.41 9.85 14.91
#$mat2
# [1] 8.37 10.69 10.55 8.72 15.85 6.01 10.51 8.82 17.36 7.04
#$mat3
# [1] 13.64 6.71 11.60 13.82 10.78 6.49 10.41 6.05 3.93 10.57
#$mat4
# [1] 8.01 10.85 8.96 11.57 7.90 10.33 5.64 9.98 7.84 5.91
#$mat5
# [1] 15.03 6.55 10.54 6.14 12.57 11.70 9.55 11.05 9.36 6.89
To change the above list output to separate vectors as vec1
, vec2
etc we can use :
names(result) <- paste0('vec', seq_along(result))
list2env(result, .GlobalEnv)
vec1
#[1] 4.40 8.07 5.78 8.72 5.82 8.47 11.06 6.41 9.85 14.91
vec2
#[1] 8.37 10.69 10.55 8.72 15.85 6.01 10.51 8.82 17.36 7.04
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…