[R] data manipulation

David Carlson dcarlson at tamu.edu
Fri Nov 22 20:37:47 CET 2013


You probably want to use cut(), but as currently stated, your
intervals leave gaps (between 20 and 21 for example):

set.seed(42)
values <- runif(25)*100
values
 [1] 91.480604 93.707541 28.613953 83.044763 64.174552 51.909595
73.658831
 [8] 13.466660 65.699229 70.506478 45.774178 71.911225 93.467225
25.542882
[15] 46.229282 94.001452 97.822643 11.748736 47.499708 56.033275
90.403139
[22] 13.871017 98.889173 94.666823  8.243756
> code <- cut(values, breaks=c(-1, 20, 45, 70, 100),
labels=LETTERS[4:1])
> code
 [1] A A C A B B A D B A B A A C B A A D B B A D A A D
Levels: D C B A

The levels are defined as (-1,20], (20,45], (45,70], (70,100] so
the second interval includes anything larger than 20 up to and
including 45.

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: r-help-bounces at r-project.org
[mailto:r-help-bounces at r-project.org] On Behalf Of philippe
massicotte
Sent: Friday, November 22, 2013 12:27 PM
To: r-help at r-project.org
Subject: Re: [R] data manipulation

Hi again.
I realized that the example I give is not valid, because the
number I'm using is not an integer (ex. 50.1).
So I thought using 
is.between = function(x, a, b) {  (x - a)  *  (b - x) > 0}
But I'm not sure how to use it with lapply to avoid looping in
my code.
Regards,Phil
> From: pmassicotte at hotmail.com
> To: r-help at r-project.org
> Date: Fri, 22 Nov 2013 18:17:03 +0000
> Subject: Re: [R] data manipulation
> 
> Thank you everyone for your suggestions.
> 
> > Date: Fri, 22 Nov 2013 18:04:16 +0000
> > From: pburns at pburns.seanet.com
> > To: pmassicotte at hotmail.com; r-help at r-project.org
> > Subject: Re: [R] data manipulation
> > 
> > The final ")" went missing in the command
> > starting 'names(neutralVec) <- '.
> > 
> > On 22/11/2013 17:51, Patrick Burns wrote:
> > > I think a list is the wrong structure,
> > > a vector would be better since you can
> > > use 'match':
> > >
> > > # transform data structure:
> > > neutralVec <- unlist(neutral_classes)
> > >
> > > names(neutralVec) <-
> > > names(neutral_classes[rep(1:length(neutral_classes),
> > > sapply(neutral_classes, length))]
> > >
> > > # get one or more results with 'match':
> > > names(neutralVec[match(c(50, 20, 10, -4), neutralVec)])
> > >
> > > # result:
> > > # [1] "B" "D" "D" NA
> > >
> > >
> > > Pat
> > >
> > >
> > > On 22/11/2013 16:58, philippe massicotte wrote:
> > >> Hi everyone.
> > >> I have a list like this:
> > >> neutral_classes = list(A = 71:100, B = 46:70, C = 21:45,
D = 0:20)
> > >> and I'm trying to return the letter of the named vector
for with an
> > >> integer belong. For example, B if I use the value 50.
> > >> Any help would be greatly appreciated.
> > >> Regards,Phil
> > >>     [[alternative HTML version deleted]]
> > >>
> > >> ______________________________________________
> > >> 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.
> > >>
> > >
> > 
> > -- 
> > Patrick Burns
> > pburns at pburns.seanet.com
> > twitter: @burnsstat @portfolioprobe
> > http://www.portfolioprobe.com/blog
> > http://www.burns-stat.com
> > (home of:
> >   'Impatient R'
> >   'The R Inferno'
> >   'Tao Te Programming')
>  		 	   		  
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
 		 	   		  
	[[alternative HTML version deleted]]

______________________________________________
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