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]]
On Jul 20, 2010, at 1:14 PM, Heiman, Thomas J. 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?You are going to have a problem if you think that "NoReduction" == 'No Reduction' -- David
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])] > xV1 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?
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 whichrecode> the combinations from column 1 and 2 into a single number. Here is asnippet> 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]=='NoReduction')> "){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]=='NoReduction')> {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 sureit 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.