[R] Translating recoding syntax from SPSS to R

Joshua Wiley jwiley.psych at gmail.com
Sun Sep 22 09:20:14 CEST 2013


Just a slight addition to Arun's answer: within() saves typing and
makes life a bit easier

#####################################################
set.seed(429)
dat1 <- data.frame(
  race=sample(1:3,20,replace=TRUE),
  usborn=sample(0:2,20,replace=TRUE))

# within is a nice way to add variables
# and make modifications within a dataset
# saves typing lots of dat1$

dat1 <- within(dat1, {
 confused <- 1 * ((race==1 | race==2) & usborn==0)
})

head(dat1)

# only confused 0
dat1 <- subset(dat1, confused == 0)
#####################################################


On Sat, Sep 21, 2013 at 11:47 PM, arun <smartpink111 at yahoo.com> wrote:
> Hi,
> Try:
> set.seed(429)
> dat1<- data.frame(race=sample(1:3,20,replace=TRUE),usborn=sample(0:2,20,replace=TRUE))
>  dat1$confused<- 1*((dat1$race==1|dat1$race==2) & dat1$usborn==0)
> head(dat1)
> #  race usborn confused
> #1    3      2        0
> #2    1      0        1
> #3    2      1        0
> #4    3      2        0
> #5    1      2        0
> #6    1      1        0
> A.K.
>
>
>
> ----- Original Message -----
> From: Mosi Ifatunji <ifatunji at gmail.com>
> To: "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Saturday, September 21, 2013 6:03 PM
> Subject: [R] Translating recoding syntax from SPSS to R
>
> Colleagues,
>
> I am in the process of learning R. I've been able to import my dataset (from Stata) and do some simple coding. I have now come to coding situation that requires some assistance. This is some code in SPSS that I would like to be able to execute in R:
>
> if (race eq 1 and usborn=0) confused=1 .
> if (race eq 2 and usborn=0) confused=1 .
> if (race eq 1 and usborn=1) confused=0 .
> if (race eq 2 and usborn=1) confused=0 .
> if (race eq 3 and usborn=1) confused=0 .
> if (race eq 3 and cohort=1) confused=0 .
> if (race eq 3 and cohort=2) confused=0 .
> variable labels confused "R claims to be both an African American and foriegn born" .
> value labels confused
>     1 "Both AfAm and Foreign"
>     2 "Not" .
> select if (confused eq 0) .
>
> Any assistance would be greatly appreciated.
>
> -- Mosi
> ______________________________________________
> 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.
>
>
> ______________________________________________
> 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.



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://joshuawiley.com/
Senior Analyst - Elkhart Group Ltd.
http://elkhartgroup.com



More information about the R-help mailing list