我使用XAMPP设置了一个MySQL数据库,并且已经做了一些非常基本的事情。我尝试使用dbApply,只是为了复制R文档中的示例。但结果看起来并不正确。我期待结果是3项清单:4,6和8缸汽车的mpg百分位数。这是代码:
con <- dbConnect(RMySQL::MySQL(), host = "localhost",dbname="rbaseball"
,user = "root", password = "")
dbWriteTable(con, "mtcars", mtcars, overwrite = TRUE)
res <- dbSendQuery(con, "SELECT * FROM mtcars ORDER BY cyl")
dbApply(res, INDEX="cyl", FUN = function(x,grp) {quantile(x$mpg)})
这是输出。这是一个30项的清单。对于除2个列表之外的所有列表,5个百分位数完全相同,表示在用于计算百分位数的数据向量中只有一个数字。这似乎不是R示例试图获得的输出。
$`4.000000`
0% 25% 50% 75% 100%
22.8 22.8 22.8 22.8 22.8
$`4.000000`
0% 25% 50% 75% 100%
24.4 24.4 24.4 24.4 24.4
$`4.000000`
0% 25% 50% 75% 100%
22.8 22.8 22.8 22.8 22.8
$`4.000000`
0% 25% 50% 75% 100%
32.4 32.4 32.4 32.4 32.4
$`4.000000`
0% 25% 50% 75% 100%
30.4 30.4 30.4 30.4 30.4
$`4.000000`
0% 25% 50% 75% 100%
33.9 33.9 33.9 33.9 33.9
$`4.000000`
0% 25% 50% 75% 100%
21.5 21.5 21.5 21.5 21.5
$`4.000000`
0% 25% 50% 75% 100%
27.3 27.3 27.3 27.3 27.3
$`4.000000`
0% 25% 50% 75% 100%
26 26 26 26 26
$`4.000000`
0% 25% 50% 75% 100%
30.4 30.4 30.4 30.4 30.4
$`6.000000`
0% 25% 50% 75% 100%
21.0 21.1 21.2 21.3 21.4
$`6.000000`
0% 25% 50% 75% 100%
21 21 21 21 21
$`6.000000`
0% 25% 50% 75% 100%
21.4 21.4 21.4 21.4 21.4
$`6.000000`
0% 25% 50% 75% 100%
18.1 18.1 18.1 18.1 18.1
$`6.000000`
0% 25% 50% 75% 100%
19.2 19.2 19.2 19.2 19.2
$`6.000000`
0% 25% 50% 75% 100%
17.8 17.8 17.8 17.8 17.8
$`8.000000`
0% 25% 50% 75% 100%
18.70 18.95 19.20 19.45 19.70
$`8.000000`
0% 25% 50% 75% 100%
14.3 14.3 14.3 14.3 14.3
$`8.000000`
0% 25% 50% 75% 100%
16.4 16.4 16.4 16.4 16.4
$`8.000000`
0% 25% 50% 75% 100%
17.3 17.3 17.3 17.3 17.3
$`8.000000`
0% 25% 50% 75% 100%
15.2 15.2 15.2 15.2 15.2
$`8.000000`
0% 25% 50% 75% 100%
10.4 10.4 10.4 10.4 10.4
$`8.000000`
0% 25% 50% 75% 100%
10.4 10.4 10.4 10.4 10.4
$`8.000000`
0% 25% 50% 75% 100%
14.7 14.7 14.7 14.7 14.7
$`8.000000`
0% 25% 50% 75% 100%
15.5 15.5 15.5 15.5 15.5
$`8.000000`
0% 25% 50% 75% 100%
15.2 15.2 15.2 15.2 15.2
$`8.000000`
0% 25% 50% 75% 100%
13.3 13.3 13.3 13.3 13.3
$`8.000000`
0% 25% 50% 75% 100%
19.2 19.2 19.2 19.2 19.2
$`8.000000`
0% 25% 50% 75% 100%
15.8 15.8 15.8 15.8 15.8
$`8.000000`
0% 25% 50% 75% 100%
15 15 15 15 15