Reputation: 814
Question
I want to do a full join on two dataframes so that some data from observations that only have one row and a few vectors with data in them get copied down over multiple rows of all the observations in another dataframe. The two dataframes are matched by "site" and "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 ))
How do I get this error to go away?
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
I could've sworn I didn't touch this since I used it a few months ago- am I now loading package in the wrong order or something? What does "Argument 2" even refer to?
Approach so far
Tried loading plyr first, then dplyr; then tried loading dplyr only. Tried looking at this (dissimilar) question. Tried to find what the error message refers to in ??mutate
, trying to see the source via
> 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")
}
Upvotes: 1
Views: 2856
Reputation: 814
"Argument 2" apparently just refers to the vectors being made anew in one data frame and merged. I started thinking that might be the case so I found the vectors that were integer and character in their respective dataframes by brute force just by going down the list of ~30 vectors being coalesced and commenting them out find which ones made the code work again when they were "off". So, this fixed it: change "yr" to integer in the offending data frame:
df1$yr<-as.integer(as.character(df1$yr))
Upvotes: 1