Reputation: 6117
Missing column-names from table()-output
The function table() will print the column-names of the dataframe in the table, when the data.frame() is created like this:
table(data.frame( var1 = "yes", var2 = "no") )
var2
var1 no
yes 1
But when i create the data.frame() like this, the table()-function will not print any column names:
e1 <- data.frame( smoking = "no", cvd = "no" )
e1 <- e1[rep(1,3495),]
e2 <- data.frame( smoking = "no", cvd = "yes" )
e2 <- e2[rep(1,57), ]
e3 <- data.frame( smoking = "yes",cvd = "no")
e3 <- e3[rep(1,2112),]
e4 <- data.frame( smoking = "yes",cvd = "yes")
e4 <- e4[rep(1,75),]
ee <- rbind( e1,e2,e3,e4)
No names are printed in the table:
> table( ee$smoking, ee$cvd)
no yes
no 3495 57
yes 2112 75
Despite the ee is actually a data.frame and has names:
> class(ee)
[1] "data.frame"
> names(ee)
[1] "smoking" "cvd"
So my question is why does the table()-function not print names when called on the ee-data.frame?
Upvotes: 0
Views: 60
Reputation: 887118
There is a dnn
argument in table
which could work in cases where the input argument are vector
s
table(ee$cvd, ee$smoking, dnn = list("smoking", "cvd"))
# cvd
#smoking no yes
# no 3495 2112
# yes 57 75
Upvotes: 0
Reputation: 445
It does print the names if you call table with the data.frame, the below code works:
table(ee)
cvd
smoking no yes
no 3495 57
yes 2112 75
But when you call table(ee$cvd,ee$smoking)
it treats them as individual vectors and not a data.frame
Upvotes: 1