2017-08-04 72 views
1
rxHistogram(~fare_amount, data = inDataSource, title = "Fare Amount Histogram") 

我發現很多關於~操作符用於什麼的答案,但操作員在答案中始終存在左側。如果沒有?樣品取自here沒有左側的Tilde操作符

回答

1

"The left-hand side is optional, and one-sided formulae are used in some contexts." (?`~`)

如果您比較以下兩個繪圖示例,您會看到,繪圖各不相同。雖然第一個圖顯示carat的值的水平密度類分佈,但第二個圖使用每行的索引來繪製carat值。

plot(~carat, data = ggplot2::diamonds, main = "With tilde", pch = 15, col = "#00000010") 
plot(ggplot2::diamonds$carat, main = "NO tilde", pch = 15, col = "#00000010") 

然而,在一些(參見以上引用)等例如線性模型的上下文中,左側是不可選的,並且缺少會引起一個錯誤:

lm(~carat, data = ggplot2::diamonds) 

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : incompatible dimensions

故意在lm等情況下,代字號用於表示公式(在這種情況下,依賴於o r獨立變量)。正如我們在?formula看到:

"The ~ operator is basic in the formation of such models. An expression of the form y ~ model is interpreted as a specification that the response y is modelled by a linear predictor specified symbolically by model ."

我們也可以看到,(?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."

針對您的特殊MS例如:

rxHistogramfunction description的規定:

" It should take the form of ~x|g1 + g2 where g1 and g2 are optional conditioning factor variables and x is the name of a variable or an on-the-fly factorization F(x) . Other expressions of x are not supported."

相關問題