[R] when use which()
soeren.vogel at eawag.ch
soeren.vogel at eawag.ch
Fri Nov 13 21:23:47 CET 2009
Hello:
# some code to assign with and without "which"
q <- 1:20; q[c(9, 12, 14)] <- NA
r <- 1:20; r[c(8:9, 12:15)] <- NA
s <- 1:20; s[c(8:9, 12:15)] <- NA
r[q < 16] <- 0
s[which(q < 16)] <- 0
r;s # both: 0 0 0 0 0 0 0 0 NA 0 0 NA 0 NA 0 16 17 18 19 20
r <- 1:20; r[c(8:9, 12:15)] <- NA
s <- 1:20; s[c(8:9, 12:15)] <- NA
r[is.na(q)] <- 30
s[which(is.na(q))] <- 30
r;s # both: 1 2 3 4 5 6 7 NA 30 10 11 30 NA 30 NA 16 17 18 19 20
So it appears to me that "a[b] <- c" delivers the same results as
a[which(b)] <- c". Is there any situation where the assignment with/
out which indeed makes a difference?
Thanks for help
Regards
Sören
More information about the R-help
mailing list