What does a formula with no left argument mean in R, e.g. ~x? -


i understand in formula y ~ x looking @ "y" function of "x". in maths f(x) = x.

in r, functions xtabs can take formula objects without left side, e.g. xtabs( ~ x). understanding of formulas, looking @ nothing function of "x", in maths = x, not how r understands formula (it returns contingency table of factor, example).

so how can understand meaning of empty left hand argument?

i'm sure has been explained somewhere, have hard time googling "r ~".

formulas have meaning in context of particular functions work them. same formula may mean different 1 function vs. function.

in case of xtabs sums left hand side on levels of right hand side , if there no left hand side gives counts. is, default left hand side can regarded vector of ones. e.g. these each give same result

x <- c(1, 1, 2, 2, 2)  # 1 xtabs(~ x)  # 2 ones <- rep(1, length = length(x)) xtabs(ones ~ x) 

this gives similar result in case result array rather table:

# 3 tapply(ones, x, sum) 

Comments

Popular posts from this blog

c# - Operator '==' incompatible with operand types 'Guid' and 'Guid' using DynamicExpression.ParseLambda<T, bool> -