[R-es] Transformar muchas variables factor en variables binarias de acuerdo a niveles

Jesús Para Fernández j@p@r@@fern@ndez @ending from hotm@il@com
Lun Jun 25 18:37:43 CEST 2018


Por cierto data.table tiene implementadas las funciones de reshape2 melt y dcast pero mucho m�s r�pidasen ejecuci�n

Obtener Outlook para Android<https://aka.ms/ghei36>

________________________________
From: R-help-es <r-help-es-bounces using r-project.org> on behalf of V�ctor Granda Garc�a <victorgrandagarcia using gmail.com>
Sent: Monday, June 25, 2018 4:23:52 PM
To: Fernando Reche Lorite
Cc: r-help-es
Subject: Re: [R-es] Transformar muchas variables factor en variables binarias de acuerdo a niveles

En esta respuesta: https://stackoverflow.com/a/35663834/2301674

tienes como hacerlo (mucho m�s sencillo que en la que enlazas t�) con
reshape2 o si prefieres la versi�n "tidy" con dplyr y tidyr (la que yo
recomiendo, pero solo porque me gusta m�s).

Espero que te sirva

On Mon, 25 Jun 2018 at 16:08 Fernando Reche Lorite via R-help-es <
r-help-es using r-project.org> wrote:

> Puedes probar con la funci�n dummy del paquete dummies.
>
> Un saludo
> Fernando Reche Lorite
> Departamento de Matem�ticas
> Universidad de Almer�a
>
> El 25 de junio de 2018, 15:55, Carlos J. Gil Bellosta <
> cgb using datanalytics.com>
> escribi�:
>
> > �No te vale model.matrix?
> >
> > El lun., 25 jun. 2018 a las 15:49, Juan Abasolo (<juan.abasolo using ehu.eus>)
> > escribi�:
> >
> > > Buenas, compa�eros.
> > >
> > > Tengo una base de datos con bastantes variables todas medidas como
> > factor,
> > > quiero que todos los factores pasen a ser variables binarias en funci�n
> > de
> > > sus valores.
> > >
> > > En este ejemplo de Stackoverflow muestran como hacerlo con una
> variable:
> > >
> > > https://stackoverflow.com/questions/33990760/converting-
> > factors-to-binary-in-r
> > >
> > > df  <-data.frame(a = c(1,2,3), b = c(1,1,2), c =
> > > c("Rose","Pink","Red"), d = c(2,3,4))
> > >
> > > cbind(df[1:2], sapply(levels(df$c), function(x) as.integer(x == df$c)),
> > > df[4])
> > >
> > > o as�
> > >
> > > library(data.table)
> > > setDT(df)[, c(levels(df$c), "c") :=
> > >     c(lapply(levels(c), function(x) as.integer(x == c)), .(NULL))]
> > >
> > >
> > > Pero no me resuelve el tener que hacerlo algunos cientos de veces, que
> es
> > > lo que querr�a evitar. S� que es evidente c�mo se tiene que hacer, pero
> > soy
> > > ciego a esa evidencia :-(
> > >
> > > Muchas gracias por la ayuda
> > >
> > >
> > > --
> > > Juan Abasolo
> > >
> > > Hizkuntzaren eta Literaturaren Didaktika Saila
> > > Bilboko Hezkuntza Fakultatea
> > > Euskal Herriko Unibertsitatea
> > > UPV/EHU
> > >
> > > Sarriena auzoa z/g
> > > 48940 Leioa
> > > Bizkaia
> > >
> > >         [[alternative HTML version deleted]]
> > >
> > > _______________________________________________
> > > R-help-es mailing list
> > > R-help-es using r-project.org
> > > https://stat.ethz.ch/mailman/listinfo/r-help-es
> > >
> >
> >         [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-help-es mailing list
> > R-help-es using r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-help-es
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-help-es mailing list
> R-help-es using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-help-es
>
--
*V�ctor Granda Garc�a*
Data Technician


v.granda using creaf.uab.cat
Tel. +34 93 581 33 53


Campus UAB. Edifici C. 08193 Bellaterra (Barcelona) | *www.creaf.cat*
<http://www.creaf.uab.es/cat/index.htm>

Abans d'imprimir aquest missatge electr�nic penseu en el medi ambient.

        [[alternative HTML version deleted]]

_______________________________________________
R-help-es mailing list
R-help-es using r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es

	[[alternative HTML version deleted]]



Más información sobre la lista de distribución R-help-es