Reputation: 183
rxHistogram(~fare_amount, data = inDataSource, title = "Fare Amount Histogram")
I've found many answers about what the ~
operator is used for, but there is always a left side for the operator in the answers. What if there is none? The sample was taken from here.
Upvotes: 1
Views: 584
Reputation: 10340
"The left-hand side is optional, and one-sided formulae are used in some contexts." (
?`~`
)
If you compare the two following examples of plot you will see, that the plots differ from each other. While the first plot shows a horizontal density-like distribution of the values of carat
the second plot uses the index of each row to plot the carat
values.
plot(~carat, data = ggplot2::diamonds, main = "With tilde", pch = 15, col = "#00000010")
plot(ggplot2::diamonds$carat, main = "NO tilde", pch = 15, col = "#00000010")
However, in some (cf. quote above) contexts like linear models for example, the left side is not optional and missing it will cause an error:
lm(~carat, data = ggplot2::diamonds)
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : incompatible dimensions
Intentionally in cases like lm
the tilde is used to express formulas (in this case the dependent or independent variables). As we can see in ?formula
:
"The
~
operator is basic in the formation of such models. An expression of the formy ~ model
is interpreted as a specification that the response y is modelled by a linear predictor specified symbolically bymodel
."
We can also see that (?formula
):
"There is a formula method for data frames. If there is only one column this forms the RHS with an empty LHS. For more columns, the first column is the LHS of the formula and the remaining columns separated by
+
form the RHS."
For your specific MS example:
The function description of rxHistogram
states that:
" It should take the form of
~x|g1 + g2
whereg1
andg2
are optional conditioning factor variables andx
is the name of a variable or an on-the-fly factorizationF(x)
. Other expressions ofx
are not supported."
Upvotes: 1