[英]How do you map every combination of categorical variables in R?
使用R,是否有辦法獲取數據集並繪制出每個分類變量的每種可能組合?
例如,假設我有一個來自在線商店的10,000行客戶數據。 我想找出哪些購物者花錢最多。 我有來自客戶的以下數據:
電子郵件(yahoo,gmail,hotmail,aol)瀏覽器(Mozilla,IE,Chrome,Opera)國家(美國,加拿大,中國,澳大利亞,埃及,韓國,巴西)每個用戶在我的商店花了多少錢。
我想找出以上三個類別變量的所有可能組合,並查看每個客戶的平均支出是多少。
在R中執行此操作的好方法是什么? (注意:我的R經驗現在是平均水平)
謝謝。
您可以使用aggregate
:
set.seed(144)
dat = data.frame(email=sample(c("yahoo", "gmail"), 10000, replace=T),
browser=sample(c("mozilla", "ie"), 10000, replace=T),
country=sample(c("usa", "canada"), 10000, replace=T),
money=runif(10000))
aggregate(dat$money, by=list(browser=dat$browser, email=dat$email,
country=dat$country), mean)
# browser email country x
# 1 ie gmail canada 0.4905588
# 2 mozilla gmail canada 0.5064342
# 3 ie yahoo canada 0.4894398
# 4 mozilla yahoo canada 0.4959031
# 5 ie gmail usa 0.5069363
# 6 mozilla gmail usa 0.5088138
# 7 ie yahoo usa 0.4957478
# 8 mozilla yahoo usa 0.4993698
要獲得多個均值和計數之類的列,可以執行以下操作:
res = aggregate(dat$money, by=list(browser=dat$browser, email=dat$email,
country=dat$country),
FUN=function(x) c(mean=mean(x), count=length(x)))
res
# browser email country x.mean x.count
# 1 ie gmail canada 0.4905588 1261.0000000
# 2 mozilla gmail canada 0.5064342 1227.0000000
# 3 ie yahoo canada 0.4894398 1267.0000000
# 4 mozilla yahoo canada 0.4959031 1253.0000000
# 5 ie gmail usa 0.5069363 1240.0000000
# 6 mozilla gmail usa 0.5088138 1236.0000000
# 7 ie yahoo usa 0.4957478 1213.0000000
# 8 mozilla yahoo usa 0.4993698 1303.0000000
這就是了:
expand.grid(gender = c("male", "female"), tShirtSize = c("xs", "s","m","l","xl"))
將返回數據幀中的所有組合。 對於摘要統計信息,請嘗試aggregate
,例如:
country = sample(c("america", "canadian"), 30, replace = TRUE)
gender = sample(c("male", "female"), 30, replace = TRUE)
x = abs(rnorm(30) * 1000)
aggregate(data.frame(x), by = list(country, gender), FUN = mean)
如果數據框中存在帶有字符串的列,則會遇到錯誤,因此我會將帶有數字值的列子集化。
這是一種利用dplyr
的方法
require(magrittr)
require(dplyr)
set.seed(123)
dat = data.frame(email=sample(c("yahoo", "gmail"), 10000, replace=T),
browser=sample(c("mozilla", "ie"), 10000, replace=T),
country=sample(c("usa", "canada"), 10000, replace=T),
money=runif(10000))
dat %>%
group_by(email, browser, country) %>%
summarize(mean = mean(money))
# email browser country mean
# 1 gmail ie canada 0.5172424
# 2 gmail ie usa 0.4921908
# 3 gmail mozilla canada 0.4934892
# 4 gmail mozilla usa 0.4993923
# 5 yahoo ie canada 0.5013214
# 6 yahoo ie usa 0.5098280
# 7 yahoo mozilla canada 0.4985357
# 8 yahoo mozilla usa 0.4919743
編輯:如果要將列表傳遞到group_by()
,則需要使用非標准的評估對象regroup()
。 例如,
mylist <- list("email", "browser", "country")
dat %>%
regroup(mylist) %>%
summarize(mean = mean(money))
創建一些虛擬數據:
dataset <- data.frame(
spend=10*runif(100),
email=sample(c("yahoo","gmail","hotmail","aol"),100,replace=TRUE),
browser=sample(c("Mozilla","IE","Chrome","Opera"),100,replace=TRUE),
country=sample(c("USA","Canada","China","Australia",
"Egypt","S.Korea","Brazil"),100,replace=TRUE))
平均每個組合的spend
:
with(dataset,by(spend,list(email,browser,country),mean))
注意沒有條目的組合的NA
。
或者將其轉換為三維array
:
as.table(with(dataset,by(spend,list(email,browser,country),mean)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.