user4095160
user4095160

Reputation:

Remove variable name as table title

table has a peculiar behaviour in the sense that it uses the variable name as table 'title'

> table(c("A","A","B"))

A B 
2 1
> a<-c("A","A","B");table(a)
a
A B 
2 1 

This behaviour is not convenient if you have a function which returns contingency tables

> aux <- function(x) return(table(x))
> aux(a)
x
A B 
2 1 

Is there a way to remove the table 'title'? Can I remove the table title and not get that blank line? I found a workaround, but I am not entirely satisfied with it.

> aux <- function(x) return(table(identity(x)))
> aux(a)

A B 
2 1 

Upvotes: 0

Views: 189

Answers (2)

Rich Scriven
Rich Scriven

Reputation: 99331

You can use deparse.level = 0 in table(). Check help(table) for its possible values with explanation.

a <- c("A", "A", "B")

table(a)
# a
# A B 
# 2 1 
table(a, deparse.level = 0)
#
# A B 
# 2 1 

Upvotes: 4

akrun
akrun

Reputation: 887108

We can use as.vector

as.vector(table(a))
#[1] 2 1

If we need to remove the 'a' in the second case

tbl <- table(a)
names(dimnames(tbl)) <- NULL

Upvotes: 0

Related Questions