英文:
How to "group by" over list elements (in Base R)
问题
以下是您要翻译的内容:
Factor c(1,2) c('a', 'b') c(2,3)
Freq 1 2 1
这是您的所需输出。如果您需要进一步的翻译或帮助,请告诉我。
英文:
Code
list(c(1,2), c('a', 'b'), c('a', 'b'), c(2,3)) -> li
##
## [[1]]
## [1] 1 2
##
## [[2]]
## [1] "a" "b"
##
## [[3]]
## [1] "a" "b"
##
## [[4]]
## [1] 2 3
Q: Required output as from table().
Factor c(1,2) c('a', 'b') c(2,3)
Freq 1 2 1
It looks very elementary, but I cannot find a solution.
Edit - Unfortunately, my list contains ca 500,000 elements. Sorry for not mentioning that. A solution might be to merge the items in the list into a single element:
##
## [[1]]
## [1] "1 2"
##
## [[2]]
## [1] "a b"
##
## [[3]]
## [1] "a b"
##
## [[4]]
## [1] "2 3"
Then unlist the list and apply table.
Q: How to concatenate the list elements in a single item?
A: sapply(li, paste, collapse = "-") |> table()
答案1
得分: 5
unique
和match
适用于列表,因此您可以将列表与其自身的unique
成员进行match
。在结果上调用table
,然后剩下的就是再次使用unique
设置结果的名称。所有这些都可以用一行代码完成:
setNames(table(match(li, unique(li))), unique(li))
#> c(1, 2) c("a", "b") c(2, 3)
#> 1 2 1
或者,如果您更喜欢清晰而不是简洁,基本的R管道等效代码为:
li |>
match(unique(li)) |>
table() |>
setNames(unique(li))
#> c(1, 2) c("a", "b") c(2, 3)
#> 1 2 1
英文:
unique
and match
work with lists, so you can match
your list to unique
members of itself. Call table
on the result, then all than remains is to set the names of the result using unique
again. This can all be done with the one-liner:
setNames(table(match(li, unique(li))), unique(li))
#> c(1, 2) c("a", "b") c(2, 3)
#> 1 2 1
Or, if you prefer clarity to brevity, the base R piped equivalent would be:
li |>
match(unique(li)) |>
table() |>
setNames(unique(li))
#> c(1, 2) c("a", "b") c(2, 3)
#> 1 2 1
答案2
得分: 1
You could extract the unique elements, then count how often an element of the list is inside the list of unique elements:
# All unique list elements
uniques = li[!duplicated(li)]
# How many list elements are inside the list containing a single unique element, for each unique element
freq = lapply(uniques, function(x) length(which(li %in% list(x))))
# Set the unique elements as names
names(freq) = uniques
freq
# $`c(1, 2)`
# [1] 1
#
# $`c("a", "b")`
# [1] 2
#
# $`c(2, 3)`
# [1] 1
(Note: I've translated the code comments and corrected the quotes around "a" and "b" to be regular double quotes.)
英文:
You could extract the unique elements, than count how often an element of the list is inside the list of unique elements:
# All unique list elements
uniques = li[!duplicated(li)]
# How many list elements are inside the list containing a single unique element, for each unique element
freq = lapply(uniques, function(x) length(which(li %in% list(x))))
# Set the unique elements as names
names(freq) = uniques
freq
# $`c(1, 2)`
# [1] 1
#
# $`c("a", "b")`
# [1] 2
#
# $`c(2, 3)`
# [1] 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论