Building a table/dataframe/something exportable from Desc function output in R

225 views Asked by At

I'm definitely a noob, though I have used R for various small tasks for several years.

For the life of me, I cannot figure out how to get the results from the "Desc" function into something I can work with. When I save the x<-Desc(mydata) the class(x) shows up as "Desc." In R studio it is under Values and says "List of 1." Then when I click on x it says ":List of 25" in the first line. There is a list of data in this object, but I cannot for the life of me figure out how to grab any of it.

Clearly I have a severe misunderstanding of the R data structures, but I have been searching for the past 90 minutes to no avail so figured I would reach out.

In short, I just want to pull certain aspects (N, mean, UB, LB, median) of the descriptive statistics provided from the Desc results for multiple datasets and build a little table that I can then work with.

Thanks for the help.

1

There are 1 answers

7
Josh Rumbut On

Say you have a dataframe, x, where:

x <- data.frame(i=c(1,2,3),j=c(4,5,6))

You could set:

desc.x <- Desc(x)

And access the info on any given column like:

desc.x$i
desc.x$i$mead
desc.x$j$sd

And any other stats Desc comes up with. The $ is the key here, it's how you access the named fields of the list that Desc returns.

Edit: In case you pass a single column (as the asker does), or simply a vector to Desc, you are then returned a 1 item list. The same principle applies but the usual syntax is different. Now you would use:

desc.x <- Desc(df$my.col)
desc.x[[1]]$mean

In the future, the way to attack this is to either look in the environment window in RStudio and play around trying to figure out how to access the fields, check the source code on github or elsewhere, or (best first choice) use str(desc.x), which gives us:

> str(desc.x)
List of 1
 $ :List of 25
  ..$ xname     : chr "data.frame(i = c(1, 2, 3), j = c(4, 5, 6))$i"
  ..$ label     : NULL
  ..$ class     : chr "numeric"
  ..$ classlabel: chr "numeric"
  ..$ length    : int 3
  ..$ n         : int 3
  ..$ NAs       : int 0
  ..$ main      : chr "data.frame(i = c(1, 2, 3), j = c(4, 5, 6))$i (numeric)"
  ..$ unique    : int 3
  ..$ 0s        : int 0
  ..$ mean      : num 2
  ..$ meanSE    : num 0.577
  ..$ quant     : Named num [1:9] 1 1.1 1.2 1.5 2 2.5 2.8 2.9 3
  .. ..- attr(*, "names")= chr [1:9] "min" ".05" ".10" ".25" ...
  ..$ range     : num 2
  ..$ sd        : num 1
  ..$ vcoef     : num 0.5
  ..$ mad       : num 1.48
  ..$ IQR       : num 1
  ..$ skew      : num 0
  ..$ kurt      : num -2.33
  ..$ small     :'data.frame':  3 obs. of  2 variables:
  .. ..$ val : num [1:3] 1 2 3
  .. ..$ freq: num [1:3] 1 1 1
  ..$ large     :'data.frame':  3 obs. of  2 variables:
  .. ..$ val : num [1:3] 3 2 1
  .. ..$ freq: num [1:3] 1 1 1
  ..$ freq      :Classes ‘Freq’ and 'data.frame':   3 obs. of  5 variables:
  .. ..$ level  : Factor w/ 3 levels "1","2","3": 1 2 3
  .. ..$ freq   : int [1:3] 1 1 1
  .. ..$ perc   : num [1:3] 0.333 0.333 0.333
  .. ..$ cumfreq: int [1:3] 1 2 3
  .. ..$ cumperc: num [1:3] 0.333 0.667 1
  ..$ maxrows   : num 12
  ..$ x         : num [1:3] 1 2 3
 - attr(*, "class")= chr "Desc"

"List of 1" means you access it by desc.x[[1]], and below that follow the $s. When you see something like num[1:3] that means it's an atomic vector so you access the first member like var$field$numbers[1]