1

So, here's what I'd like to do:

df <- data.frame(a=1:6, b=1:6)
ss <- magicSubset(df, a <= 3)

ss$b <- 100

df$b # should be c(100,100,100,4,5,6)

Is there something like this in R, or in a package? I guess it would not be too hard to implement... are there reasons it's a bad idea?

David
  • 91
  • 1
  • 3
  • Can you explain why you need this? Perhaps there is another option to achieve what you want at the end of it all. – eddi Nov 07 '13 at 18:24
  • 1
    If switch to data.table instead of data.frame is an option for you, data.table usually doesn't perform copy unless explicitly requested --> [EXAMPLE](http://stackoverflow.com/questions/8030452/pass-by-reference-the-operator-in-the-data-table-package) – digEmAll Nov 07 '13 at 18:51
  • Here's the rationale: I am creating a framework where users will regularly have to update a single specific row from a database, in functions that they define themselves. If I could "pass in" that row to the function, it will be much easier for them to write `obj$field <- value` than `objects$field[objects$id==obj$id] <- value`. I'll take a look at data.table, maybe that is it! – David Nov 07 '13 at 21:53
  • I looked at `data.table` and it was very close, but no banana... I think. If `dt` is a data.table, then `dt2 <- dt` will copy by reference. But `dt2 <- dt[1:3,]` creates a separate copy. Or am I missing a function somewhere? The data.table documentation is hard to read... – David Nov 08 '13 at 11:16

1 Answers1

3

You can subset df$b and assign it a value:

df$b[df$a <= 3] <- 100
Christopher Louden
  • 7,540
  • 2
  • 26
  • 29
  • I get that, but I would like to create an object which I can pass around and which updates its "parent" when it gets updated. – David Nov 07 '13 at 18:02
  • 1
    @David R makes copies of everything, there isn't a simple way to get to pointer-like behavior with R variables, like you might in, say, Python. – Matthew Plourde Nov 07 '13 at 18:05
  • @David That said, it sounds like maybe S4 or Reference classes might provide more of a traditional object oriented framework that would serve your purposes. – joran Nov 07 '13 at 18:32
  • I was thinking of something like that. I get frustrated when I have to write code like `df$foo[some_complicated_condition] <- df$foo[some_complicated_condition] + df$bar[some_complicated_condition]`. Just wondering if anyone had beaten me to it. – David Nov 07 '13 at 18:51
  • When I have `some_complicated_condition`, I define it first (`keep <- some_complicated_condition`) and then subset the data frame (`df$foo[keep] <- df$foo[keep] + df$bar[keep]`). It doesn't get to what you are asking for, but it is clearer to me and shorter to write. – Christopher Louden Nov 07 '13 at 18:57
  • @David check out `data.table`, where the syntax would be: `df[condition, foo := foo + bar]` – eddi Nov 07 '13 at 19:16