Johnathan
Johnathan

Reputation: 1907

Can I store the results of the summary function in R?

I have lists in R (given by collaborators). Each list has a boolean variable called "er", and I would like to compute the ratio of TRUE/FALSE. However, there might be NA.

I applied the summary function on one list and got

Mode        FALSE    TRUE   NA's
"logical"   "199"    "798"  "0"

I tried to store the results with:

table <- summary(....)

R accepts the command, but I don't see anything in my environment. Any hint would be greatly appreciated. Thank you!

Upvotes: 1

Views: 3501

Answers (2)

Marco Torchiano
Marco Torchiano

Reputation: 732

You cannot see anything in your environment because with

Apparently it appears in the var list, though you should be careful because with

table <- summary(...)

you are overwriting the built-in function table(), which btw is the one you should be using instead of summary. For instance

l <- rep(c(T,F,NA),c(798,199,0))
freq <- table(l,useNA="always")

Upvotes: -1

Anders Ellern Bilgrau
Anders Ellern Bilgrau

Reputation: 10223

It appears to work just fine:

ER <- c(TRUE, FALSE, NA, TRUE, TRUE, FALSE, FALSE, FALSE)
a <- summary(ER)
print(a)
#   Mode   FALSE    TRUE    NA's 
#logical       4       3       1 

But if you're interested in a table, take a look of the function of the same name:

b <- table(ER, useNA = "ifany")
print(b)
#ER
#FALSE  TRUE  <NA> 
#    4     3     1 

This should also illustrate why you might want to avoid making a variable called table.

Edit If you want the proportion of TRUE and FALSE entries then something like

b/sum(b)
#ER
#FALSE  TRUE  <NA> 
#0.500 0.375 0.125 

or

b/sum(!is.na(ER))
#ER
#    FALSE      TRUE      <NA> 
#0.5714286 0.4285714 0.1428571

should work depending on whether you want to count the NAs. In the latter, the number given under the NAs is meaningless.

Edit2 Or even better, take a look at prop.table as suggested by Gregor in the comments.

Upvotes: 2

Related Questions