我有以下数据:
len = 1000
vint1 = vint1=sample(1:150,len,replace=TRUE)
vch1=sample(LETTERS[1:5],len,replace=TRUE)
vbin1=sample(letters[1:2],len,replace=TRUE)
mydf = data.frame(vint1, vch1, vbin1)
但是我必须再创建一列“类别”,其中应具有以下规则的条目:
'N' if < 90th percentile
'cat1' if >=90th and <95th percentile
'cat2' if >=95th and <99th percentile
'cat3' if >99th percentile
始终检查该vch1和vbin1的百分位数。
我可以通过以下代码确定该组vch1和vch2的vint1值是否> 90%。
with(mydf, ave(vint1, vch1, vbin1, FUN=function(x) x>quantile(x,0.9)))
但是如何分类?
编辑:
我尝试了以下代码。想确认是否可以或是否有更好的方法:
with(mydf, ave(vint1, vch1, vbin1, FUN=function(x)
ifelse(x<quantile(x,0.9), 'N',
ifelse(x<quantile(x,0.95),'cat1',
ifelse(x<(quantile(x,0.99)),'cat2','cat3'
)))
)
)
这是来自以下问题的后续问题:按R中的百分比对数据帧进行分类
这可能会有所帮助。使用group_by
,ntile
在dplyr
和你的ifelse
说法,我想出了以下内容。
library(dplyr)
group_by(mydf, vch1, vbin1) %>%
mutate(check = ntile(vint1, 100),
out = ifelse(check > 99, "cat3",
ifelse(between(check, 95, 99), "cat2",
ifelse(between(check, 90, 95), "cat1", "N")))) %>%
ungroup()
# A part of the outcome
# vint1 vch1 vbin1 check out
#1 138 C b 88 N
#2 66 B a 39 N
#3 24 D a 16 N
#4 141 B a 90 cat1
#5 27 C a 13 N
#6 29 C a 16 N
#7 11 D b 4 N
#8 24 B b 21 N
#9 72 E a 46 N
#10 25 C b 15 N
主意
transform(mydf,
check = ave(vint1, vch1, vbin1, FUN=function(x){
ifelse(x<quantile(x,0.9), 'N',
ifelse(x<quantile(x,0.95),'cat1',
ifelse(x<(quantile(x,0.99)),'cat2','cat3'
)))
})
)
# vint1 vch1 vbin1 check
#1 90 D b N
#2 136 C b cat1
#3 55 B a N
#4 56 B b N
#5 56 D a N
#6 100 A b N
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句