I often use do.call("cbind.data.frame", my_list) after a lapply() call and usually I face no problems. For some reason in the following list the column names are different after binding them; the list number preceeds the names.
My list is something like this:
dput(my_list)
list(`1` = structure(list(`Pulmonary_embolism~f.20002.0` = NA,
`Pulmonary_embolism~f.20002.1` = NA, `Pulmonary_embolism~f.20002.2` = NA,
`Pulmonary_embolism~f.20002.3` = NA, `Pulmonary_embolism~f.20002.all` = NA), row.names = "1", class = "data.frame"),
`2` = structure(list(`Pulmonary_embolism~f.6152.0` = NA,
`Pulmonary_embolism~f.6152.1` = NA, `Pulmonary_embolism~f.6152.2` = NA,
`Pulmonary_embolism~f.6152.3` = NA, `Pulmonary_embolism~f.6152.all` = NA), row.names = "1", class = "data.frame"))
But after do.call("cbind.data.frame", my_list) the variables change:
names(do.call("cbind.data.frame", my_list))
[1] "1.Pulmonary_embolism~f.20002.0" "1.Pulmonary_embolism~f.20002.1" "1.Pulmonary_embolism~f.20002.2" "1.Pulmonary_embolism~f.20002.3" "1.Pulmonary_embolism~f.20002.all"
[6] "2.Pulmonary_embolism~f.6152.0" "2.Pulmonary_embolism~f.6152.1" "2.Pulmonary_embolism~f.6152.2" "2.Pulmonary_embolism~f.6152.3" "2.Pulmonary_embolism~f.6152.all"
How to prevent the list number beeing part of the column name?
First you could extract the column names using
lapplywithunlistand make sure you useuse.names=FALSEto remove the names of the dataframes in the names of the column. After that you can use these names to yourdo.calloutput like this:Another option could be using
bind_colsfromdplyrto not have the numbers in the names like this:Created on 2023-06-29 with reprex v2.0.2