R中的符号是什么?

时间:2014-04-09 16:53:48

标签: r

我有时特别在莱迪思和ggplot2中看过这个符号。看起来它是用于将两个变量相关联来表示关系。它是仅在两个图形包中还是在R中定义的?它的解释是什么?

e.g。

cars <- read.csv("cars.csv", row.names=1)
library(lattice)
xyplot(Price ~ Weight, data=cars)
histogram( ~ Weight, data=cars)

1 个答案:

答案 0 :(得分:1)

R支持一种名为“formula”的特殊数据类型,其格式为

LHS ~ RHS

虽然并不总是需要LHS。有关如何指定LHS和RHS的规则及其含义(见?formula)。

公式的解释取决于函数调用,因此您需要阅读特定调用的文档。例如,在

aggregate(mpg~cyl,mtcars,mean)
#   cyl      mpg
# 1   4 26.66364
# 2   6 19.74286
# 3   8 15.10000

该公式表示“以mtcars为单位mpg by cyl并计算每组的平均值”。

另一方面,在lm(...)

中使用时
fit <- lm(mpg~wt+hp+disp,mtcars)
summary(fit)
# ...
# Coefficients:
#              Estimate Std. Error t value Pr(>|t|)    
# (Intercept) 37.105505   2.110815  17.579  < 2e-16 ***
# wt          -3.800891   1.066191  -3.565  0.00133 ** 
# hp          -0.031157   0.011436  -2.724  0.01097 *  
# disp        -0.000937   0.010350  -0.091  0.92851    
# ---
# ...

表示“拟合线性模型mpg = b0 + b1 * wt + b2 * hp + b3 * disp”。请注意,您没有指定b。

xyplot(...)

library(lattice)
xyplot(mpg~wt,mtcars)

该公式表示“在mtcars中绘制mgp vs wt”。

最后,您可以将变量设置为公式,如

myFormula <- mpg~hp+wt+disp
fit <- lm(myFormula,mtcars)