无法使用dplyr()计算mean()

时间:2019-05-28 21:14:23

标签: r dplyr

我有以下代码,在其中我无法计算代码的最后一行(第17列中前5个值的平均值),从而导致出现警告消息Warning message:In mean.default(df[1:5, 17], na.rm = TRUE) : argument is not numeric or logical: returning NA

有人建议我在做什么错吗?谢谢!

library(eurostat)
library(dplyr)
library(tidyr)


earn_mw_avgr2 <- get_eurostat("earn_mw_avgr2", stringsAsFactors = FALSE)

df <- earn_mw_avgr2 %>% 
    filter(geo %in% c("BE","BG","CZ","DK","DE","EE","IE","EL","ES","FR","HR",
                   "IT","CY","LV","LU","HU","MT","NL","AT","PL","PT","RO",
                   "SI","SK","FI","SE","UK"),
        indic_se=="MW_MEAGE",
        nace_r2=="B-S") %>% 
    spread(time, values)%>%
    select(-c(17,18))%>%
    mutate(avg=rowMeans(.[14:16], na.rm = TRUE))%>%
    arrange(desc(avg))


mean(df[1:5,17], na.rm = TRUE) 

2 个答案:

答案 0 :(得分:4)

问题是它是newUser.contactDetails.countryName = "UK"; ,并且不会删除尺寸为tibble(其中data.frame?Extract的尺寸)

drop = TRUE

mean(df[1:5,][[17]]) #[1] 47.29667 适用于mean s。如果我们检查vector

  

x-一个R对象。当前,存在用于数字/逻辑矢量以及日期,日期时间和时间间隔对象的方法。仅允许trim = 0的复数向量。

答案 1 :(得分:1)

sed添加到$avg可以解决此问题。完整代码如下。

df[1:5,17]