2

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.

J. Doe
  • 21
  • 4

1 Answers1

0

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]

Josh Rumbut
  • 2,640
  • 2
  • 32
  • 43
  • Hi, thank you so much for your reply. I should specify that I am calling Desc(x) on just one column of my df. It is giving me multiple lines of summary statistics. When I set desc.x <- Desc(x$col1) it returns my data in the console, but when I try, for example, desc.x$mean or anything else it just returns NULL. .... Good call, though! I will just call Desc across the whole df (rather than just one column) and specify retroactively on what I want. Thank you! – J. Doe Sep 26 '18 at 17:52
  • The same principle works for a single column, in that case you would do: `desc.x[[1]]$length` or `desc.x[[1]]$mean` – Josh Rumbut Sep 26 '18 at 18:05
  • Oh wonderful thank you. Last question - some of the returns, such as MeanCI and Median are not options when I do desc.x$median etc. Any other tips? you rock thanks again – J. Doe Sep 26 '18 at 18:10
  • This library is really great for learning R data structure access patterns apparently. You get the median through this: `desc.x[[1]]$quant['median']`. Somehow Mean CI gets cut off from the results, seems like a bug maybe? It's easy enough to calculate: `qt(p=0.025, df=desc.x[[1]]$n - 1) * desc.x[[1]]$meanSE` for a 95% CI. – Josh Rumbut Sep 26 '18 at 18:27
  • Wow thanks a million. May I ask you how you determined this structure so I don't have to bother people like you in the future? Thank you. – J. Doe Sep 26 '18 at 18:50
  • See my edit! Also check out http://adv-r.had.co.nz/Data-structures.html – Josh Rumbut Sep 26 '18 at 19:21
  • 1
    Nothing to add to Josh's comments. Besides, hmm, I wouldn't directly call the "MeanCI missing in the resulting object" thing a bug. It is calculated in the print routine print.Desc.numeric for historical reasons. But I admit it violates the rule of concentrating calculations on one place. I'll consider to change that and report the MeanCI as well directly in the result object. – Andri Signorell Sep 27 '18 at 07:40