R: arguments in table command must all have the same length -


> table(x = c(1, 0, 1, 0, 1), y = c(1, 1, 1)) error in table(x = c(1, 0, 1, 0, 1), y = c(1, 1, 1)) :    arguments must have same length 

i encountered error when trying run table command. however, don't see why such table can't constructed?

   0   1 x  2   3 y  0   3 

is there way construct table of categorical data vectors not same length?

table literally requires 2 variables of same length tabulated. same similar frequency/pivot/table functions in sas, spss, stata, excel etc etc etc. need 2 columns - 1 showing source (x or y) , 1 showing value (0 or 1).

x <- c(1, 0, 1, 0, 1) y <- c(1, 1, 1) source <- rep(c("x","y"), c(length(x), length(y))) value  <- c(x,y)  table(source,value) #      value #source 0 1 #     x 2 3 #     y 0 3 

there trickier ways this, straight-forward. 1 alternative comes mind is:

table(stack(list(x=x,y=y))) #or  table(stack(mget(c("x","y")))) 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -