R中的数据操作和子集化

时间:2014-07-24 09:01:59

标签: r subset

数据框tab1中的一列如下:

PossessionPct
51
49
42.7
57.3
54
46
57.5
42.5
55.6
44.4
60.6
39.4
51
40.2

如何从此列中提取并根据值将数据分类为GOOD,MEDIUM和BAD?

2 个答案:

答案 0 :(得分:0)

 dat <- structure(list(PossessionPct = c(51, 49, 42.7, 57.3, 54, 46, 
 57.5, 42.5, 55.6, 44.4, 60.6, 39.4, 51, 40.2)), .Names = "PossessionPct", row.names = c(NA, 
 -14L), class = "data.frame")

 dat$Group <- with(dat, cut(PossessionPct, breaks=c(-Inf,41,51,61),
                  labels=c("Good","Medium", "Bad")))

 split(dat,dat$Group)

答案 1 :(得分:0)

另一种方法(将值替换为您自己的好/中/差阈值。

with(tab1, ifelse(PossessionPct < 45, "Good", 
                  ifelse(PossessionPct > 50, "Bad", "Medium")))