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
No hay comentarios:
Publicar un comentario