Re: [R] creating a new vecotr in a for loop

From: Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>
Date: Tue, 25 Nov 2008 20:14:13 -0500

Dear PDXRugger,
If I understand correctly, try this:

# Data
x = c(1:5,1,4,7)

# Option 1
require(car)
y1=recode(x, "1=4; 4=2; 7=3")

# Option 2
y2=ifelse(x==1,4,
ifelse(x==4,2,
ifelse(x==7,3,x)))

# Are y1 and y2 equals?
all.equal(y1,y2)

# Ordering x by y1
x[order(y1)]

See ?order, ?ifelse and ?recode in the 'car' package for more information.

HTH, Jorge

On Tue, Nov 25, 2008 at 6:19 PM, PDXRugger <J_R_36_at_hotmail.com> wrote:

>
> I have consulted the intro and nabble but have not found an answer to what
> should be a simple question, here goes:
>
> I am doing a crosscheck of a data frame and pulling out a single value
> based
> on an inputted value ie based on x i will select y, or if x =2 then my code
> returns 7.
>
> x y
> 1 4
> 2 7
> 3 10
> 4 2
>
> My code currently iterates through for as many times as the length of the x
> dataframe. What i would like to do is then is to build a vector for those
> values selected. so if vector x = 1,4,7 then "new-y" would equal 4, 2, 3.
>
> x y
> 1 4
> 2 7
> 3 10
> 4 2
> 5 21
> 6 13
> 7 3
> 8 90
>
> I need to ultimetly re-sort my x vector based on the values of y but the
> function i am using is the subset function and i cant do it without y being
> a vector itself and it is currently only a one value object.
>
> #Creates test Candidates Vector
> Candidates=c(100,101,102,103,104,105)
>
> #Creates object equaling the number of candidate TAZs from the main script
> Location Choice Model
> NumCands1=length(Candidates)
>
> Dev..At=9999999
> for(i in 1:NumCands1){
>
> #Renames Location Choice Model generated TAZ's object
> Loc_Mod_TAZ=Candidates[i]
>
> #Converts Development size from main script to Development density
> function
> format
> Dev_Size=Dev..At
>
> #This is my "y" value in the example i gave above, this is the value
> that i
> need put into a # vector, a vector that should have as
> many values as there are number of candidates
> TAZDetermine_FEET=TAZ_VAC_FEET[TAZ_VAC_FEET$TAZ==Loc_Mod_TAZ,2]
>
> #Creates new vector based on adaquate vacancy in the TAZ and the
> development to be located
> Newcands=subset(Loc_Mod_TAZ, TAZDetermine_FEET>=Dev_Size)
>
>
> }
>
> Thanks for the help
> Cheers,
> JR
> --
> View this message in context:
> http://www.nabble.com/creating-a-new-vecotr-in-a-for-loop-tp20691663p20691663.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.
>

        [[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. Received on Wed 26 Nov 2008 - 01:17:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 26 Nov 2008 - 01:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive