简体   繁体   English

对列表元素应用过滤器 function

[英]Apply filter function on list elements

I would like to subset my data (lists within a list) with the filter function, where I want to filter based on a list with selected dates.我想使用过滤器 function 对我的数据(列表中的列表)进行子集化,我想根据带有选定日期的列表进行过滤。 Applied for one single sublist, it works well, but the upscaling into a for loop for the whole data set causes problems either regarding the indexing or the automatically adding of the sublists to the main list (output is just the last sublist).应用于单个子列表,它工作得很好,但是升级到整个数据集的 for 循环会导致关于索引或子列表自动添加到主列表的问题(输出只是最后一个子列表)。

The filtering for one list element:一个列表元素的过滤:


library(dplyr)
sublist_1 <- data.frame("value" = rnorm(10), "date" = 1:10, "parameter" = "X")
sublist_2 <- data.frame("value" = rnorm(10), "date" = 1:10, "parameter" = "Y"))
selected_dates_X <- c(3, 5, 7)
selected_dates_Y <- c(4, 6, 8)
df_total <-list("sublist_1" = sublist_1, "sublist_2" = sublist_2)


filtered_sublist_1 <- filter(df_total$sublist_2, 
                             date %in% selected_dates_Y)

My unsuccessful try to apply this in for loop, with an added if-statement.我不成功地尝试在 for 循环中应用它,并添加了一个 if 语句。 I want to create two lists, in which the filtered data is added for parameter X and Y, respectively.我想创建两个列表,分别为参数 X 和 Y 添加过滤后的数据。

df_X <- list()
df_Y <- list()

for (i in df_total) {
  if (i$parameter %in% "X") {
  filtered_sublist_X <- filter(df_total[[i]],
              date %in% selected_dates_X) 
  df_X[[length(df_X) + 1]] <- filtered_sublist_X
  
  } else {
    filtered_sublist_Y <- filter(df_total[[i]],
              date %in% selected_dates_Y) 
  df_Y[[length(df_Y) + 1]] <- filtered_sublist_Y
  }
}

As output, I want two datasets, filtered by selected_date for each parameter X and Y. Maybe this is not an appropriate approach to do this, then feel free to suggest other solutions, maybe with a function and lapply()?作为 output,我想要两个数据集,按每个参数 X 和 Y 的 selected_date 过滤。也许这不是一个合适的方法,然后随意提出其他解决方案,也许使用 function 和 lapply()? (Tried it, but also doesn't work) (试过了,还是不行)

Thanks!谢谢!

You can try this lapply approach -您可以尝试这种lapply方法 -

filtered_sublist_X <- lapply(df_total, function(x) 
            subset(x, parameter %in% "X" & date %in% selected_dates_X))

filtered_sublist_Y <- lapply(df_total, function(x) 
            subset(x, parameter %in% "Y" & date %in% selected_dates_Y))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM