Is there a reason why this returns list() but contains nothing in it??
library(rlist)
library(tidyverse)
data(chickwts)
list(chickwts) %>%
list.select(feed,weight) %>%
list.filter(.$weight<=200)
Is there a reason why this returns list() but contains nothing in it??
library(rlist)
library(tidyverse)
data(chickwts)
list(chickwts) %>%
list.select(feed,weight) %>%
list.filter(.$weight<=200)
list()
means an empty list: nothing matched the condition.
If I understand correctly, list.filter()
can only test a condition on an entire element, not on the vector inside this element:
y <- list(list(weight=1))
list.filter(y, weight == 1)
# [[1]]
# [[1]]$weight
# [1] 1
#
y <- list(list(weight=1:2))
list.filter(y, weight == 1)
# list()
To do processing inside the element, you need list.map()
. Here are two versions of what you want:
list(chickwts) %>%
list.select(feed, weight) %>%
list.map(Filter(function(x){x <= 200}, weight))
# [[1]]
# [1] 179 160 136 168 108 124 143 140 181 141 148 169 193 199 171 158 153
list(chickwts) %>%
list.select(feed, weight) %>%
list.map(weight[weight <= 200])
# [[1]]
# [1] 179 160 136 168 108 124 143 140 181 141 148 169 193 199 171 158 153
Thank you! this is very helpful
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.