R puzzles

Navigation

Press the right arrow key (→) to go to next puzzle. Press the down arrow key (↓) to incrementally reveal answer step by step.

Note

For all puzzles please assume that tidyverse has been loaded.

↓ Puzzle 1

What number does the following line of code return?

data.frame(
  x = 1:2,
  c = 1:2
) |>
  dplyr::filter(is.function(c)) |>
  nrow()

↓ step by step answer

Step 1

data.frame(
  x = 1:2,
  c = 1:2
)
  x c
1 1 1
2 2 2

Step 2

data.frame(
  x = 1:2,
  c = 1:2
) |>
  dplyr::filter(is.function(c))
[1] x c
<0 rows> (or 0-length row.names)

Final answer

data.frame(
  x = 1:2,
  c = 1:2
) |>
  dplyr::filter(is.function(c)) |>
  nrow()
[1] 0

↓ Puzzle 2

What number does the following line of code return?

data.frame(
  x = 1:2,
  c = 1:2
) |>
  filter(is.function(!!c)) |>
  nrow()

↓ step by step answer

Step 1

data.frame(
  x = 1:2,
  c = 1:2
)
  x c
1 1 1
2 2 2

Step 2

data.frame(
  x = 1:2,
  c = 1:2
) |>
  filter(is.function(!!c))
  x c
1 1 1
2 2 2

Final answer

data.frame(
  x = 1:2,
  c = 1:2
) |>
  filter(is.function(!!c)) |>
  nrow()
[1] 2

↓ Puzzle 3

What number does the following line of code return?

tibble(
  f = c(list(\(x) x + 1), list(\(x) x + 2)),
  y = c(1,1)
) |>
  rowwise() |>
  mutate(g = list(\(x) f(x) + 1), x = g(y)) |>
  pull(x) |>
  sum()

↓ step by step answer

Step 1

tibble(
  f = c(list(\(x) x + 1), list(\(x) x + 2)),
  y = c(1,1)
)
# A tibble: 2 × 2
  f          y
  <list> <dbl>
1 <fn>       1
2 <fn>       1

Step 2

tibble(
  f = c(list(\(x) x + 1), list(\(x) x + 2)),
  y = c(1,1)
) |>
  rowwise()
# A tibble: 2 × 2
# Rowwise: 
  f          y
  <list> <dbl>
1 <fn>       1
2 <fn>       1

Step 3

tibble(
  f = c(list(\(x) x + 1), list(\(x) x + 2)),
  y = c(1,1)
) |>
  rowwise() |>
  mutate(g = list(\(x) f(x) + 1), x = g(y))
# A tibble: 2 × 4
# Rowwise: 
  f          y g          x
  <list> <dbl> <list> <dbl>
1 <fn>       1 <fn>       3
2 <fn>       1 <fn>       4

Step 4

tibble(
  f = c(list(\(x) x + 1), list(\(x) x + 2)),
  y = c(1,1)
) |>
  rowwise() |>
  mutate(g = list(\(x) f(x) + 1), x = g(y)) |>
  pull(x)
[1] 3 4

Final answer

tibble(
  f = c(list(\(x) x + 1), list(\(x) x + 2)),
  y = c(1,1)
) |>
  rowwise() |>
  mutate(g = list(\(x) f(x) + 1), x = g(y)) |>
  pull(x) |>
  sum()
[1] 7

↓ Puzzle 4

What number does the following line of code return?

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise() |>
  mutate(y = list(get(x))) |> 
  arrange(o) |>
  pull(y) |>
  paste0(collapse = '') |>
  parse(text=_) |>
  eval() |>
  sum()

↓ step by step answer

Step 1

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
)
  o  c data mean    sum    x
1 1 (4    a    + 5*2,1)    c
2 2 (3    b    + 5*2,1) mean
3 0 (2    c    + 5*2,1) data
4 4 (1    d    + 5*2,1)  sum

Step 2

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise()
# A tibble: 4 × 6
# Rowwise: 
      o c     data  mean  sum    x    
  <dbl> <chr> <chr> <chr> <chr>  <chr>
1     1 (4    a     +     5*2,1) c    
2     2 (3    b     +     5*2,1) mean 
3     0 (2    c     +     5*2,1) data 
4     4 (1    d     +     5*2,1) sum  

Step 3

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise() |>
  mutate(y = list(get(x)))
# A tibble: 4 × 7
# Rowwise: 
      o c     data  mean  sum    x     y        
  <dbl> <chr> <chr> <chr> <chr>  <chr> <list>   
1     1 (4    a     +     5*2,1) c     <chr [1]>
2     2 (3    b     +     5*2,1) mean  <chr [1]>
3     0 (2    c     +     5*2,1) data  <chr [1]>
4     4 (1    d     +     5*2,1) sum   <chr [1]>

Step 4

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise() |>
  mutate(y = list(get(x))) |> 
  arrange(o)
# A tibble: 4 × 7
# Rowwise: 
      o c     data  mean  sum    x     y        
  <dbl> <chr> <chr> <chr> <chr>  <chr> <list>   
1     0 (2    c     +     5*2,1) data  <chr [1]>
2     1 (4    a     +     5*2,1) c     <chr [1]>
3     2 (3    b     +     5*2,1) mean  <chr [1]>
4     4 (1    d     +     5*2,1) sum   <chr [1]>

Step 5

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise() |>
  mutate(y = list(get(x))) |> 
  arrange(o) |>
  pull(y)
[[1]]
[1] "c"

[[2]]
[1] "(4"

[[3]]
[1] "+"

[[4]]
[1] "5*2,1)"

Step 6

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise() |>
  mutate(y = list(get(x))) |> 
  arrange(o) |>
  pull(y) |>
  paste0(collapse = '')
[1] "c(4+5*2,1)"

Step 7

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise() |>
  mutate(y = list(get(x))) |> 
  arrange(o) |>
  pull(y) |>
  paste0(collapse = '') |>
  parse(text=_)
expression(c(4 + 5 * 2, 1))

Step 8

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise() |>
  mutate(y = list(get(x))) |> 
  arrange(o) |>
  pull(y) |>
  paste0(collapse = '') |>
  parse(text=_) |>
  eval()
[1] 14  1

Final answer

data.frame(
  o = c(1,2,0,4),
  c = paste0('(', 4:1),
  data = tolower(LETTERS[1:4]),
  mean = rep('+',4),
  sum = rep('5*2,1)',4),
  x = c('c', 'mean','data', 'sum')
) |>
  rowwise() |>
  mutate(y = list(get(x))) |> 
  arrange(o) |>
  pull(y) |>
  paste0(collapse = '') |>
  parse(text=_) |>
  eval() |>
  sum()
[1] 15