[英]Sorting continuous variables by dummy categories in R
我有一個income
變量,其中包含有關races
變量的收入信息,其中races=1
是White,而races=2
是Black。 我試圖找出一種方法來查看我的數據集中有多少黑人超過316000。我知道如何通過簡單地在Stata中做到這一點
sort races income
by races: count if income>316000
但是,我在R中苦苦掙扎。
x<-table(income,races)
x[(x>316000) if races==2]
但收到錯誤消息。
嘗試這個
x[x$income > 316000 & x$races == 2,]
在R中,您很少(也許永遠不需要)對數據進行排序。 考慮類似:
table(races[income > 316000])
假設您的數據框名為df
,則還有其他可能性:
df <- data.frame(income = c(316000, 316000, 316000, 316000, 316001, 316001),
race = c(1, 1, 1, 2, 2, 2))
df
# income race
# 1 316000 1
# 2 316000 1
# 3 316000 1
# 4 316000 2
# 5 316001 2
# 6 316001 2
with(df, sum(income[race == 2] > 316000))
# [1] 2
# or
with(df, sum(income > 316000 & race == 2))
# [1] 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.