# This is a reduction which seems to indicate an issue when # adding a column using an arbitrary set of indexes. # First, let's create a data-frame with some random values: s <- data.frame(x = runif(10), y = runif(10)) # Now, two randomly generated lists of numbers that we'll use to try to index # This could be created thusly: # wrong <- sample(1:nrow(s), nrow(s) * 0.8), etc. wrong <- c(3, 6, 7, 5, 1, 2, 9, 8) right <- c(4, 10, 2, 6, 1, 5, 9, 3) # Indexing the df with the vector without 10 (the length of the data.frame s) # generates an error when creating a new column: s$z[wrong] <- 1 > Error in `$<-.data.frame`(`*tmp*`, "z", value = c(1, 1, 1, NA, 1, 1, 1, : replacement has 9 rows, data has 10 # Whereas using the vector *with* 10 does work when creating a new column: s$z[right] <- 1 > s x y z 1 0.28089503 0.60456879 1 2 0.37238938 0.79486150 1 3 0.76933910 0.11040848 1 4 0.90518539 0.09314296 1 5 0.62716400 0.81500552 1 6 0.64327678 0.58516840 1 7 0.05474681 0.23425909 NA 8 0.83767040 0.73871715 NA 9 0.01475207 0.23033121 1 10 0.18314193 0.10045413 1