Last active
August 29, 2015 14:20
-
-
Save fredbenenson/2d72f23eb035bcf101c3 to your computer and use it in GitHub Desktop.
Data Frame Indexing Bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
certifiedwaif
commented
May 2, 2015
Strangely, when I define a function called f with the same content, and then call it with the arguments
f(wrong, z, 1)
the function works as expected, and returns the data frame
x y z
1 0.6707523 0.6788389 1
2 0.7418809 0.4239940 1
3 0.5061432 0.2350612 1
4 0.3961475 0.8396474 NA
5 0.2268333 0.8911401 1
6 0.5864661 0.6671142 1
7 0.2140100 0.7586803 1
8 0.9915062 0.9027944 1
9 0.4577299 0.9190519 1
10 0.3907814 0.6885121 NA
Weird!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment