Converting column of strings to boolean

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

Converting column of strings to boolean

domcastro
Hi

I'm trying to convert a column of strings (nominal types) to a set of boolean / binary / logical values. For example, in the column there is red, blue, green and yellow. There are 100 rows and each has a colour. I want to convert the column to 4 columns: red, blue, green,yellow and then either 1 or 0 put in the relevant row.
Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Converting column of strings to boolean

Pete Brecknock
domcastro wrote
Hi

I'm trying to convert a column of strings (nominal types) to a set of boolean / binary / logical values. For example, in the column there is red, blue, green and yellow. There are 100 rows and each has a colour. I want to convert the column to 4 columns: red, blue, green,yellow and then either 1 or 0 put in the relevant row.
Thanks
maybe model.matrix will help ....

# d is my understanding of your data
d<-factor(c("red","green","red","blue","green","yellow","red"))
model.matrix(~d -1)

HTH

Pete
Reply | Threaded
Open this post in threaded view
|

Re: Converting column of strings to boolean

arun kirshna
Hi,

Anther possibility may be to use:

library(MatrixModels)

  model.Matrix(~d-1,sparse=FALSE)
#7 x 4 Matrix of class "ddenseModelMatrix"
 # dblue dgreen dred dyellow
#1     0      0    1       0
#2     0      1    0       0
#3     0      0    1       0
#4     1      0    0       0
#5     0      1    0       0
#6     0      0    0       1
#7     0      0    1       0
 model.Matrix(~d-1,sparse=TRUE)
#"dsparseModelMatrix": 7 x 4 sparse Matrix of class "dgCMatrix"
 # dblue dgreen dred dyellow
#1     .      .    1       .
#2     .      1    .       .
#3     .      .    1       .
#4     1      .    .       .
#5     .      1    .       .
#6     .      .    .       1
#7     .      .    1       .
#@ assign:  1 1 1 1
#@ contrasts:
#$d
#[1] "contr.treatment"

A.K.


----- Original Message -----
From: Pete Brecknock <[hidden email]>
To: [hidden email]
Cc:
Sent: Saturday, January 26, 2013 6:27 PM
Subject: Re: [R] Converting column of strings to boolean

domcastro wrote
> Hi
>
> I'm trying to convert a column of strings (nominal types) to a set of
> boolean / binary / logical values. For example, in the column there is
> red, blue, green and yellow. There are 100 rows and each has a colour. I
> want to convert the column to 4 columns: red, blue, green,yellow and then
> either 1 or 0 put in the relevant row.
> Thanks

maybe model.matrix will help ....

# d is my understanding of your data
d<-factor(c("red","green","red","blue","green","yellow","red"))
model.matrix(~d -1)

HTH

Pete



--
View this message in context: http://r.789695.n4.nabble.com/Converting-column-of-strings-to-boolean-tp4656739p4656741.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: Converting column of strings to boolean

Rui Barradas
In reply to this post by domcastro
Hello,

Something like this?


x <- sample(c("red", "blue", "green", "yellow"), 100, replace = TRUE)
cnames <- unique(x)
sapply(cnames, function(.x) x == .x)


Hope this helps,

Rui Barradas

Em 26-01-2013 22:25, domcastro escreveu:

> Hi
>
> I'm trying to convert a column of strings (nominal types) to a set of
> boolean / binary / logical values. For example, in the column there is red,
> blue, green and yellow. There are 100 rows and each has a colour. I want to
> convert the column to 4 columns: red, blue, green,yellow and then either 1
> or 0 put in the relevant row.
> Thanks
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Converting-column-of-strings-to-boolean-tp4656739.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: Converting column of strings to boolean

domcastro
In reply to this post by Pete Brecknock
Thanks all. I will give them all a go and let you know the outcome.

kind regards