[R] R help..subsetting data frame that meeting multiple criteria

Rui Barradas
Sat, 24 Nov 2012 00:46:24 -0000

Hello,

Like the following?

dat <- read.table(text="
value class percent
15526 36 4.6875
15527 62 85.9375
15527 82 32.4564
15528 36 70.3125
15528 62 9.375
15528 82 74.6875
", header = TRUE)

per70 <- dat$percent > 70
split(dat, list(dat$class, per70))

Hope this helps,

Rui Barradas
Em 23-11-2012 21:14, prasmas escreveu:
> Hi,
> I am new to R.  I am trying to regroup data frame using multiple
constrains.
> for example
>
> data frame: data
>    value	class	percent
> 15526	36	4.6875
> 15527	62	85.9375
> 15527	82	32.4564
> 15528	36	70.3125
> 15528	62	9.375
> 15528	82	74.6875
>
> I need to regroup each class that have greater than or equal to 70 percent
> into new group. Similarly, I also need to regroup each class that have less
> than 70 percent into new group.
>
> I can do this by using following syntax for each class
> class36<- data[data$class==36&data$percent>70,]
> class36a<- data[data$class==36&data$percent<=70,]
> but I have 100 different classes. In order to do this for all 100 classes,
I
> have write that syntax 100 times. There would be some way to do dynamically
> to regroup for 100 classes (may be using for loop) but I dont know. Can you
> please help in this.
> Output should be like
> data frame: class36
> value	class	percent
> 15528	36	70.3125
>
> data frame: class36a
> value	class	percent
> 15526	36	4.6875
>
> data frame: class62
> 15527	62	85.9375
>
> data frame: class62a
> 15528	62	9.375
>
> data frame: class82
> 15528	82	74.6875
>
> data frame: class82a
> 15527	82	32.4564