0

I have a simple question, to which I have not been able to find a solution here:

When I want to keep a selection of variables from a data frame, the variables get removed from all copies of that data frame loaded in my workspace.

Is there a way to only remove it from a single data frame?

A reproducible example (only remove it from df and not df2)?

require(data.table)

df <- structure(list(group = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L), x = c(0L, 0L, 0L, 1L, 
1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 1L), 
    time = c(1636L, 1637L, 1638L, 1639L, 1640L, 1641L, 1642L, 
    1683L, 1684L, 1685L, 1686L, 1687L, 1688L, 1689L, 1690L, 1691L, 
    1638L, 1639L, 1640L)), .Names = c("group", "x", "time"), class = "data.frame", row.names = c(NA, 
-19L))



df2 <- df


varstokeep <- c("group","x")
vartodrop <- which(!names(df)%in%varstokeep)
set(df, i=NULL, j=vartodrop, value=NULL)

The reason is that I have a large file, which I use as the basis for multiple (more aggregated) files. Having to load the basic file 6 times would take a lot more time.

lmo
  • 37,904
  • 9
  • 56
  • 69
research111
  • 347
  • 5
  • 18

0 Answers0