Hi all,
I would like to take a variables: d1 to d9 of my "temp" dataframe and make separately nine new dataframes in my workspace like in here, like in a faster way than writing it down one by one, if possible:
library(freqdist)
d1 <- freqdist(temp$d1)
d2 <- freqdist(temp$d2)
d3 <- freqdist(temp$d3)
d4 <- freqdist(temp$d4)
d5 <- freqdist(temp$d5)
d6 <- freqdist(temp$d6)
d7 <- freqdist(temp$d7)
d8 <- freqdist(temp$d8)
d9 <- freqdist(temp$d9)
temp <- structure(list(d1 = structure(c(NA, NA, 1, 2, 1, NA, 4, 1, 2,
2), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d2 = structure(c(NA, NA, 1, 2, 1, NA, 4, 1, 2, 2), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d3 = structure(c(NA, NA, NA, 2, 1, NA, 3, NA, 2, 2), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d4 = structure(c(NA, NA, NA, 2, NA, NA, 4, 4, 1, 1), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d5 = structure(c(3, 4, 4, 2, 1, 1, 1, 4, 4, 3), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d6 = structure(c(2, 1, NA, 2, 2, NA, 1, 1, NA, 3), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d7 = structure(c(4, 2, NA, 2, 2, NA, 3, 4, 2, 4), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d8 = structure(c(2, 2, NA, 2, 2, NA, 3, 4, 3, 4), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
)), d9 = structure(c(1, NA, NA, 2, 1, NA, NA, 1, 1, 1), format.spss = "F1.0", display_width = 12L, class = c("haven_labelled",
"vctrs_vctr", "double"), labels = c(`not applicable` = 0, `not reduced` = 1,
`slightly reduced` = 2, `moderately reduced` = 3, `strongly reduced` = 4
))), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))
and then I want to join these dataframes into one dataframe:
df <- do.call(rbind, list(d1, d2, d3 , d4, d5, d6, d7, d8, d9))
in order to achieve the desired result as below:
Any ideas will be much appreciated, thank you.
I tried with for loop but it does wrong calculations:
for(i in names(temp)) {
nam <- paste("d", i, sep = "")
assign(nam, as.data.frame(freqdist(temp[,i])))
}
I do not know what I am doing wrong.