2

I have the following data set:

dat<-structure(list(X1979 = c(1.26884, 0.75802, 0.35127, -0.0679517, 
-4.34841, -0.312289, -5.02931, -2.49339, -12.9065, -2.90853, 
-1.02833, 0.333109, 1.70236, -2.44456, -1.83307, -0.982637, -2.14197, 
-4.1294, -3.98545, -6.26205, -5.56162, 0.0789091, 1.63146, -0.214938 
), X1980 = c(-1.32651, -0.0199441, -1.08583, 3.25939, 0.0402712, 
-3.22174, -0.859756, -3.30898, 1.0128, 0.847161, 2.75866, 1.93117, 
1.05851, 1.83372, -0.811736, -0.992584, -0.110012, 0.132343, 
2.21745, -1.48902, 0.111302, -3.77058, -3.65044, -2.41263)), class = 
"data.frame", row.names = 50:73)

I would like to Apply the following function per column in the above data:

  library(dplyr)
  library(tibble)
  library(zoo)


  test <- function(x){ 
  dat %>%
  rownames_to_column() %>%
  filter(V1 > 0 &
   rollsum(V1 > 0, 4, fill = NA, align = 
  "left") >= 3 &
   rollsum(V1, 4, fill = NA, align = 
  "left") > 1) %>%
  return(slice(1))
  }

 test(dat)

I encountered an error saying V1 not found in the data. So my question is how do I correct this function so that it can read the values in a column regardless of the header name?

I'll appreciate any help on this.

Tung
  • 26,371
  • 7
  • 91
  • 115
Lyndz
  • 347
  • 1
  • 13
  • 30
  • Does this answer your question? [Programming with dplyr using string as input](https://stackoverflow.com/questions/44121728/programming-with-dplyr-using-string-as-input) – NelsonGon May 05 '20 at 15:33
  • 3
    You need `!!sym` or recently `{{}}` I guess. However if you want to do it on one dataset, would a function be necessary(considering the names are got from internally)? – NelsonGon May 05 '20 at 15:34
  • 1
    in `dat` data frame your column names are `X1979` and `X1980`, try changing that and using NSE mentioned above if you want to specify the column selection – EJJ May 05 '20 at 15:35
  • 1
    @NelsonGon I am not sure if I got you correctly, you mean !!V1? the column names are X1979...X2019 for the whole data that I am applying this. – Lyndz May 05 '20 at 15:37

2 Answers2

4

You need to use tidy evaluation. More info here:

library(zoo)
library(rlang)
library(tidyverse)

dat <- structure(list(X1979 = c(1.26884, 0.75802, 0.35127, -0.0679517, 
                              -4.34841, -0.312289, -5.02931, -2.49339, -12.9065, -2.90853, 
                              -1.02833, 0.333109, 1.70236, -2.44456, -1.83307, -0.982637, -2.14197, 
                              -4.1294, -3.98545, -6.26205, -5.56162, 0.0789091, 1.63146, -0.214938 
), X1980 = c(-1.32651, -0.0199441, -1.08583, 3.25939, 0.0402712, 
             -3.22174, -0.859756, -3.30898, 1.0128, 0.847161, 2.75866, 1.93117, 
             1.05851, 1.83372, -0.811736, -0.992584, -0.110012, 0.132343, 
             2.21745, -1.48902, 0.111302, -3.77058, -3.65044, -2.41263)), class = 
  "data.frame", row.names = 50:73)

Use curly-curly {{}}

test <- function(dat, column_name){ 
  dat %>%
    rownames_to_column() %>%
    filter({{column_name}} > 0 &
             rollsum({{column_name}} > 0, 4, fill = NA, align = 
                       "left") >= 3 &
             rollsum({{column_name}}, 4, fill = NA, align = 
                       "left") > 1) %>%
    slice(1) -> result
    return(result)
}

test(dat, X1979)
#>   rowname  X1979   X1980
#> 1      50 1.2688 -1.3265

Use .data[[]] pronoun

test2 <- function(dat, column_name){ 
  dat %>%
    rownames_to_column() %>%
    filter(.data[[column_name]] > 0 &
             rollsum(.data[[column_name]] > 0, 4, fill = NA, align = 
                       "left") >= 3 &
             rollsum(.data[[column_name]], 4, fill = NA, align = 
                       "left") > 1) %>%
    slice(1) -> result
  return(result)
}

out <- colnames(dat) %>% 
  set_names %>% 
  map_dfr(~ test2(dat, .x), .id = 'Col_ID')
out
#>   Col_ID rowname    X1979   X1980
#> 1  X1979      50   1.2688 -1.3265
#> 2  X1980      58 -12.9065  1.0128

Created on 2020-05-05 by the reprex package (v0.3.0)

Tung
  • 26,371
  • 7
  • 91
  • 115
2

Just to demonstrate what @NelsonGon mentioned in the link re non-standard evaluation see the code below.

# if passing a string
test <- function(x) {
  my_v <- rlang::sym(x)

  out <- dat %>% 
    rownames_to_column() %>%
    filter(!!my_v > 0 &
             rollsum(!!my_v > 0, 4, fill = NA, align = 
                       "left") >= 3 &
             rollsum(!!my_v, 4, fill = NA, align = 
                       "left") > 1) %>% 
    slice(1)
    return(out)
}
test("X1979")

# if passing expression
test <- function(x) {
  my_v <- rlang::enquo(x)

  out <- dat %>% 
    rownames_to_column() %>%
    filter(!!my_v > 0 &
             rollsum(!!my_v > 0, 4, fill = NA, align = 
                       "left") >= 3 &
             rollsum(!!my_v, 4, fill = NA, align = 
                       "left") > 1) %>% 
    slice(1)
  return(out)
}
test(X1979)

But it would be likely best to also pass the data.frame as an argument in the function, calling it z.

test3 <- function(z, x) {
  my_v <- rlang::sym(x)

  out <- z %>% 
    rownames_to_column() %>%
    filter(!!my_v > 0 &
             rollsum(!!my_v > 0, 4, fill = NA, align = 
                       "left") >= 3 &
             rollsum(!!my_v, 4, fill = NA, align = 
                       "left") > 1) %>% 
    slice(1)
  return(out)
}
test3(dat, "X1979")
EJJ
  • 1,474
  • 10
  • 17
  • 1
    @EJJ..I got this. Thank you also for the help! – Lyndz May 05 '20 at 15:55
  • 2
    Just a note that it's generally preferable to use `.data[[string]]` than `sym()` / `ensym()` with `!!`. The `.data` pronoun always throws an error when the column doesn't exist. With a symbol created with sym, R will try to find a matching symbol in the environment if the column doesn't exist, which might or might not fail. – Lionel Henry May 06 '20 at 07:25
  • Thanks for the note! Still trying to fully understand NSE – EJJ May 07 '20 at 18:10