[R] convert data frame: two variables into _one_ binary variable
Carl Witthoft
carl at witthoft.com
Mon Nov 25 18:13:47 CET 2013
In R, as.logical() and other functions treat anything >0 as TRUE. Thus:
Rgames> foo<-sample(0:5,10,rep=TRUE)
Rgames> foo
[1] 0 5 1 0 1 5 2 5 4 5
Rgames> as.logical(foo)
[1] FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
For your case, simply (womensrole$agree>womensrole$disagree) will return
the logical vector you want.
Just for info,
In R, as.logical() and other functions treat anything >0 as TRUE. Thus:
Rgames> foo<-sample(0:5,10,rep=TRUE)
Rgames> foo
[1] 0 5 1 0 1 5 2 5 4 5
Rgames> as.logical(foo)
[1] FALSE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE
Liviu Andronic wrote
> Dear all,
> I am trying to convert the following data frame into a format more
> useful for me:
>> library("HSAUR2", lib.loc="C:/Program Files/R/R-3.0.2/library")
> Loading required package: tools
>
> Attaching package: ‘HSAUR2’
>
> The following object is masked _by_ ‘.GlobalEnv’:
>
> womensrole
>
>> head(womensrole)
> education gender agree disagree sexe
> 1 0 Male 4 2 0
> 2 1 Male 2 0 0
> 3 2 Male 4 0 0
> 4 3 Male 6 3 0
> 5 4 Male 5 5 0
> 6 5 Male 13 7 0
>
>
> In 'womensrole', how do I convert 'agree' and 'disagree' variables
> into one proper binary variable, say:
> education gender agree sexe
> 1 0 Male TRUE 0
> 2 0 Male TRUE 0
> 3 0 Male TRUE 0
> 4 0 Male TRUE 0
> 5 0 Male FALSE 0
> 6 0 Male FALSE 0
> 7 1 Male TRUE 0
> 8 1 Male TRUE 0
> 9 2 Male TRUE 0
> 10 2 Male TRUE 0
> 11 2 Male TRUE 0
> 12 2 Male TRUE 0
> [..]
>
> I'm sure there is an easy way to do this (in the form of 'melt',
> 'cast', etc.), but I'm not sure how to approach the problem.
>
> Regards,
> Liviu
>
> --
> Do you know how to read?
> http://www.alienetworks.com/srtest.cfm
> http://goodies.xfce.org/projects/applications/xfce4-dict#speed-reader
> Do you know how to write?
> http://garbl.home.comcast.net/~garbl/stylemanual/e.htm#e-mail
>
> ______________________________________________
> R-help@
> 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.
--
View this message in context: http://r.789695.n4.nabble.com/convert-data-frame-two-variables-into-one-binary-variable-tp4681098p4681123.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list