假設我有以下串列:
test<-list(c("a","b","c"),c("a,c"),c("a,c"),c("a","b","c"),c("a"))
我想要這樣的結果:
("a","b","c") = 2
("a,c")=2
("a")=1
我試過
> table(test)
Error in table(test) : all arguments must have the same length
uj5u.com熱心網友回復:
這是一個單線。上面一行數字是向量的名稱。
table(match(test, unique(test)))
#1 2 3
#2 2 1
編輯
@Ritchie Sacramento 在評論中的一個建議是將上面輸出的名稱更改為
setNames(table(match(test, unique(test))), sapply(unique(test), toString))
#a, b, c a,c a
# 2 2 1
uj5u.com熱心網友回復:
這有什么錯sapply和toString:
> table(sapply(test, toString))
a a, b, c a,c
1 2 2
>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/325737.html
標籤:r
