Moysey Abramowitz
Moysey Abramowitz

Reputation: 380

R dplyr: join within pipe

I am totally new to dplyr and am trying to use dplyr to do the following:

I have the dataframe 'tdata' and want to fill omitted periods (prd) with 'NA' within each group. I want to get the dataframe 'results'. Speed matters for me, so I hope that there is a way to do it in dplyr faster than in for loop.

> tdata <- data.frame(group = c(10, 10, 10, 11, 11), prd = c(1, 2, 5, 3, 5), value = c(2,7,3,6,2))
> tdata
  group prd value
1    10   1     2
2    10   2     7
3    10   5     3
4    11   3     6
5    11   5     2
> result <- data.frame(group = c(10, 10, 10, 10, 10, 11, 11, 11), prd = c(1, 2, 3, 4, 5, 3, 4, 5), value = c(2, 7, 'NA', 'NA', 3, 6, 'NA', 2))
> result
  group prd value
1    10   1     2
2    10   2     7
3    10   3    NA
4    10   4    NA
5    10   5     3
6    11   3     6
7    11   4    NA
8    11   5     2

I tried to use pipes and got this error:

> fdata <- tdata %>%
+   group_by(group) %>%
+   arrange(prd) %>%
+   left_join(data.frame(prd_v=min(prd):max(prd)), ., by=c("prd_v" = "prd"))
Error in data.frame(prd_v = min(prd):max(prd)) : object 'prd' not found

UPDATE: Additionally, I want to use this pipe inside the larger function, so I would like to have

period_variable <- "prd"

and then

tdata2 <- ndata %>%
  group_by(group) %>%
  complete(period_variable = full_seq(period_variable), period = 1) %>%
  ungroup()
tdata2

But it does not work. I tried to play with get(), parse(), eval(), as.name(), as.symbol(), UQ(), !!, sym() but it still does not work.

Upvotes: 2

Views: 2310

Answers (2)

Jos&#233;
Jos&#233;

Reputation: 931

As for the second question, I don't know if this is what you want, but I would do something like this:

prd = c(1, 2, 5, 3, 5)
period_variable <- quote(prd)

tdata2 <- tdata %>%
dplyr::group_by(group) %>%
tidyr::complete(prd= tidyr::full_seq(eval(period_variable), period = 1)) %>%
dplyr::ungroup()

Upvotes: 0

www
www

Reputation: 39154

We can use the complete function from the tidyr package.

library(dplyr)
library(tidyr)

tdata2 <- tdata %>%
  group_by(group) %>%
  complete(prd = full_seq(prd, period = 1)) %>%
  ungroup()
tdata2
# # A tibble: 8 x 3
#   group   prd value
#   <dbl> <dbl> <dbl>
# 1    10     1     2
# 2    10     2     7
# 3    10     3    NA
# 4    10     4    NA
# 5    10     5     3
# 6    11     3     6
# 7    11     4    NA
# 8    11     5     2

Upvotes: 4

Related Questions