简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM