[R] Recoding a variable

Stephan Kolassa Stephan.Kolassa at gmx.de
Thu Jul 3 21:32:06 CEST 2008


internet.use[internet.use=="Never" | internet.use=="Don't know"] <- 0
internet.use[internet.use!=0] <- 1

HTH,
Stephan



Spencer schrieb:
> Hi All,
> 
> I'm relatively new to R. I have a variable, "internet use," which ranges 
> from "Almost everyday, "Several times a week," "Several times a month," 
> "Seldom," "Never," and  "Don't know." I want to recode this data into a 
> new variable, say "use.internet," such that I have a dichotomous 
> variable indicating simply whether or not they have access to the 
> internet. Hence, I want to make "Never" and "Don't know" equal to 0, 
> everything else 1. I thought about doing an if...then statement, but am 
> not sure how to do this in R. This is easy to do in SPSS.
> 
> A sample of the existing array is the following:
> 
> Seldom               Several times a week
> Several times a month
> Never
> Never                Never               Seldom
> Several times a week
> Never               Never               Almost everyday      Never 
> And the ideal new variable:
> 
> 1
> 1
> 1
> 0
> 0
> 0
> 1
> 1
> 0
> 0
> 1
> 0
> 
> Any help would be great!
> 
> 
> ------------------------------------------------------------------------
> 
> ______________________________________________
> 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