How to create objects from dataframe$variables and join them together into df

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.

library(freqdist)

temp  <-
  structure(
    list(
[...]
    row.names = c(NA,-10L),
    class = c("tbl_df", "tbl", "data.frame")
  )

# lets create data frame of desired results/shape
cosik <- data.frame(
  INTERVENTIONS = character(),
  rowname = character(),
  frequencies = numeric(),
  percentage = numeric(),
  cumulativepercentage = numeric()
)

# lets run it for each column and rbind to new data frame
for (i in 1:ncol(temp)) {
  d <- freqdist(temp[,i])
  d["INTERVENTIONS"] <- paste("d",i,sep="")
  d["rowname"] <- row.names(d)
  d <- d[, c("INTERVENTIONS", "rowname", "frequencies", "percentage", "cumulativepercentage")]
  cosik <- rbind(cosik, d)
}
str(cosik)

Hope it helps,
Grzegorz

Hi @gsapijaszko and thank you very much,

One more question, how to do this, if I want to have single variables (d1:d9) as separate dataframes in my environment, please ? Is it the other way to do this than as below:

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)

For example for View(d1) it gives me:

obraz

best,
Andrzej

for (i in 1:ncol(temp)) {
  assign(paste0("d", i), freqdist(temp[,i]))
}

should work :slight_smile:

Regards,
Grzesiek

Yes, indeed , thanks a lot again,

kind regards,
Andrzej

Another option using purrr:map_dfr()

library(freqdist)
library(tidyverse)

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"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ))
map_dfr(.x = names(temp) %>% set_names,
        .f = ~ freqdist(temp[[.x]]) %>%  rownames_to_column(var = "rowname"),
        .id = "INTERVENTIONS")
#>    INTERVENTIONS rowname frequencies percentage cumulativepercentage
#> 1             d1       1           3   42.85714             42.85714
#> 2             d1       2           3   42.85714             85.71429
#> 3             d1       4           1   14.28571            100.00000
#> 4             d1  Totals           7  100.00000            100.00000
#> 5             d2       1           3   42.85714             42.85714
#> 6             d2       2           3   42.85714             85.71429
#> 7             d2       4           1   14.28571            100.00000
#> 8             d2  Totals           7  100.00000            100.00000
#> 9             d3       1           1   20.00000             20.00000
#> 10            d3       2           3   60.00000             80.00000
#> 11            d3       3           1   20.00000            100.00000
#> 12            d3  Totals           5  100.00000            100.00000
#> 13            d4       1           2   40.00000             40.00000
#> 14            d4       2           1   20.00000             60.00000
#> 15            d4       4           2   40.00000            100.00000
#> 16            d4  Totals           5  100.00000            100.00000
#> 17            d5       1           3   30.00000             30.00000
#> 18            d5       2           1   10.00000             40.00000
#> 19            d5       3           2   20.00000             60.00000
#> 20            d5       4           4   40.00000            100.00000
#> 21            d5  Totals          10  100.00000            100.00000
#> 22            d6       1           3   42.85714             42.85714
#> 23            d6       2           3   42.85714             85.71429
#> 24            d6       3           1   14.28571            100.00000
#> 25            d6  Totals           7  100.00000            100.00000
#> 26            d7       2           4   50.00000             50.00000
#> 27            d7       3           1   12.50000             62.50000
#> 28            d7       4           3   37.50000            100.00000
#> 29            d7  Totals           8  100.00000            100.00000
#> 30            d8       2           4   50.00000             50.00000
#> 31            d8       3           2   25.00000             75.00000
#> 32            d8       4           2   25.00000            100.00000
#> 33            d8  Totals           8  100.00000            100.00000
#> 34            d9       1           5   83.33333             83.33333
#> 35            d9       2           1   16.66667            100.00000
#> 36            d9  Totals           6  100.00000            100.00000

Created on 2022-01-18 by the reprex package (v2.0.1)

1 Like

Thank you for help, but when I run your code I got an error:

Error in set_names(.) : 1 argument passed to 'names<-' which requires 2

That is weird maybe you are using an older purrr version that requires you to explicitly set the names, try with this:

map_dfr(.x = names(temp) %>% purrr::set_names(names(temp)),
        .f = ~ freqdist(temp[[.x]]) %>%  rownames_to_column(var = "rowname"),
        .id = "INTERVENTIONS")

As you can see in the reprex from my original post, it works with the sample data you have provided

Now it works nicely. I use purrr 0.3.4, so up-to-date version. Thank you very much again.
I have seen your reprex and was suprised that it did not work for me. I use R x64 v. 4.1.1 and RStudio daily v. 361.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.