[R] Defining categories

arun smartpink111 at yahoo.com
Thu Oct 25 01:02:41 CEST 2012


Hi,
May be this:
dat1<-read.table(text="
2.880556
0.616667
5.083333
0.858333
0.466667
2.936111
4.258333
0.258333
2.033333
2.583333
1.088889
0.447222
1.872222
0.080556
4.033333
4.116667
1.633333
2.147222
",sep="",header=FALSE)
dat1$category<-ifelse(dat1$V1<=1 &dat1$V1>0,1,ifelse(dat1$V1>1 & dat1$V1<=2,2,ifelse(dat1$V1>2&dat1$V1<=3,3,ifelse(dat1$V1>3&dat1$V1<=4,4,ifelse(dat1$V1>4&dat1$V1<=5,5,6)))))


 head(dat1)
#        V1 category
#1 2.880556        3
#2 0.616667        1
#3 5.083333        6
#4 0.858333        1
#5 0.466667        1
#6 2.936111        3
A.K.



----- Original Message -----
From: bibek sharma <mbhpathak at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, October 24, 2012 6:52 PM
Subject: [R] Defining categories

Hello R user,

Data below represent year in decimal. I would like to catagorize it
in such a way that any valye [0,1] goes to catagory 1 , (1,2] goes to
catagory 2 and so on..
Any suggestion how it can be done with if else statement or any other way?

2.880556
0.616667
5.083333
0.858333
0.466667
2.936111
4.258333
0.258333
2.033333
2.583333
1.088889
0.447222
1.872222
0.080556
4.033333
4.116667
1.633333
2.147222

Thank you for  your help.
Bibek

______________________________________________
R-help at 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.





More information about the R-help mailing list