mutate_impl(.data,dots)出错:评估错误:参数2必须是字符型

时间:2018-05-11 15:56:24

标签: r dplyr coalesce mutate

问题

我想在两个数据帧上进行完全连接,以便来自观察的一些数据只有一行和几个带有数据的向量被复制到另一个数据帧的所有观察的多行中。这两个数据帧由“site”和“lib”匹配。

library(data.table); library(magrittr); library(dplyr) 
df1<-structure(list(lib = c(10118L, 10118L, 10118L, 10104L, 10104L, 10104L                      ), site = c("yanglingshaanxi", "yanglingshaanxi", "yanglingshaanxi",                                   "tianjin", "tianjin", "tianjin"), y = c(1.896017347, 0.919531829,                                                                           1.150194405, 2.164771575, 1.060472222, 1.372680891), yr = c("1991", "1992", "1993", "2009", "2010",                                                                                                                                       "2012")), .Names = c(                                                                                                                                            "lib", "site", "y","yr"), class = c("data.table", "data.frame"), row.names = c(NA,                                                                                                                                                                                                                      -6L))
df2<-structure(list(yr = c(NA_integer_, NA_integer_), lib = c(10104L,                                                          10118L), site = c("tianjin", "yanglingshaanxi"), y = c(NA_real_,                                                                                                                 NA_real_)), .Names = c("yr", "lib", "site", "y"), class = "data.frame", row.names = c(10695L,                                                                                                                                                                                                       10698L))

y<-full_join(df1, df2,by=c("site","lib")) %>%
  mutate(
    yr=coalesce(yr.x,yr.y), #there are way more vectors that i'm actually coalescing than this it that matters
    y=coalesce(y.x,y.y)
    )  %>% 
  select(-c(
    yr.x,yr.y,
    y.x,y.y,
    lib  ))

如何让这个错误消失?

Error in mutate_impl(.data, dots) : 
  Evaluation error: Argument 2 must be type integer, not character.
In addition: Warning message:
package ‘bindrcpp’ was built under R version 3.3.3 

我可以发誓自从我几个月前使用它以来我没有触及过这种情况 - 我现在正在以错误的顺序加载包装吗? “论据2”甚至指的是什么?

到目前为止的方法

首先尝试加载plyr,然后dplyr;然后尝试只加载dplyr。试着看this (dissimilar) question。试图找到错误消息在??mutate中引用的内容,尝试通过

查看来源
> getAnywhere(mutate)
A single object matching ‘mutate’ was found
It was found in the following places
  package:dplyr
  namespace:dplyr
with value

function (.data, ...) 
{
    UseMethod("mutate")
}

1 个答案:

答案 0 :(得分:1)

“参数2”显然只是指在一个数据帧中重新生成并合并的向量。我开始认为可能是这种情况,所以我发现了各自数据帧中的整数和字符的向量,只需要通过下面的~30个向量的列表进行合并并将它们注释掉,找出哪些使代码再次起作用他们是“关闭”。 因此,这修复了它:在违规数据框中将“yr”更改为整数:

df1$yr<-as.integer(as.character(df1$yr))