假设我导入了外部数据,这是一个列联表:
var_names <-c("School", "Coffeeshop", "Hospitals", "Parks")
Washington <- c(142, 120, 20, 20)
Seattle<-c(120, 140, 30, 40)
ctable <- data.frame(rbind(Washington, Seattle))
colnames(ctable) <- var_names
ctable
School Coffeeshop Hospitals Parks
Washington 142 120 20 20
Seattle 120 140 30 40
现在ctable是'data.frame',如何在R中将其类转换为'table'?谢谢!
答案 0 :(得分:5)
首先将其转换为矩阵(数组):
class(as.table(as.matrix(ctable)))
[1] "table"