Why is it that I can’t assign a value to an entire column of a data frame, and then a single element in the same “within” statement? The code:
foo <- data.frame( a=seq(1,10) )
foo <- within(foo, {
b <- 1 # set all of b to 1
})
foo <- within(foo, {
c <- 1 # set all of c to 1
c[2] <- 20 # set one element to 20
b[2] <- 20
})
foo
Gives:
a b c
1 1 1 1
2 2 20 20
3 3 1 1
4 4 1 20
5 5 1 1
6 6 1 20
7 7 1 1
8 8 1 20
9 9 1 1
10 10 1 20
The value of b is what I expected. The value of c is strange. It seems to do what I expect if the assignment to the entire column (ie b <- 1) is in a different “within” statement than the assignment to a single element (ie b[2] <- 20). But not if they’re in the same “within”.
Is this a bug, or something I just don’t understand about R?
My guess is that the assignments to new columns are done as you “leave” the function. When doing
all you have really created is a vector
c <- c(1, 20). When R has to assign this to a new column, the vector is recycled, creating the 1,20,1,20,… pattern you are seeing.