Home > Software engineering >  Getting the output of str() and saving it to a dataframe
Getting the output of str() and saving it to a dataframe

Time:02-25

I want to have a script that will ouput the class of a variable for a dataframe. I usually use str() to do this but I realize I would like to save the results to a dataframe.

Code

str(mtcars)

> str(mtcars)
'data.frame':   32 obs. of  11 variables:
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : num  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : num  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : num  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : num  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: num  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: num  4 4 1 1 2 1 4 2 2 4 ...

Is there any easy way to save this output?

My desired output would be something like this

Colname    class
mpg         num
cyl         num
disp        num
etc..   

CodePudding user response:

You can use a couple tidyverse functions: purrr::map_chr calls the class function on each column, returning a named character vector, and tibble::enframe makes a data frame out of the names and values.

tibble::enframe(purrr::map_chr(mtcars, class), name = "Colname", value = "class")
#> # A tibble: 11 × 2
#>    Colname class  
#>    <chr>   <chr>  
#>  1 mpg     numeric
#>  2 cyl     numeric
#>  3 disp    numeric
#>  4 hp      numeric
#>  5 drat    numeric
#>  6 wt      numeric
#>  7 qsec    numeric
#>  8 vs      numeric
#>  9 am      numeric
#> 10 gear    numeric
#> 11 carb    numeric

With base R functions, just create the data frame directly from the names and sapply, dropping the row names (they'll be the same as the first column)

data.frame(Colname = names(mtcars),
           class = sapply(mtcars, class),
           row.names = NULL)
# same output, just not a tibble

CodePudding user response:

We may use stack from base R on a named vector to create two column data.frame

setNames(stack(sapply(mtcars, class))[2:1], c("Colname", "class"))

-output

 Colname   class
1      mpg numeric
2      cyl numeric
3     disp numeric
4       hp numeric
5     drat numeric
6       wt numeric
7     qsec numeric
8       vs numeric
9       am numeric
10    gear numeric
11    carb numeric

Another option is from the output of str with base R

strcapture("^([^:] ):\\s (\\w ).*", trimws(capture.output(str(mtcars))[-1], 
       whitespace = "\\s \\$\\s "), 
  data.frame(Colname = character(), class = character()))

-output

 Colname class
1     mpg    num
2     cyl    num
3     disp   num
4     hp     num
5     drat   num
6     wt     num
7     qsec   num
8     vs     num
9     am     num
10    gear   num
11    carb   num
  • Related