rlist package - just trying to open a list

Hi,

Hopefully this is a simple question - I've been exploring the "rlist" package. I like that it has filter, sort, join etc but using lists. I'm not very comfortable with lists in general and when I started running the below, it wouldn't give me any error but it would just simply say "list()". I'm assuming it is returning a list object but how do I open it?

library(PipeR)
library(rlist)

A = list(NAME = c("A","B","C"),
          PAY = c("100","200","300"))
A%>>%
list.filter(A, PAY >= 100)%>>%
list.select(NAME)

If you look at the example if list.filter, you'll see that rlist is made to be used on more nested list, which a form of some elements, with the same field that you want to filter out. The result is one of the element of the primary list, based on a criteria on the nested list component. I am not sure it is useful in your current situation.

You could use this form


A = list(
  e1 = list(NAME = "A", PAY = "100"), 
  e2 = list(NAME = "B", PAY = "200"),
  e3 = list(NAME = "C", PAY = "300")
)

library(pipeR)
library(rlist)
A %>>%
  list.filter(PAY >= 200) %>>%
  list.select(NAME)
#> $e2
#> $e2$NAME
#> [1] "B"
#> 
#> 
#> $e3
#> $e3$NAME
#> [1] "C"

try purrr::transpose to pass from the two form of lists

Currently, you are using a list that you could put inside a data.frame


A = list(NAME = c("A","B","C"),
         PAY = c("100","200","300"))

library(dplyr)
library(pipeR)
A %>>%
  as_tibble() %>>%
  filter(PAY >= 200) %>>%
  pull(NAME)
#> [1] "B" "C"

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

You can also use base R

A = list(NAME = c("A","B","C"),
         PAY = c("100","200","300"))

A$NAME[A$PAY>=200]
#> [1] "B" "C"

Hope it helps

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.