nostats
nostats

Reputation: 3

Multiple level names from output (extraction)

this is kind of a follow up to a previous question that I had (and was answered) about extracting a specific output from a multi-output function. I have had success calling simple output, like the mean from the summary() call, but I'm having difficulty following a similar procedure for extracting output from the fitdistr() function.

For example, when I run this function with the "normal" distribution, the output is mean and sd:

> storage<-fitdistr((as.numeric(diameter.bin[[1]]$Strength)),"normal")

>       storage
      mean          sd    
>0.81428910     0.89574658

>(0.04360426) (0.03083287)

When I lookup (I'm using Rstudio, and I can look at stored variables/matrices etc) what exactly stored in my 'storage' variable, I get this:

structure(list(estimate = structure(c(0.814289099526066, 0.89574657988675
), .Names = c("mean", "sd")), sd = structure(c(0.0436042612645108, 
0.0308328688287655), .Names = c("mean", "sd")), vcov = structure(c(0.00190133160042372, 
0, 0, 0.00095066580021186), .Dim = c(2L, 2L), .Dimnames = list(
c("mean", "sd"), c("mean", "sd"))), n = 422L, loglik = -552.330814327093), .Names = c("estimate", "sd", "vcov", "n", "loglik"), class = "fitdistr")

What I would like to do is extract just the mean value (the first entry under the column 'mean', not the one in brackets). I have tried the following:

> test<-storage["estimate"]

> test

>$estimate
>     mean        sd 

>0.8142891 0.8957466 

So I can successfully pull up the first row of data. Now I'm stuck at extracting just the mean value. My stored variable 'test', which looks like it should have names 'mean' and 'sd', actually has no names to call. Here is what is stored in my 'test' variable:

structure(list(estimate = structure(c(0.814289099526066, 0.89574657988675 ), .Names = c("mean", "sd"))), .Names = "estimate")

I can see that in fact the names "mean" and "sd" are a part of my test variable, but I can't seem to access them. When I try:

> names(test)

>[1] "estimate"

> test["mean"]

>$< N A >

>NULL

Anyone have an idea of how to get access to this other 'level' of names, I would really appreciate it!

Upvotes: 0

Views: 148

Answers (1)

agstudy
agstudy

Reputation: 121568

When you are confused use str to get the right structure of the object.

str(storage)
List of 5
 $ estimate: Named num [1:2] 0.814 0.896
  ..- attr(*, "names")= chr [1:2] "mean" "sd"
 $ sd      : Named num [1:2] 0.0436 0.0308
  ..- attr(*, "names")= chr [1:2] "mean" "sd"
 $ vcov    : num [1:2, 1:2] 0.001901 0 0 0.000951
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:2] "mean" "sd"
  .. ..$ : chr [1:2] "mean" "sd"
 $ n       : int 422
 $ loglik  : num -552
 - attr(*, "class")= chr "fitdistr"

mean and sd are in estimate varaible , I see a $ next to estimate, so I do this

storage$estimate
     mean        sd 
0.8142891 0.8957466 

Then I do

storage$estimate[1]

Or

storage$estimate['mean']

    mean 
0.8142891 

Upvotes: 3

Related Questions