[R] Odp: If help

Petr PIKAL petr.pikal at precheza.cz
Wed Jul 21 11:39:41 CEST 2010


Hi

I would make an interaction of those two vectors.

interaction(sample(letters[1:2], 20, replace=T),sample(letters[1:2], 20, 
replace=T))
 [1] a.a a.b b.b b.a b.b b.b a.a a.a b.b b.a a.a a.a b.b b.a a.a a.b b.a 
a.a b.b
[20] a.b
Levels: a.a b.a a.b b.b

You will get factor and you can change easily labels and change them to 
numeric vector.

See, ?levels, ?factor

Regards
Petr
 

r-help-bounces at r-project.org napsal dne 20.07.2010 19:14:28:

> Hi Y'all,
> 
> I have some data in a table with 2 columns. There are two values: 
"Reduction" 
> and "No Reduction. "  I am trying to make a new variable change which 
recode 
> the combinations from column 1 and 2 into a single number.  Here is a 
snippet 
> from the table:
> 
> [1,] "NoReduction"              "NoReduction"
>  [2,] "Reduction"                "Reduction"
>  [3,] "NoReduction"              "NoReduction"
>  [4,] "NoReduction"              "NoReduction"
>  [5,] "Reduction"                "Reduction"
>  [6,] "Reduction"                "Reduction"
>  [7,] "Reduction"                "Reduction"
>  [8,] "NoReduction"              "NoReduction"
>  [9,] "NoReduction"              "NoReduction"
> [10,] "NoReduction"              "NoReduction"
> 
> This is the code that I have written so far..
> 
> for (i in 1:nrow(change20082009))
> if(change20082009[i,1]=='No Reduction' & change20082009[i,2]=='No 
Reduction') 
> "){change20082009[i,3] <- 1} else
> if(change20082009[i,1]=='No Reduction' & 
change20082009[i,2]=='Reduction')
> {change20082009[i,3] <- -1} else
> if(change20082009[i,1]=='Reduction'    & change20082009[i,2]=='No 
Reduction') 
> {change20082009[i,3] <- 2} else
> if(change20082009[i,1]=='Reduction'    & 
change20082009[i,2]=='Reduction') 
> {change20082009[i,3] <- 0}
> )
> 
> I can't seem to get the code above to work..Any suggestions (I am sure 
it is 
> really basic)? Is there a better way to do this?
> 
> Sincerely,
> 
> tom
> 
> 
>    [[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