如何从R中的数据库计算数据(如均值、中位数等)
对于我的项目,我必须从PostgreSQL数据库计算R中的数据。但在R中,我的数据无法计算,因为它被读取为字符串。当我试图将其转换为数字或双精度时。这是错误的。 这是我的R代码如何从R中的数据库计算数据(如均值、中位数等),r,postgresql,R,Postgresql,对于我的项目,我必须从PostgreSQL数据库计算R中的数据。但在R中,我的数据无法计算,因为它被读取为字符串。当我试图将其转换为数字或双精度时。这是错误的。 这是我的R代码 > myTable<-"SELECT DISTINCT + round(avg(finalcall),2) + FROM kpidetail + where finalcall is not null AND priority like 'call' + group by agentname" > d
> myTable<-"SELECT DISTINCT
+ round(avg(finalcall),2)
+ FROM kpidetail
+ where finalcall is not null AND priority like 'call'
+ group by agentname"
> data0<-dbGetQuery(con,myTable)
> data0
round
1 91.56
2 88.72
3 100.00
4 70.00
5 95.00
> mean(data0)
[1] NA
Warning message:
In mean.default(data0) : argument is not numeric or logical: returning NA
> dataj<-as.numeric(data0)
Error: (list) object cannot be coerced to type 'double'
> mean(unlist(asnumeric(data0)))
Error in unlist(asnumeric(data0)) : could not find function "asnumeric"
> dataj<-unlist(as.double(data0))
Error in unlist(as.double(data0)) :
(list) object cannot be coerced to type 'double'
> dataj<-as.double(data0)
Error: (list) object cannot be coerced to type 'double'
>myTable数据0数据0
圆形的
1 91.56
2 88.72
3 100.00
4 70.00
5 95.00
>平均值(数据0)
[1] NA
警告信息:
默认值(data0):参数不是数字或逻辑参数:返回NA
>dataj平均值(未列出(asnumeric(data0)))
取消列表时出错(asnumeric(data0)):找不到函数“asnumeric”
>dataj dataj对dbGetQuery()
的调用似乎返回了一个列表,其中一个元素是SQL结果集中的列round
。您可以从列表中访问此round
列,然后获得所需的计算结果(例如平均值):
mean(as.numeric(data0$round))