Matrix to data.frame with factors

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Matrix to data.frame with factors

brunosm
Hi all,

I have a matrix with 100 variables: each variable as a value of 0 or 1.

What i want to do is convert this matrix to a data.frame but convert all the variables to factors (0 and 1) also.

I know i can do this one variable a time but i have 100 variables...

Any easy way of doing this??

Thanks a lot,

Bruno
Reply | Threaded
Open this post in threaded view
|

Re: Matrix to data.frame with factors

arun kirshna
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hi,
Try this:
set.seed(1)
mat1<-matrix(sample(0:1,50,replace=TRUE),nrow=10,ncol=5)
 dat1<-as.data.frame(mat1)
dat2<- do.call(data.frame,lapply(dat1,as.factor))
 str(dat2)
#'data.frame': 10 obs. of  5 variables:
 #$ V1: Factor w/ 2 levels "0","1": 1 1 2 2 1 2 2 2 2 1
 #$ V2: Factor w/ 2 levels "0","1": 1 1 2 1 2 1 2 2 1 2
 #$ V3: Factor w/ 2 levels "0","1": 2 1 2 1 1 1 1 1 2 1
 #$ V4: Factor w/ 2 levels "0","1": 1 2 1 1 2 2 2 1 2 1
 #$ V5: Factor w/ 2 levels "0","1": 2 2 2 2 2 2 1 1 2 2

A.K.
Reply | Threaded
Open this post in threaded view
|

Re: Matrix to data.frame with factors

Rui Barradas
In reply to this post by brunosm
Hello,

Try the following.

x <- matrix(sample(0:1, 12, TRUE), ncol = 4)
y <- data.frame(apply(x, 2, factor))
str(y)

Hope this helps,

Rui Barradas
Em 19-10-2012 12:04, brunosm escreveu:

> Hi all,
>
> I have a matrix with 100 variables: each variable as a value of 0 or 1.
>
> What i want to do is convert this matrix to a data.frame but convert all the
> variables to factors (0 and 1) also.
>
> I know i can do this one variable a time but i have 100 variables...
>
> Any easy way of doing this??
>
> Thanks a lot,
>
> Bruno
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Matrix-to-data-frame-with-factors-tp4646730.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: Matrix to data.frame with factors

brunosm
In reply to this post by arun kirshna
Thanks a lot!
Reply | Threaded
Open this post in threaded view
|

Re: Matrix to data.frame with factors

brunosm
In reply to this post by Rui Barradas
Obrigado Rui, é isso mesmo ;)
Reply | Threaded
Open this post in threaded view
|

Re: Matrix to data.frame with factors

Bert Gunter
In reply to this post by Rui Barradas
Well, strictly speaking, this is still doing it "one variable at a
time." The interpreted loop is hidden, but it's still happening.

A loop free but clumsier approach is:

y <- data.frame(matrix(as.character(x),nrow = nrow(x)))

## Note also that the original column names will be lost and will have
to be added to the data frame.

It would also not surprise me if for such a small matrix that Rui's
version were faster.

-- Bert

On Fri, Oct 19, 2012 at 7:07 AM, Rui Barradas <[hidden email]> wrote:

> Hello,
>
> Try the following.
>
> x <- matrix(sample(0:1, 12, TRUE), ncol = 4)
> y <- data.frame(apply(x, 2, factor))
> str(y)
>
> Hope this helps,
>
> Rui Barradas
> Em 19-10-2012 12:04, brunosm escreveu:
>>
>> Hi all,
>>
>> I have a matrix with 100 variables: each variable as a value of 0 or 1.
>>
>> What i want to do is convert this matrix to a data.frame but convert all
>> the
>> variables to factors (0 and 1) also.
>>
>> I know i can do this one variable a time but i have 100 variables...
>>
>> Any easy way of doing this??
>>
>> Thanks a lot,
>>
>> Bruno
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Matrix-to-data-frame-with-factors-tp4646730.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.



--

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm

______________________________________________
[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: Matrix to data.frame with factors

Peter Dalgaard-2
In reply to this post by Rui Barradas

On Oct 19, 2012, at 16:07 , Rui Barradas wrote:

> Hello,
>
> Try the following.
>
> x <- matrix(sample(0:1, 12, TRUE), ncol = 4)
> y <- data.frame(apply(x, 2, factor))
> str(y)
>
> Hope this helps,

Another way, possibly more easily generalized:

x <- matrix(sample(0:1, 12, TRUE), ncol = 4)
y <- as.data.frame(x)
y[] <- lapply(y, factor, levels=0:1)
str(y)

with extensions like

is01 <- function(x) all(na.omit(x) %in% 0:1)
x <- matrix(sample(0:1, 12, TRUE), ncol = 4)
y <- as.data.frame(x)
ix <- sapply(y, is01)
y[ix] <- lapply(y[ix], factor, levels=0:1, labels=c("n","y"))
str(y)

>
> Rui Barradas
> Em 19-10-2012 12:04, brunosm escreveu:
>> Hi all,
>>
>> I have a matrix with 100 variables: each variable as a value of 0 or 1.
>>
>> What i want to do is convert this matrix to a data.frame but convert all the
>> variables to factors (0 and 1) also.
>>
>> I know i can do this one variable a time but i have 100 variables...
>>
>> Any easy way of doing this??
>>
>> Thanks a lot,
>>
>> Bruno
>>
>>
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Matrix-to-data-frame-with-factors-tp4646730.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.

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[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: Matrix to data.frame with factors

arun kirshna
In reply to this post by brunosm
Hi,
No problem.

You can also try this:
set.seed(1)
mat1<-matrix(sample(0:1,50,replace=TRUE),nrow=10,ncol=5)
 dat1<-as.data.frame(mat1)
 dat2<-do.call(data.frame,rapply(dat1,as.factor,classes="integer",how="replace"))
# str(dat2)
#'data.frame':    10 obs. of  5 variables:
# $ X1: Factor w/ 2 levels "0","1": 1 2 1 1 1 1 1 2 2 1
# $ X2: Factor w/ 2 levels "0","1": 2 1 1 1 2 1 1 2 1 2
# $ X3: Factor w/ 2 levels "0","1": 1 2 1 1 1 2 2 1 2 2
# $ X4: Factor w/ 2 levels "0","1": 1 2 1 1 2 1 2 1 1 1
# $ X5: Factor w/ 2 levels "0","1": 1 1 2 2 2 2 1 1 2 2
A.K.






----- Original Message -----
From: brunosm <[hidden email]>
To: [hidden email]
Cc:
Sent: Friday, October 19, 2012 10:15 AM
Subject: Re: [R] Matrix to data.frame with factors

Thanks a lot!



--
View this message in context: http://r.789695.n4.nabble.com/Matrix-to-data-frame-with-factors-tp4646730p4646756.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.