Re: [R] Better way than an ifelse statement?
Try this: example$X3 - sapply(example$X2, switch, -3, -1, 1, 3) On Thu, Jan 14, 2010 at 5:05 AM, Joshua Wiley jwiley.ps...@gmail.com wrote: Hello All, I am trying to create a column of weights based off of factor levels from another column. I am using the weights to calculate L scores. Here is an example where the first column are scores, the second is my factor and the third I want to be a column of weights. I can do what I want with an ifelse statement (see below), but I am wondering if anyone knows of a cleaner way to do this? example - data.frame(cbind(rnorm(4), rep(1:4, 1), c(0))) example$X3 - ifelse(example$X2==1, -3, ( ifelse(example$X2==2, -1, ( ifelse(example$X2==3, 1, ( ifelse(example$X2==4, 3, NA))) ## this seems sloppy to me example X1 X2 X3 1 1.75308880 1 -3 2 -0.49273616 2 -1 3 -0.12446648 3 1 4 -0.06417217 4 3 Thanks for your help, Joshua -- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ R-help@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.
Re: [R] Better way than an ifelse statement?
Or this ; example$X3 - c(-3, -1, 1, 3)[ example$X2 ] Romain On 01/14/2010 11:55 AM, Henrique Dallazuanna wrote: Try this: example$X3- sapply(example$X2, switch, -3, -1, 1, 3) On Thu, Jan 14, 2010 at 5:05 AM, Joshua Wileyjwiley.ps...@gmail.com wrote: Hello All, I am trying to create a column of weights based off of factor levels from another column. I am using the weights to calculate L scores. Here is an example where the first column are scores, the second is my factor and the third I want to be a column of weights. I can do what I want with an ifelse statement (see below), but I am wondering if anyone knows of a cleaner way to do this? example- data.frame(cbind(rnorm(4), rep(1:4, 1), c(0))) example$X3- ifelse(example$X2==1, -3, ( ifelse(example$X2==2, -1, ( ifelse(example$X2==3, 1, ( ifelse(example$X2==4, 3, NA))) ## this seems sloppy to me example X1 X2 X3 1 1.75308880 1 -3 2 -0.49273616 2 -1 3 -0.12446648 3 1 4 -0.06417217 4 3 Thanks for your help, Joshua -- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ -- Romain Francois Professional R Enthusiast +33(0) 6 28 91 30 30 http://romainfrancois.blog.free.fr |- http://tr.im/KfKn : Rcpp 0.7.2 |- http://tr.im/JOlc : External pointers with Rcpp `- http://tr.im/JFqa : R Journal, Volume 1/2, December 2009 __ R-help@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.
Re: [R] Better way than an ifelse statement?
This works too: example$X3 - example$X2 * 2 - 5 On Thu, Jan 14, 2010 at 8:55 AM, Henrique Dallazuanna www...@gmail.com wrote: Try this: example$X3 - sapply(example$X2, switch, -3, -1, 1, 3) On Thu, Jan 14, 2010 at 5:05 AM, Joshua Wiley jwiley.ps...@gmail.com wrote: Hello All, I am trying to create a column of weights based off of factor levels from another column. I am using the weights to calculate L scores. Here is an example where the first column are scores, the second is my factor and the third I want to be a column of weights. I can do what I want with an ifelse statement (see below), but I am wondering if anyone knows of a cleaner way to do this? example - data.frame(cbind(rnorm(4), rep(1:4, 1), c(0))) example$X3 - ifelse(example$X2==1, -3, ( ifelse(example$X2==2, -1, ( ifelse(example$X2==3, 1, ( ifelse(example$X2==4, 3, NA))) ## this seems sloppy to me example X1 X2 X3 1 1.75308880 1 -3 2 -0.49273616 2 -1 3 -0.12446648 3 1 4 -0.06417217 4 3 Thanks for your help, Joshua -- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ __ R-help@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. -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40 S 49° 16' 22 O __ R-help@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.
Re: [R] Better way than an ifelse statement?
Hello, Thanks for all of your responses. I got a lot of great options that work. Thanks again! Joshua __ R-help@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.
Re: [R] Better way than an ifelse statement?
There is a recode function in the Hmisc package that might help: library(Hmisc) example$X3 - recode(example$X2, 1:4, c(-3,-1,1,3)) -Ista On Thu, Jan 14, 2010 at 2:05 AM, Joshua Wiley jwiley.ps...@gmail.com wrote: Hello All, I am trying to create a column of weights based off of factor levels from another column. I am using the weights to calculate L scores. Here is an example where the first column are scores, the second is my factor and the third I want to be a column of weights. I can do what I want with an ifelse statement (see below), but I am wondering if anyone knows of a cleaner way to do this? example - data.frame(cbind(rnorm(4), rep(1:4, 1), c(0))) example$X3 - ifelse(example$X2==1, -3, ( ifelse(example$X2==2, -1, ( ifelse(example$X2==3, 1, ( ifelse(example$X2==4, 3, NA))) ## this seems sloppy to me example X1 X2 X3 1 1.75308880 1 -3 2 -0.49273616 2 -1 3 -0.12446648 3 1 4 -0.06417217 4 3 Thanks for your help, Joshua -- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ __ R-help@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. -- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org __ R-help@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.