2018-04-25

Calcular la diferencia entre filas alternas en R

Problema

Queremos calcular la diferencia entre filas alternas en R.

df <- 
structure(list(MemId = c(1, 2, 3, 4, 5,6), ET = structure(c(1506829256,
 1506829319, 1506843096,1506843226, 1506850144, 1506853708), class = 
c("POSIXct", "POSIXt"))), .Names = c("MemId", "ET"), row.names = c("1",
 "2", "14", "15", "37", "38"), class = "data.frame")
  MemId                  ET
1      1 2017-10-01 05:40:56
2      2 2017-10-01 05:41:59
14     3 2017-10-01 09:31:36
15     4 2017-10-01 09:33:46
37     5 2017-10-01 11:29:04
38     6 2017-10-01 12:28:28

Solución

Creamos una columna auxiliar con ceros y unos. Y con la función difftime, calculamos para los unos la diferencia entre un momento en el tiempo (ET) y el anterior (lag(ET)).

library(dplyr)
df %>%
  mutate(id = rep_len(0:1, nrow(df))) %>%
  mutate(dif = ifelse(id == 1, difftime(ET, lag(ET), units = "secs"), NA))
  MemId                  ET id  dif
1     1 2017-10-01 05:40:56  0   NA
2     2 2017-10-01 05:41:59  1   63
3     3 2017-10-01 09:31:36  0   NA
4     4 2017-10-01 09:33:46  1  130
5     5 2017-10-01 11:29:04  0   NA
6     6 2017-10-01 12:28:28  1 3564

Entradas relacionadas

Referencias

2018-04-15

Recrear un objeto con la función dput en R

Problema

Deseamos recrear un objeto en R. Es decir, queremos el código que nos permite crear dicho objeto, en lugar de importarlo. En nuestro ejemplo un data frame cargado en nuestro directorio de trabajo, del que mostramos la parte inicial y final.

library(psych)
headTail(df)
    time group value
1      1     A     3
2      2     A     3
3      3     A     8
4      4     A     5
...  ...     ...
47     7     E     7
48     8     E     8
49     9     E    10
50    10     E     3

Solución

La función dput nos permite recrear dicho objeto.

dput(df)
 structure(list(time = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), group = c("A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "E", 
"E", "E", "E", "E", "E", "E", "E", "E", "E"), value = c(3L, 3L, 
8L, 5L, 4L, 4L, 9L, 6L, 7L, 4L, 5L, 2L, 6L, 8L, 6L, 7L, 6L, 2L, 
4L, 10L, 3L, 7L, 3L, 2L, 8L, 6L, 5L, 3L, 1L, 7L, 6L, 9L, 7L, 
4L, 6L, 4L, 7L, 6L, 7L, 7L, 3L, 4L, 10L, 2L, 7L, 10L, 7L, 8L, 
10L, 3L)), .Names = c("time", "group", "value"), row.names = c(NA, 
-50L), class = "data.frame")
Si quisiéramos crear de nuevo el objeto.

df <- structure(list(time = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L), group = c("A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "C", "C", "C", "C", "C", "C", "C", "C", 
"C", "C", "D", "D", "D", "D", "D", "D", "D", "D", "D", "D", "E", 
"E", "E", "E", "E", "E", "E", "E", "E", "E"), value = c(3L, 3L, 
8L, 5L, 4L, 4L, 9L, 6L, 7L, 4L, 5L, 2L, 6L, 8L, 6L, 7L, 6L, 2L, 
4L, 10L, 3L, 7L, 3L, 2L, 8L, 6L, 5L, 3L, 1L, 7L, 6L, 9L, 7L, 
4L, 6L, 4L, 7L, 6L, 7L, 7L, 3L, 4L, 10L, 2L, 7L, 10L, 7L, 8L, 
10L, 3L)), .Names = c("time", "group", "value"), row.names = c(NA, 
-50L), class = "data.frame")
Si en lugar de recrear el código, quremos guardar el objeto en el directorio para acceder a él posteriormente:

# Guardarmos objeto en el directorio de trabajo
dput(df. "df")
# Volvemos a traerlo al directorio de trabajo
df_nuevo <- dget("df")

Referencias

Nube de datos