Single assignment of elements within the same inside the entire column of a data frame

Why can't I assign a value to an entire column of a data frame, and then one item in the same β€œinside” statement? 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:

  abc 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 that I am doing what I expect if the assignment to the entire column (ie b <-1) is in a different expression "inside" than assignment to one element (ie b [2] <- 20 ) But not if they are in the same "inside."

Is this a mistake or something that I just don't understand about R?

+4
source share
2 answers

I assume that assignments to new columns are performed as you β€œleave” this function. By doing

 c <- 1 c[2] <- 20 

all you really created is the vector c <- c(1, 20) . When R needs to assign this to a new column, the vector is recycled, creating the pattern 1,20,1,20, ... that you see.

+3
source

This is interesting.

This is because c is determined only up to length 2, and after that the typical R "utilization rule accepts and repeats c until it matches the length of the data frame. (And as an aside, this only works for integer multiples: you don't You can replicate a 3 or 4 vector in a data frame of ten 10 rows.)

Refining has its critics. I think this is an asset for the dynamically typed interpreted language R, especially when you want to explore data interactively. "Extending" the data according to the container and expression is usually good, even if it gives an odd puzzle, like here.

+2
source

Source: https://habr.com/ru/post/1436989/


All Articles