Created
February 8, 2017 17:49
-
-
Save infotroph/937511c33eaa2aaa98a9a321c7d4a2fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(dplyr) | |
badly_behaved_function = function(x)if(x==1){data.frame(a=1:5, b=5:1)}else{list()} | |
obtained = data.frame(id=1:3) %>% group_by(id) %>% do(res = badly_behaved_function(.$id)) | |
obtained | |
# Source: local data frame [3 x 2] | |
# Groups: <by row> | |
# | |
# # A tibble: 3 × 2 | |
# id res | |
# * <int> <list> | |
# 1 1 <data.frame [5 × 2]> | |
# 2 2 <list [0]> | |
# 3 3 <list [0]> | |
obtained %>% tidyr::unnest() | |
# Error: Each column must either be a list of vectors or a list of data frames [res] | |
# Succeeds, but note that IDs 2 and 3 are completely removed | |
obtained %>% filter(length(res) > 0) %>% tidyr::unnest() | |
# # A tibble: 5 × 3 | |
# id a b | |
# <int> <int> <int> | |
# 1 1 1 5 | |
# 2 1 2 4 | |
# 3 1 3 3 | |
# 4 1 4 2 | |
# 5 1 5 1 |
One approach: Filter and then join the result back onto the full ID list.
> left_join(obtained %>% select(id), obtained %>% filter(length(res) > 0) %>% tidyr::unnest())
Joining, by = "id"
Source: local data frame [7 x 3]
Groups: <by row>
# A tibble: 7 × 3
id a b
<int> <int> <int>
1 1 1 5
2 1 2 4
3 1 3 3
4 1 4 2
5 1 5 1
6 2 NA NA
7 3 NA NA
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Desired output: