Skip to content

Instantly share code, notes, and snippets.

@fredbenenson
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save fredbenenson/2d72f23eb035bcf101c3 to your computer and use it in GitHub Desktop.

Select an option

Save fredbenenson/2d72f23eb035bcf101c3 to your computer and use it in GitHub Desktop.

Revisions

  1. fredbenenson revised this gist May 1, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion data_frame_indexing_bug.r
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,8 @@ s <- data.frame(x = runif(10), y = runif(10))
    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:
    # 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
  2. fredbenenson revised this gist May 1, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion data_frame_indexing_bug.r
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # This is a reduction which seems to indicate an issue with adding a column using an arbitrary set of indexes.
    # 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))
  3. fredbenenson created this gist May 1, 2015.
    31 changes: 31 additions & 0 deletions data_frame_indexing_bug.r
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    # This is a reduction which seems to indicate an issue with 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