如果我有一个列表列表,如:
pets = list(list("Shoji", "cat", 18), list("Hanako", "dog", 15), list("Sachiko", "alligator", 7), list("Toby", "dog", "12"))
如何将动物的名称放入不同的动物类别,并将相同的动物分类到相同的类别?
$cat
[1] "Shoji"
$dog
[1] "Hanako" "Toby"
$alligator
[1] "Sachiko"
答案 0 :(得分:0)
您可以使用tapply
:
tapply(X = sapply(pets, `[[`, 1),
INDEX = sapply(pets, `[[`, 2),
FUN = paste)
# $alligator
# [1] "Sachiko"
#
# $cat
# [1] "Shoji"
#
# $dog
# [1] "Hanako" "Toby"