[R] Creating a vector based on lookup function

Charles C. Berry cberry at tajo.ucsd.edu
Wed Nov 26 21:30:31 CET 2008


On Wed, 26 Nov 2008, PDXRugger wrote:

>
> I am still searching for a solution to what i think is a simple problem i am
> having with building a vector in a for loop.  I have built a more
> understandable example so hopefully that will help..help you, help me, if
> you know what i mean.
>
> dev=400
> #test location model TAZs to reference
> cands=c(101,105,109)
>
> #Create Object of length of cands
> candslength=length(cands)
>
> #TEST TAZ Vector
> CANDS=(100:110)
>
> #Test Dev Vector
> TAZDEVS=c(120,220,320,420,520,620,720,820,920,1020,1120)
>
> #Creates datframe of CANDS and DEVS
> TAZLIST=data.frame(CANDS,TAZDEVS)
>
> for(i in 1:candslength){
>
> 	cand=cands[i]
>
> 	#Creates an object based on the value of "cands" in TAZLIST
> 	TAZDet=TAZLIST[TAZLIST$CANDS==cand[i],2]
>
> 	}
>
> What i would like to see is TAZDet filled with the "cands" corresponding
> values found in "TAZLIST"'s second column TAZDEVS, they would be
> 120,520,920.

No.

 	101 is the second element of CANDS
 	220 is the second element of TAZDEVS

So, 120 is not 'corresponding' in any sense I know of. et cetera.


  So if things worked the way i would like them TAZDet would be
> a vector containing these three values (102,520,920).  At this point it
> gives me NAs.

Which happens because you have an obvious bug. What is

 	TAZLIST$CANDS==cand[3]
??



> Any help would be desirable, Thanks guys and gals.

Is this what you seek?

 	with(TAZLIST,TAZDEVS[match(cands,CANDS)])

HTH,

Chuck


>
>
> Cheers,
> JR
> -- 
> View this message in context: http://www.nabble.com/Creating-a-vector-based-on-lookup-function-tp20706588p20706588.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list