convert data frame: two variables into _one_ binary variable

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

convert data frame: two variables into _one_ binary variable

Liviu Andronic
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: convert data frame: two variables into _one_ binary variable

arun kirshna
Hi,
One way would be:

womensrole <- read.table(text="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",sep="",header=TRUE,stringsAsFactors=FALSE)
library(reshape2)
 res1 <- melt(womensrole,measure.vars=c("agree","disagree"),var="agree")
 res2 <- within(res1[rep(1:nrow(res1),res1$value),], agree <- agree=="agree")[,c(1,2,4,3)]
 res2New <- res2[order(res2$education),]
row.names(res2New) <- 1:nrow(res2New)

A.K.



On Monday, November 25, 2013 4:24 AM, Liviu Andronic <[hidden email]> 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

______________________________________________
[hidden email] 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.


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: convert data frame: two variables into _one_ binary variable

Carl Witthoft
In reply to this post by Liviu Andronic
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: convert data frame: two variables into _one_ binary variable

David Carlson
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

> womensrole <- HSAUR::womensrole
> step1 <- reshape(womensrole, varying=c("agree", "disagree"),
+   v.names="Freq", timevar="Agree", times=c(TRUE, FALSE),
+   direction="long")
> step1 <- step1[order(step1$id),]
> rownames(step1) <- NULL # Simplify the row names
> step2 <- step1[rep(rownames(step1), step1$Freq), c(5, 1:3)]
> rownames(step2) <- NULL # Simplify the row names
> head(step2, 12)
   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: [hidden email] [mailto:[hidden email]] On Behalf Of Carl Witthoft
Sent: Monday, November 25, 2013 11:14 AM
To: [hidden email]
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

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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: convert data frame: two variables into _one_ binary variable

Thomas Stewart-3
In reply to this post by Liviu Andronic
I do not think Carl's solution answers your question.  Try this:

z <- textConnection(
"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"
)

zz <- read.table(z,header=TRUE, stringsAsFactors=FALSE);
close(z)

zzz <- melt(zz,id.var=c("education","gender","sexe"))
zzzz <- zzz[rep(1:nrow(zzz), zzz$value), 1:4 ]
zzzz$agree <- zzzz$variable == 'agree'

zzzz

-tgs


On Mon, Nov 25, 2013 at 4:24 AM, Liviu Andronic <[hidden email]>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
>
> ______________________________________________
> [hidden email] 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.
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: convert data frame: two variables into _one_ binary variable

Liviu Andronic
Dear all,
Thank you so much for all the replies.

The following worked exactly as needed:
> library("HSAUR2", lib.loc="/home/liv/R/i686-pc-linux-gnu-library/3.0")
> library("reshape2", lib.loc="/usr/local/lib/R/site-library")
> data(womensrole)
> zzz <- melt(womensrole,id.var=c("education","gender"))
> zzzz <- zzz[rep(1:nrow(zzz), zzz$value), 1:4 ]
> zzzz$agree <- zzzz$variable == 'agree'
> zzzz[ zzzz$education %in% 0:1, ]
education gender variable value agree
1 0 Male agree 4 TRUE
1.1 0 Male agree 4 TRUE
1.2 0 Male agree 4 TRUE
1.3 0 Male agree 4 TRUE
2 1 Male agree 2 TRUE
2.1 1 Male agree 2 TRUE
22 0 Female agree 4 TRUE
22.1 0 Female agree 4 TRUE
22.2 0 Female agree 4 TRUE
22.3 0 Female agree 4 TRUE
23 1 Female agree 1 TRUE
43 0 Male disagree 2 FALSE
43.1 0 Male disagree 2 FALSE
64 0 Female disagree 2 FALSE
64.1 0 Female disagree 2 FALSE

Regards,
Liviu


On Mon, Nov 25, 2013 at 8:26 PM, Thomas Stewart
<[hidden email]> wrote:

> I do not think Carl's solution answers your question.  Try this:
>
> z <- textConnection(
> "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"
> )
>
> zz <- read.table(z,header=TRUE, stringsAsFactors=FALSE);
> close(z)
>
> zzz <- melt(zz,id.var=c("education","gender","sexe"))
> zzzz <- zzz[rep(1:nrow(zzz), zzz$value), 1:4 ]
> zzzz$agree <- zzzz$variable == 'agree'
>
> zzzz
>
> -tgs
>
>
> On Mon, Nov 25, 2013 at 4:24 AM, Liviu Andronic <[hidden email]>
> 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
>>
>> ______________________________________________
>> [hidden email] 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.
>
>



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

______________________________________________
[hidden email] 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.