Soliciting analogies for arguments to "["
I have come around to thinking of arguments to "[
" as being similar to machine gun belts:
set.seed(10)
datfrm <- data.frame(a= rev(letters[1:10]), b = runif(10), cc = letters[1:10] )
datfrm[datfrm$b < 0.5, "b"] <- 0
so the machine gun belt is only running through the first argument to "[
":
---
---
---
---
datfrm[datfrm$b < 0.5, "b"] <- 0
But if you want to only make assignments to a subset, say from another column:
--- ---
--- ---
--- ---
--- ---
datfrm[ datfrm$b < .5, "cc"] <- 开发者_Python百科datfrm[ datfrm$b < .5, "a"]
The expression datfrm$b
gets repeatedly evaluated and when they are paired on either side of an assignment... all proceeds as expected. So what's wrong or right with this and can it be improved?
You can store the index first so it's only calculated once:
idx <- datfrm$b < .5
datfrm[idx, "cc"] <- datfrm[idx, "a"]
This is simpler to read and less prone to mistakes when making changes.
I think within
probably provides the least typing, so using idx
as above, and replacing the original with the output of within():
datfrm <- within(datfrm, cc[idx] <- a[idx])
精彩评论