jwildfire
jwildfire

Reputation: 68

full_join data with 0 rows

I'm trying to write a function that supports joining data with 0 rows, but I get this error:

a<-tibble("key", "value", .rows=0)
b<-tibble(key = c("test1","test2"), value=c("result1","result2"))
full_join(a, b, by="key") # Bombs

# Error: `by` can't contain join column `key` which is missing from LHS

merge(a, b, by="key") # Also bombs 

# Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column

Desired output is:

# A tibble: 2 x 3
  key   value.x value.y
  <chr> <chr>   <chr>  
1 test1 NA      result1
2 test2 NA      result2

Is there a better way to do this? Or any ideas for a workaround without a bunch of conditional logic using nrows()==0?

Upvotes: 2

Views: 253

Answers (1)

A. Suliman
A. Suliman

Reputation: 13125

We can define an empty tibble using character(), integer() etc ...

library(tibble)
a<-tibble(key=character(), value=character())
b<-tibble(key=c("test1","test2"), value=c("result1","result2"))

full_join(a, b, by='key') 

We got this Error

Error: by can't contain join column key which is missing from LHS

Because a<-tibble("key", "value", .rows=0) returns a with a column named "key" not key hence full_join will fail.

Upvotes: 2

Related Questions