MadmanLee
MadmanLee

Reputation: 558

Create a table with column names from xtabs function

I am having trouble with the xtabs function. I have been creating tables from data frame using xtabs. I am trying to find out with the column names are with the command, names(). R responds "NULL".

Why is this, and how do i convert this xtabs table into a table or data frame with column names.

The function ftable does not provide what i need.

Upvotes: 0

Views: 3408

Answers (1)

akrun
akrun

Reputation: 887098

Suppose if you want the dataset to look similar to xtabs(..) result with columns:

 dat <- structure(list(cntry1 = c("usa", "usa", "usa", "canada", "canada", 
 "cuba"), cntry2 = c("canada", "bahamas", "cuba", "bahamas", "cuba", 
 "bahamas"), var1 = c(70L, 29L, 39L, 15L, 35L, 5L)), .Names = c("cntry1", 
 "cntry2", "var1"), class = "data.frame", row.names = c(NA, -6L
 ))

 r1 <- xtabs(var1~cntry2+cntry1, dat)
 as.data.frame.matrix(r1)
 #         canada cuba usa
 #bahamas     15    5  29
 #canada       0    0  70
 #cuba        35    0  39

Upvotes: 3

Related Questions