简体   繁体   中英

Manually entering contingency table in R

Hi I have the following code in R,

> trial <- matrix(c(6,9,6,5,2,2,5,8,5,3,2,4,6,8,8), ncol=5,byrow=T)
> colnames(trial) <- c('Never','Rarely','Sometimes', 'Often' ,'Always')
> rownames(trial) <- c('Walk', 'Bus','Drive')

> trial
      Never Rarely Sometimes Often Always
Walk      6      9         6     5      2
Bus       2      5         8     5      3
Drive     2      4         6     8      8

However what I want is;

Breakfast  Never  Rarely  Sometimes Often  Always
Travel
Walk         6      9       6         5      2
Bus          2      5       8         5      3
Drive        2      4       6         8      8

ie a contingency table where Breakfast has options Never, Rarely etc. and Travel has options Walk,bus etc. Thank you

Answered courtesy: Salvatore S. Mangiafico, Ph.D.

trial <- matrix(c(6,9,6,5,2,2,5,8,5,3,2,4,6,8,8), ncol=5,byrow=T)
colnames(trial) <- c('Never','Rarely','Sometimes', 'Often' ,'Always')
rownames(trial) <- c('Walk', 'Bus','Drive')

Trial = as.table(trial)

dimnames(Trial)
names(dimnames(Trial)) = c("Transport", "Breakfast")

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