[R-es] Tasa variación diaria COVID-19

Carlos Ortega co| @end|ng |rom qu@||tyexce||ence@e@
Dom Mar 22 23:50:54 CET 2020


Hola,

Te sale error porque has cargado el fichero con read.csv().
Si pruebas a cargarlo con "fread()" tus datos tendrán clase data.table.
El error que obtienes es porque el objeto df (igual que datos) no es un
data.table.

Saludos,
Carlos Ortega
www.qualityexcellence.es

El dom., 22 mar. 2020 a las 22:45, Javier Marcuzzi (<
javier.ruben.marcuzzi using gmail.com>) escribió:

> Eric
>
> ¿Que dataset utilizo? Por curiosidad probé su código, pero me salen
> errores. Copio y pego todo como me sale en la consola.
>
> > #
>
> https://raw.githubusercontent.com/datasets/covid-19/master/data/time-series-19-covid-combined.csv
> > datos <-
> read.csv("C:/Users/HP/Downloads/time-series-19-covid-combined.csv")
> >
> > library(data.table)
> > library(ggplot2)
> > library(anytime)
> >
> > df <-datos
> >
> > df[, fec:=anytime(fec)]
> Error in `:=`(fec, anytime(fec)) :
>   Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are
> defined for use in j, once only and in particular ways. See help(":=").
> > setkey(df,com,fec)
> Error in setkeyv(x, cols, verbose = verbose, physical = physical) :
>   x is not a data.table
> >
> > # newc: son los nuevos casos
> > df[, newc:=ct - shift(ct), by=.(com)]
> Error in `[.data.frame`(df, , `:=`(newc, ct - shift(ct)), by = .(com)) :
>   unused argument (by = .(com))
> >
> > # tasa: es la tasa de cambio que calculé así (ct2 - ct1)/ct1
> >
> > df[, tasa:=newc/ct, by=.(com)]
> Error in `[.data.frame`(df, , `:=`(tasa, newc/ct), by = .(com)) :
>   unused argument (by = .(com))
>
> El dom., 22 mar. 2020 a las 16:52, neo (<ericconchamunoz using gmail.com>)
> escribió:
>
> > Hola Javier, yo lo hice de la siguiente forma:
> >
> > library(data.table)
> > library(ggplot2)
> > library(anytime)
> >
> > df <- fread("/home/neo/Desktop/rhelp/spain.csv")
> > df[, fec:=anytime(fec)]
> > setkey(df,com,fec)
> >
> >      # newc: son los nuevos casos
> > df[, newc:=ct - shift(ct), by=.(com)]
> >
> >      # tasa: es la tasa de cambio que calculé así (ct2 - ct1)/ct1
> >
> > df[, tasa:=newc/ct, by=.(com)]
> >
> >
> > y obtuve lo siguiente:
> >
> >
> >                     fec       com     pob ct newc tasa
> > 1: 2020-04-02 23:00:00 Andalucía 8414240 13 NA          NA
> > 2: 2020-05-02 23:00:00 Andalucía 8414240 12   -1 -0.08333333
> > 3: 2020-06-02 23:00:00 Andalucía 8414240 21    9 0.42857143
> > 4: 2020-04-02 23:00:00    Aragón 1319291  0 NA          NA
> > 5: 2020-05-02 23:00:00    Aragón 1319291  1    1 1.00000000
> > 6: 2020-06-02 23:00:00    Aragón 1319291  6    5 0.83333333
> > 7: 2020-04-02 23:00:00  Asturias 1022800  2 NA          NA
> > 8: 2020-05-02 23:00:00  Asturias 1022800  5    3 0.60000000
> > 9: 2020-06-02 23:00:00  Asturias 1022800  5    0 0.00000000
> >
> > Espero que te sirva.
> >
> > Saludos !!
> >
> > Eric.
> >
> >
> >
> >
> > On 21-03-20 14:42, Javier Gómez Gonzalez wrote:
> > > Hola:
> > >
> > > Tengo los datos diarios del número de casos de coronavirus por
> > comunidades
> > > autónomas y quiero calcular el número diario de nuevos casos y la tasa
> > > diaria de variación del número de casos y no se como hacerlo en R.
> > >
> > > *fecha*
> > >
> > > *comunidad*
> > >
> > > *poblacion*
> > >
> > > *casos_totales*
> > >
> > > 04/03/2020
> > >
> > > Andalucía
> > >
> > > 8414240
> > >
> > > 13
> > >
> > > 05/03/2020
> > >
> > > Andalucía
> > >
> > > 8414240
> > >
> > > 12
> > >
> > > 06/03/2020
> > >
> > > Andalucía
> > >
> > > 8414240
> > >
> > > 21
> > >
> > > 04/03/2020
> > >
> > > Aragón
> > >
> > > 1319291
> > >
> > > 0
> > >
> > > 05/03/2020
> > >
> > > Aragón
> > >
> > > 1319291
> > >
> > > 1
> > >
> > > 06/03/2020
> > >
> > > Aragón
> > >
> > > 1319291
> > >
> > > 6
> > >
> > > 04/03/2020
> > >
> > > Asturias
> > >
> > > 1022800
> > >
> > > 2
> > >
> > > 05/03/2020
> > >
> > > Asturias
> > >
> > > 1022800
> > >
> > > 5
> > >
> > > 06/03/2020
> > >
> > > Asturias
> > >
> > > 1022800
> > >
> > > 5
> > >
> > > Gracias
> > >
> > >       [[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
>


-- 
Saludos,
Carlos Ortega
www.qualityexcellence.es

	[[alternative HTML version deleted]]



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