0

以下のデータセットの例では、1年に要約された製品ごとのユニークな顧客の数を見つける必要があります。出力は、ヘッダー付きのdata.frameである必要があります:year-product-number of Customers

ご協力いただきありがとうございます。

year <- c("2009", "2010")
product <- c("a", "b", "c")
df <- data.frame(customer = sample(letters, 50, replace = T),
                 product = sample(product, 50, replace = T),
                 year = sample(year, 50, replace = T))
4

2 に答える 2

4

With aggregate()(included-with-R statsパッケージ内):

agdf<-aggregate(customer~product+year,df,function(x)length(unique(x)))
agdf
#  product year customer
#1       a 2009        7
#2       b 2009        8
#3       c 2009       10
#4       a 2010        7
#5       b 2010        7
#6       c 2010        6
于 2013-03-25T12:40:05.100 に答える
2

plyrの使用summarise

require(plyr)
ddply(df, .(product, year), summarise, customers=length(unique(customer)))
于 2013-03-25T12:09:57.273 に答える