[R] data manipulation

Patrick Burns pburns at pburns.seanet.com
Fri Nov 22 18:51:24 CET 2013


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')



More information about the R-help mailing list