英文:
How to change a multi-level nested list into a tibble to avoid an error with filter?
问题
在一个具有三个层级的嵌套列表中,我需要到达第三个层级(即资源)并检查数据中是否存在一些特殊字符。我尝试创建了类似的数据示例,但仍然存在无法解决的差异。虽然第一级(foo)和第二级(例如,obj1)是列表,但第三级(资源)是list(s3:data.frame)。我运行了下面的代码,但如您所见,在第二部分(即筛选)时出现错误。请问如何避免这个错误?
obj1 <- list(resource = list(bodyPart = c("leg", "arm", "knee"), side = "RIGHT", device = "SENS"))
obj2 <- list(resource = list(bodyPart = c("leg", "arm", "knee"), side = "LEFT", device = "GOM"))
x <- list(foo = obj1, bar = obj2)
Dat <- lapply(x, function(tb) tb[sapply(tb, function(z) any(grepl("[^\x01-\x7F]", z), na.rm = TRUE))]) %>%
dplyr::filter(if_any(everything(), ~ grepl("[^\x01-\x7F]", .)))
Error in UseMethod("filter") :
no applicable method for 'filter' applied to an object of class "list"
英文:
In a nested list with three levels, I need to reach the third level (i.e., resources) and check whether some special characters exist in the data. I tried to make a similar sample of data but there is still a difference I could not address. While level1 (foo) and level 2 (e.g., obj1) are lists, level 3 (resource) is list(s3:data.frame). I run the code below but as you can see there is an error when it comes to the second part (i.e., filter). Could you please tell me how to avoid this error?
obj1 <- list(resource = list(bodyPart = c("leg", "arm", "knee"),side = "RIGHT", device = "SENS"))
obj2 <- list(resource = list(bodyPart = c("leg", "arm", "knee"), side = "LEFT", device = "GOM"))
x <- list(foo = obj1, bar = obj2)
Dat <- lapply(x, function(tb) tb[sapply(tb, function(z) any(grepl("[^\x01-\x7F]", z), na.rm = TRUE))]) %>%
dplyr::filter(if_any(everything(), ~ grepl("[^\x01-\x7F]", .)))
Error in UseMethod("filter") :
no applicable method for 'filter' applied to an object of class "list"
答案1
得分: 0
你可以使用 rapply
递归扫描列表节点:
x |> rapply(f = \(node) grepl("[^\x01-\x7F]", node))
生成上述的 tibble:
library(tibble)
data.frame(has_invalid_character = x |> rapply(f = \(node) grepl("[^\x01-\x7F]", node)),
content = x |> rapply(f = \(node) node)
) |>
rownames_to_column('item') |>
as_tibble()
# A tibble: 10 x 3
item has_invalid_character content
<chr> <lgl> <chr>
1 foo.resource.bodyPart1 FALSE leg
2 foo.resource.bodyPart2 FALSE arm
3 foo.resource.bodyPart3 FALSE knee
4 foo.resource.side FALSE RIGHT
5 foo.resource.device FALSE SENS
## etc.
英文:
you could recursively scan the list nodes with rapply
:
x |> rapply(f = \(node) grepl("[^\x01-\x7F]", node))
to generate a tibble from above:
library(tibble)
data.frame(has_invalid_character = x |> rapply(f = \(node) grepl("[^\x01-\x7F]", node)),
content = x |> rapply(f = \(node) node)
) |>
rownames_to_column('item') |>
as_tibble()
# A tibble: 10 x 3
item has_invalid_character content
<chr> <lgl> <chr>
1 foo.resource.bodyPart1 FALSE leg
2 foo.resource.bodyPart2 FALSE arm
3 foo.resource.bodyPart3 FALSE knee
4 foo.resource.side FALSE RIGHT
5 foo.resource.device FALSE SENS
## etc.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论