Sírvete: https://stackoverflow.com/questions/7014387/whats-the-difference-between-1l-and-1
Saludos, Carlos Ortega www.qualityexcellence.es El 16 de noviembre de 2017, 15:37, Jesús Para Fernández < j.para.fernan...@hotmail.com> escribió: > Una duda sobre 1L, 0L... > > He visto que se pone en data.table, ¿es para indicar que es un numero > entero y obligar a que tenga ese formato? ¿por que no solo poner 1, 0... en > vez de 1L,0L...? > ------------------------------ > *De:* Carlos Ortega <c...@qualityexcellence.es> > *Enviado:* jueves, 16 de noviembre de 2017 13:33 > *Para:* Jesús Para Fernández > *Cc:* r-help-es@r-project.org > *Asunto:* Re: [R-es] Manera eficiente de añadir el valor anterior por > grupo > > Hola, > > Esta es una forma: > > > library(data.table)> datos<-data.table(grupo=rep(c("a","b"), > > each=5),x=c(1:10),y=rnorm(10,2,1))> > new_datos <- datos[, > > .SD[1:(.N+1)],by=grupo]> # datos[, new_y:= shift(y,1), by=grupo]> > > > new_datos[, new_x := shift(y,1), by=grupo]> new_datos[, x:= ifelse(is.na > > <https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fis.na&data=02%7C01%7Cj.para.fernandez%40hotmail.com%7Ca00e675c854c4949def508d52cee32dd%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636464323916834208&sdata=znykutMonHcdfngOxQF5bxhNeWMkoafEa%2BbT2EV18rw%3D&reserved=0>(x), > > 0L, x ), by=grupo]> new_datos$x <- as.double(new_datos$x)> > new_datos[ > > ,x:= ifelse(x == 0, new_x, x), by=grupo]> new_datos$new_x <- NULL> > > new_datos grupo x y > 1: a 1.0000000 2.2626549 > 2: a 2.0000000 1.4232653 > 3: a 3.0000000 1.4361233 > 4: a 4.0000000 1.5171921 > 5: a 5.0000000 1.2444494 > 6: a 1.2444494 NA > 7: b 6.0000000 2.1755191 > 8: b 7.0000000 3.7519007 > 9: b 8.0000000 3.5258307 > 10: b 9.0000000 2.8897186 > 11: b 10.0000000 0.9801489 > 12: b 0.9801489 NA > > > He tenido que hacer varias operaciones con las que no contaba, debido al > tipo de la columna "x". > > Saludos, > Carlos Ortega > www.qualityexcellence.es > <https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.qualityexcellence.es&data=02%7C01%7Cj.para.fernandez%40hotmail.com%7Ca00e675c854c4949def508d52cee32dd%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636464323916834208&sdata=XheKo2c6OF75qPU8y680nuuj2W9pNr%2F7GGMysXYJ9%2Bc%3D&reserved=0> > > > > El 16 de noviembre de 2017, 11:33, Jesús Para Fernández < > j.para.fernan...@hotmail.com> escribió: > > Buenas > > Tengo un Data table de la siguiente manera: > > > datos<-data.table(grupo=rep(c("a","b"),5),x=c(1:10),y=rnorm(10,2,1)) > > > Lo que quiero es añadir una fila por cada grupo y en esa nueva fila, al > valor de la x ponerle el valor anterior de la y > > Lo que hago es añadir una nueva fila por grupo, con: > datos[,.SD[1:(.N+1)],by=grupo] > > Y para añadir el valor anterior uso la función shift, pero lo estoy > haciendo sacando en que filas de las x hay NA y cogiendo el valor anterior, > pero todo casero. > > ¿HAy algguna manera eficiente de hacerlo en data.table? > > He probado con: > datos[is.na > <https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fis.na&data=02%7C01%7Cj.para.fernandez%40hotmail.com%7Ca00e675c854c4949def508d52cee32dd%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636464323916834208&sdata=znykutMonHcdfngOxQF5bxhNeWMkoafEa%2BbT2EV18rw%3D&reserved=0> > (y),x:=shift(y,1),by=grupo] > > Pero no funciona. > > Gracias! > Jesús > > [[alternative HTML version deleted]] > > > _______________________________________________ > R-help-es mailing list > R-help-es@r-project.org > https://stat.ethz.ch/mailman/listinfo/r-help-es > <https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help-es&data=02%7C01%7Cj.para.fernandez%40hotmail.com%7Ca00e675c854c4949def508d52cee32dd%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636464323916834208&sdata=Sl86lkdEaJeuBDAF31CpCXVr3BTXzAkm9QXiOBBI9MU%3D&reserved=0> > > > > > -- > Saludos, > Carlos Ortega > www.qualityexcellence.es > <https://eur02.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.qualityexcellence.es&data=02%7C01%7Cj.para.fernandez%40hotmail.com%7Ca00e675c854c4949def508d52cee32dd%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636464323916834208&sdata=XheKo2c6OF75qPU8y680nuuj2W9pNr%2F7GGMysXYJ9%2Bc%3D&reserved=0> > -- Saludos, Carlos Ortega www.qualityexcellence.es [[alternative HTML version deleted]] _______________________________________________ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es