0

I have the following data:

   factor_1 <- c("0","B", "C")
    factor_2 <- c("0","BB", "CC")
    factor_3 <- c("0","BBB", "CCC", "DDD")
    factor_4 <- c("0","BBBB", "CCCC", "DDDD", "EEEE")
    factor_5 <- c("0","BBBBB", "CCCCC", "DDDDD", "EEEEE", "FFFFFF")

Suppose I have the following function:

filter_list <- function(x) {
  flist <- list()
  for (i in seq_along(x)) {
    flist <- c(flist,
      combn(x, i) |>     # matrix with cols = combinations of i elements
        data.frame() |>  # temporarily turn it into a data frame
        as.list()        # make it a list and append it to flist
    )
  }
  names(flist) <- NULL   # strip off the rather annoying item names
  flist
}

I am trying to test this function for the following case:

factor_1_sets <- filter_list(factor_1)
factor_2_sets <- filter_list(factor_2)
factor_3_sets <- filter_list(factor_3)
factor_4_sets <- filter_list(factor_4)
factor_5_sets <- filter_list(factor_5)
# For convenience, we make a list of all the lists.
factor_sets <- list(factor_1_sets, factor_2_sets, factor_3_sets, factor_4_sets, factor_5_sets)

My Question: The above function seems to have some errors relating to "unexpected symbols":

filter_list <- function(x) {
+     flist <- list()
+     for (i in seq_along(x)) {
+         flist <- c(flist,
+                    combn(x, i) |>     # matrix with cols = combinations of i elements
Error: unexpected '>' in:
"        flist <- c(flist,
                   combn(x, i) |>"
>                        data.frame() |>  # temporarily turn it into a data frame
Error: unexpected '>' in "                       data.frame() |>"
>                        as.list()        # make it a list and append it to flist
Error in typeof(x) : argument "x" is missing, with no default
>         )
Error: unexpected ')' in "        )"
>     }
Error: unexpected '}' in "    }"
>     names(flist) <- NULL   # strip off the rather annoying item names
Error in names(flist) <- NULL : object 'flist' not found
>     flist
Error: object 'flist' not found
> }
Error: unexpected '}' in "}"

I tried to remove some of the ">" symbols which might be causing this error - now the function can at least be defined without any initial errors :

filter_list <- function(x) {
    flist <- list()
    for (i in seq_along(x)) {
        flist <- c(flist,
                   combn(x, i) |    # matrix with cols = combinations of i elements
                       data.frame() |  # temporarily turn it into a data frame
                       as.list()        # make it a list and append it to flist
        )
    }
    names(flist) <- NULL   # strip off the rather annoying item names
    flist
}

Problem: However, I still can not used the function to perform the above task:

factor_1_sets <- filter_list(factor_1)

 Error in typeof(x) : argument "x" is missing, with no default 

Can someone please show me what I am doing wrong?

Thanks!

> sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                    LC_TIME=English_Canada.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] GA_3.2.1         iterators_1.0.13 foreach_1.5.1    dplyr_1.0.6     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7       rstudioapi_0.13  magrittr_2.0.1   tidyselect_1.1.0 R6_2.5.0         rlang_0.4.10     fansi_0.4.2      tools_4.0.3      xfun_0.21        tinytex_0.30     utf8_1.1.4      
[12] cli_2.5.0        DBI_1.1.1        ellipsis_0.3.2   assertthat_0.2.1 tibble_3.1.2     lifecycle_1.0.0  crayon_1.3.4     purrr_0.3.4      vctrs_0.3.8      codetools_0.2-16 glue_1.4.2      
[23] compiler_4.0.3   pillar_1.6.1     generics_0.1.0   pkgconfig_2.0.3 
user438383
  • 5,716
  • 8
  • 28
  • 43
stats_noob
  • 5,401
  • 4
  • 27
  • 83

2 Answers2

2

|> is R's brand new pipe operator since version 4.1.0.

Upgrade your R to the current version then it should work fine.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
hyena
  • 318
  • 1
  • 9
  • @ HYENA: Thank you so much for your answer! In the mean time, is there a way to change this new pipe operator such that the above function runs on an older version of R? Thank you so much! – stats_noob Dec 31 '21 at 07:04
  • 1
    just wrap everything in a function call as in `as.list(data.frame(combn(x,i)))`. – hyena Dec 31 '21 at 07:09
0

Based on the suggestion by @ HYENA, I think I figured it out! (replace |> with %>%)

filter_list <- function(x) {
  flist <- list()
  for (i in seq_along(x)) {
    flist <- c(flist,
      combn(x, i) %>%    # matrix with cols = combinations of i elements
        data.frame() %>%   # temporarily turn it into a data frame
        as.list()        # make it a list and append it to flist
    )
  }
  names(flist) <- NULL   # strip off the rather annoying item names
  flist
}


#test

 filter_list(factor_1)
[[1]]
[1] "AAAA"

[[2]]
[1] "BBBB"

[[3]]
[1] "CCCC"

#etc

[[11]]
[1] "BBBB" "DDDD"

[[12]]
[1] "BBBB" "EEEE"

#etc

[[27]]
[1] "AAAA" "BBBB" "CCCC" "EEEE"

[[28]]
[1] "AAAA" "BBBB" "DDDD" "EEEE"

[[29]]
[1] "AAAA" "CCCC" "DDDD" "EEEE"

[[30]]
[1] "BBBB" "CCCC" "DDDD" "EEEE"

[[31]]
[1] "AAAA" "BBBB" "CCCC" "DDDD" "EEEE"

Side Thoughts: Does anyone know why |> has been replaced with %>% ?

stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • Maybe %>% is not a base R thing. See https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Special-operators – hyena Dec 31 '21 at 07:19
  • `|>` has **not** been replaced by `%>%`. The former is a new base R pipe operator, the latter is contributed package `magrittr`'s pipe operator. – Rui Barradas Dec 31 '21 at 07:19
  • Thanks everyone for your replies! Just to clarify - can someone please tell me if I have correctly converted the original function into the right format? Or should I have done it a different way? Thanks! – stats_noob Dec 31 '21 at 07:20
  • Your solution looks good to me. – hyena Dec 31 '21 at 07:23
  • One more thing. I would just use plain function calls in a previous version of R because in my R environment, many packages have defined the %>% operator, including jqr, purrr, tidyr, and more. It is not clear which version will be used if not clearly specified, and if the version used is what I wanted. – hyena Dec 31 '21 at 07:33
  • Note that `|>` is faster than `%>%`, see [this answer](https://stackoverflow.com/a/67683626/6574038). Thus you may want to consider what @HYENA suggests in the comment to their [answer](https://stackoverflow.com/a/70539786/6574038), or better update R. – jay.sf Dec 31 '21 at 10:12