[R-es] Ayuda con Date
Mauricio Monsalvo
m.monsalvo en gmail.com
Jue Feb 16 17:23:30 CET 2017
Carlos, probé en primer lugar tu propuesta. Pero:
as.data.table(datos)[, .N, by=pprfecbaja]
pprfecbaja N
1: 0001-01-01 2000
2: 1994-08-26 20
3: 2005-07-06 585
4: 2005-03-07 29
5: 2004-03-23 33
6: 2004-10-18 1
7: 1997-09-01 9
8: 1997-09-04 16
9: 1996-05-29 3
...
datos$pprfecbaja <- ymd(datos$pprfecbaja)
Warning message:
764 failed to parse.
> as.data.table(datos)[, .N, by=pprfecbaja]
pprfecbaja N
1: <NA> 2770
2: 2001-01-05 9
3: 2001-01-08 16
4: 2001-01-07 7
5: 2001-09-07 1
6: 2001-05-12 1
7: 2001-10-22 1
8: 2001-12-27 1
9: 2001-08-20 1
10: 2001-03-09 1
Es decir, ymd() lleva todos los datos a unas fechas que no existen, todas
del año 2001.
Por otro lado, la opción de Gerar:
datos$pprfecbaja<-strptime(datos$pprfecbaja, "%Y-%m-%d") ##transformar la
variable de formato Character a formato POSIXlt
Warning messages:
1: In `[<-.data.table`(x, j = name, value = value) :
Supplied 11 items to be assigned to 2808 items of column 'pprfecbaja'
(recycled leaving remainder of 3 items).
2: In `[<-.data.table`(x, j = name, value = value) :
Coerced 'list' RHS to 'character' to match the column's type. Either
change the target column to 'list' first (by creating a new 'list' vector
length 2808 (nrows of entire table) and assign that; i.e. 'replace'
column), or coerce RHS to 'character' (e.g. 1L, NA_[real|integer]_, as.*,
etc) to make your intent clear and for speed. Or, set the column type
correctly up front when you create the table and stick to it, please.
Da valores raros a las celdas, algo así como (el print sale truncado):
c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, ... 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, NA, NA, NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
Finlmente, la opción de Freddy *andó:*
datos$pprfecbaja<-replace(as.Date(datos$pprfecbaja),
datos$pprfecbaja=="0001-01-01", NA)
as.data.table(datos)[, .N, by=pprfecbaja]
pprfecbaja N
1: <NA> 2006
2: 1994-08-26 20
3: 2005-07-06 585
4: 2005-03-07 29
5: 2004-03-23 33
6: 2004-10-18 1
7: 1997-09-01 9
8: 1997-09-04 16
9: 1996-05-29 3
10: 1997-09-03 7
El 16 de febrero de 2017, 12:43, Carlos Ortega <cof en qualityexcellence.es>
escribió:
> Hola,
>
> Así funciona...
>
> #--------------------------------
>
> library(lubridate)
> datos <- read.table("datos.csv", header = TRUE, sep = ";", as.is = TRUE)
> #Primero quito fechas malas
> datos$pprfecbaja <- ifelse(datos$pprfecbaja=="0001-01-01", NA,
> datos$pprfecbaja)
>
> # Convierto campo fechas en clase Date
> datos$pprfecbaja <- ymd(datos$pprfecbaja)
>
> # Comprobacion de la clase
> tail(datos)
> class(datos$pprfecbaja[2808])
>
> #--------------------------------
>
> Saludos,
> Carlos Ortega
> www.qualityexcellence.es
>
> El 16 de febrero de 2017, 13:34, Mauricio Monsalvo <m.monsalvo en gmail.com>
> escribió:
>
>> Hola.
>> Tengo una duda con esta sintaxis. Tengo una variable con formato Date que
>> por algún motivo (el data.table viene de una consulta con PostgreSQL):
>> datos <- prov[, pprid, pprfecbaja]
>> str(datos)
>> Cuando intento quitar las fechas de bajas inválidas (0001-01-01) y
>> convertirlas a NA, la variable resultante "pierde" su condición de Date.
>> Probé con distintas formas, siempre con el mismo resultado:
>> datos$pprfecbaja <- ifelse(datos$pprfecbaja=="0001-01-01", NA,
>> datos$pprfecbaja)
>> datos$pprfecbaja <- ifelse(datos$pprfecbaja=="0001-01-01", NA,
>> as.Date(datos$pprfecbaja))
>> datos$pprfecbaja <- ifelse(year(datos$pprfecbaja) < 1994, NA,
>> as.Date(datos$pprfecbaja))
>> ¿Podrían por favor ayudarme a correr la sintaxis correcta?
>> Adjunto los datos de ejemplo.
>> Muchas gracias.
>>
>> --
>> Mauricio
>>
>> _______________________________________________
>> R-help-es mailing list
>> R-help-es en r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-help-es
>>
>
>
>
> --
> Saludos,
> Carlos Ortega
> www.qualityexcellence.es
>
--
Mauricio
[[alternative HTML version deleted]]
Más información sobre la lista de distribución R-help-es