Skip to content
Prev 333661 / 398506 Next

convert data frame: two variables into _one_ binary variable

I think the OP was looking to expand the data frame so that each row was a single observation so that the first row becomes 6 rows, 4-TRUE and 2-FALSE. Something like this
+   v.names="Freq", timevar="Agree", times=c(TRUE, FALSE), 
+   direction="long")
id education  sex Agree
1   1         0 Male  TRUE
2   1         0 Male  TRUE
3   1         0 Male  TRUE
4   1         0 Male  TRUE
5   1         0 Male FALSE
6   1         0 Male FALSE
7   2         1 Male  TRUE
8   2         1 Male  TRUE
9   3         2 Male  TRUE
10  3         2 Male  TRUE
11  3         2 Male  TRUE
12  3         2 Male  TRUE

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Carl Witthoft
Sent: Monday, November 25, 2013 11:14 AM
To: r-help at r-project.org
Subject: Re: [R] convert data frame: two variables into _one_ binary variable

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

            
--
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.

______________________________________________
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.