[R-es] separar una variable en dos variables por una o más condiciones

juan manuel dias ju@m@d|@@ @end|ng |rom gm@||@com
Mar Mayo 31 21:38:45 CEST 2022


Hola,

Tengo una base de datos y en uno de las variables/vectores, que es formato
character, dentro de ese vector me encuentro que tengo casos con notación
científica, casos alfanuméricos etc.

Tengo que dividir esa variable en dos nuevas variables, de forma tal que me
quede una variable con los casos que tengan notación científica para
convertir a número, otro que donde tenga los alfanuméricos.
variable<- c("9,301E+11","8,301E+11","00001-00170818","1000055365","G.A.
001-24699570")
Unidades <- c("remito","remito","remito","remito","remito")
base.1<-cbind(variable,Unidades)
base.1
[image: image.png]

Lo que necesito sería separar "variable" asi:

variable<- c("9,301E+11","8,301E+11","00001-00170818","1000055365","G.A.
001-24699570")
variable.1<-c("9,301E+11","8,301E+11",NA,NA,NA)
variable.2<-c("","","00001-00170818","1000055365","G.A. 001-24699570")
Unidades <- c("remito","remito","remito","remito","remito")
base.2<-as.data.frame(cbind(variable,variable.1,variable.2,Unidades))

[image: image.png]

Lo que no logro hacer es separar "variable" en variable.1 y variable.2.
Intenté con str_detect dentro de un mutate y con if_else pero no logré que
me de el resultado que busco.

####
!str_detect(base2$variable.1,"\\E\\+"))
str_detect( base2$variable.1  ,"\\E\\+"))

Teniendo esa separación luego hago lo siguiente:

## reemplazo comas por puntos "." en  variable.1 para convertir a numérica
## convierto numérica variable.1
## y finalmente elimino notación científica
base.2$variable.1 <-(gsub(",", ".", base.2$variable.1))
base.2$variable.1 <-as.numeric(base.2$variable.1)
base.2$variable.1<-format(base.2$variable.1, scientific=F)

Y finalmente crear una variable "variable.3" que quede así:

## genero una variable final
base.2$variable.3<-ifelse(base.2$variable.1=="
 NA",base.2$variable.2,base.2$variable.1)
view(base.2)

[image: image.png]

Gracias!

Juan.

------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <https://stat.ethz.ch/pipermail/r-help-es/attachments/20220531/641e496b/attachment-0001.html>

------------ próxima parte ------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 16186 bytes
Desc: no disponible
URL: <https://stat.ethz.ch/pipermail/r-help-es/attachments/20220531/641e496b/attachment-0003.png>

------------ próxima parte ------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 19267 bytes
Desc: no disponible
URL: <https://stat.ethz.ch/pipermail/r-help-es/attachments/20220531/641e496b/attachment-0004.png>

------------ próxima parte ------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 18916 bytes
Desc: no disponible
URL: <https://stat.ethz.ch/pipermail/r-help-es/attachments/20220531/641e496b/attachment-0005.png>


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