[R] group data based on row value

3 views
Skip to first unread message

Ye Lin

unread,
May 22, 2013, 5:40:48 PM5/22/13
to R help
hey, I want to divide my data into three groups based on the value in one
column with group name.

dat:

Var
0
0.2
0.5
1
4
6

I tried:

dat <- cbind(dat, group=cut(dat$Var, breaks=c(0.1,0.6)))

But it doesnt work, I want to group those <0.1 as group A, 0.1-0.6 as group
B, >0.6 as group C

Thanks for your help!

[[alternative HTML version deleted]]

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

arun

unread,
May 22, 2013, 6:06:05 PM5/22/13
to Ye Lin, R help
Hi,
Try:
dat<- read.table(text="
Var
0
0.2
0.5
1
4
6
",sep="",header=TRUE)

res1<-within(dat,group<-factor(findInterval(Var,c(-Inf,0.1,0.6),rightmost.closed=TRUE),labels=LETTERS[1:3]))
 res1
 # Var group
#1 0.0     A
#2 0.2     B
#3 0.5     B
#4 1.0     C
#5 4.0     C
#6 6.0     C

#or

res2<-within(dat,group<-factor(cut(Var,breaks=c(-Inf,0.1,0.6,Inf)),labels=LETTERS[1:3]))
 identical(res1,res2)
#[1] TRUE
A.K.


>hey, I want to divide my data into three groups based on the value in one
>column with group name.
>
>dat:
>
>Var
>0
>0.2
>0.5
>1
>4
>6
>
>I tried:
>
>dat <- cbind(dat, group=cut(dat$Var, breaks=c(0.1,0.6)))
>
>But it doesnt work, I want to group those <0.1 as group A, 0.1-0.6 as group
>B, >0.6 as group C
>
>Thanks for your help!

Jeff Newmiller

unread,
May 22, 2013, 6:26:44 PM5/22/13
to Ye Lin, R help
dat$group <- cut( dat$Var, breaks=c(-Inf,0.1, 0.6,Inf))
levels(dat$group) <- LETTERS[1:3]

---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdne...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

David Carlson

unread,
May 23, 2013, 11:11:20 AM5/23/13
to Ye Lin, R help
The OP indicated that the middle group should be closed on both ends, i.e.
[0.1, 0.6].

> dat2 <- rbind(dat, 0.1, 0.6)
> dat2$group <- factor(ifelse(dat2$Var<.1, "A", ifelse(dat2$Var>.6, "C",
"B")))
> dat2
Var group
1 0.0 A
2 0.2 B
3 0.5 B
4 1.0 C
5 4.0 C
6 6.0 C
7 0.1 B
8 0.6 B

Does it but would be clumsy for more than three groups. Depending on the
precision of the numbers something like

> dat2$group <- cut( dat2$Var, breaks=c(-Inf, 0.1-.0001, 0.6+.0001, Inf),
labels=LETTERS[1:3])
> dat2
Var group
1 0.0 A
2 0.2 B
3 0.5 B
4 1.0 C
5 4.0 C
6 6.0 C
7 0.1 B
8 0.6 B

would also work.

-------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352
Reply all
Reply to author
Forward
0 new messages