简体   繁体   English

使用xtabs函数创建具有列名的表

[英]Create a table with column names from xtabs function

I am having trouble with the xtabs function. 我在使用xtabs函数时遇到麻烦。 I have been creating tables from data frame using xtabs. 我一直在使用xtabs从数据框创建表。 I am trying to find out with the column names are with the command, names(). 我试图找出列名称与命令names()。 R responds "NULL". R响应“ NULL”。

Why is this, and how do i convert this xtabs table into a table or data frame with column names. 这是为什么,以及如何将这个xtabs表转换为具有列名的表或数据框。

The function ftable does not provide what i need. 函数ftable不提供我所需要的。

Suppose if you want the dataset to look similar to xtabs(..) result with columns: 假设您希望数据集看起来与带有列的xtabs(..)结果相似:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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