[R] If help
jim holtman
jholtman at gmail.com
Tue Jul 20 19:39:35 CEST 2010
Try this, assuming your data is consistent:
> x <- read.table(textConnection('"NoReduction" "NoReduction"
+ "Reduction" "Reduction"
+ "NoReduction" "NoReduction"
+ "NoReduction" "NoReduction"
+ "Reduction" "Reduction"
+ "Reduction" "Reduction"
+ "Reduction" "Reduction"
+ "NoReduction" "Reduction"
+ "Reduction" "NoReduction"
+ "NoReduction" "NoReduction"
+ "NoReduction" "NoReduction"
+ "NoReduction" "NoReduction"'), as.is=TRUE)
>
> mapping <- c("NoReduction NoReduction" = 1,
+ "NoReduction Reduction" = -1,
+ "Reduction NoReduction" = 2,
+ "Reduction Reduction" = 0)
>
> x$change <- mapping[paste(x[,1], x[,2])]
> x
V1 V2 change
1 NoReduction NoReduction 1
2 Reduction Reduction 0
3 NoReduction NoReduction 1
4 NoReduction NoReduction 1
5 Reduction Reduction 0
6 Reduction Reduction 0
7 Reduction Reduction 0
8 NoReduction Reduction -1
9 Reduction NoReduction 2
10 NoReduction NoReduction 1
11 NoReduction NoReduction 1
12 NoReduction NoReduction 1
On Tue, Jul 20, 2010 at 1:14 PM, Heiman, Thomas J. <theiman at mitre.org> wrote:
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list