如何在列表元素上进行“分组”(在Base R中)

huangapple go评论63阅读模式
英文:

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

uniquematch适用于列表,因此您可以将列表与其自身的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

huangapple
  • 本文由 发表于 2023年4月17日 23:17:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定