2
我有一個長格式的數據框,其中有兩個因子變量作爲列,第一列代表一系列唯一類,第二列代表分組變量(在這種情況下,班級發生的城市)。從列中提取列表,對於另一列中的每個組
class <- c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K")
city <- c("Atlanta", "Atlanta", "Atlanta", "Detroit", "Detroit", "Milwaukee", "Milwaukee", "Milwaukee", "Milwaukee", "Portland", "Juneau")
samp <- data.frame(class, city)
samp
# class city
# A Atlanta
# B Atlanta
# C Atlanta
# D Detroit
# E Detroit
# F Milwaukee
# G Milwaukee
# H Milwaukee
# I Milwaukee
# J Portland
# K Juneau
對於每個城市,我想要列出所有在那裏發生的課程。理想情況下,輸出將如下所示:
class.list <- list(Atlanta = c("A", "B", "C"), Detroit = c("D", "E"), Milwaukee = c("F", "G", "H", "I"), Portland = "J", Juneau = "K")
class.list
# $Atlanta
# [1] "A" "B" "C"
#
# $Detroit
# [1] "D" "E"
#
# $Milwaukee
# [1] "F" "G" "H" "I"
#
# $Portland
# [1] "J"
#
# $Juneau
# [1] "K"
我試過各種解決方案並失敗。可以說我來最接近的是通過dcast
,但它是不是很我的目標是爲輸出:
library(reshape2)
class.list <-dcast(samp, city ~ class)
class.list
# place A B C D E F ...
# Atlanta Atlanta Atlanta Atlanta <NA> <NA> <NA> ...
# Detroit <NA> <NA> <NA> Detroit Detroit <NA> ...
# ...
完美。如此簡潔!感謝您的多種選擇。 – coip