[R] Conditional expressions in commands -- basic

Richard Sherman rss.pdx at gmail.com
Sun Mar 23 03:21:14 CET 2014


Hi all,

A simple question, new-ish to R, coming from Stata, and I yes I've looked at great length and not found ...

In Stata I might write (with * in place of #) 

# regress y on x in the set of observations where a==1
reg y x if a==1

# descriptives on x where a==1
su x if a==1

# generate a logical depending on the value of a variable
gen z = (a == 1)

# or likewise for a numeric
gen x = y if a == 1

# show the values of a variable based on the value of another variable 
di y if a ~= .

# generate a logical based on missing-ness of a variable
gen w = (a~=.)

or any of many similar operations conditional on the value of a variable.

Believe it or not this is a big obstacle to learning R. 

---
Richard Sherman



More information about the R-help mailing list