0

I have had a problem in my R code that comes up from time to time, where I will try to overwrite values of a variable using a subscripted assignment, and some/all of the values do not get overwritten. (I have since figured out half of the problem, but the second half of the question still applies.)

Here is a simplified example of the code, which compares two variables to see which one is bigger, then finds places where they are equal and sets the "is bigger" variable to -1 to indicate that neither is bigger.

a <- rep(0:2,96)
b <- rep(0:3,72)
dataset <- data.frame(cbind(a,b))
dim(dataset) # Show dimensions

> [1] 288   2

# Add a few random NAs
dataset$a[15] <- NA
dataset$b[27] <- NA
dataset$a_bigger <- (dataset$a > dataset$b)
dataset$b_bigger <- (dataset$b > dataset$a)
table(dataset[,c('a_bigger','b_bigger')],useNA='ifany')

>        b_bigger
>a_bigger FALSE TRUE <NA>
>   FALSE    70  144    0
>   TRUE     72    0    0
>   <NA>      0    0    2

dataset$same <- (dataset$a == dataset$b) # Find values where they are the same and neither is bigger
table(dataset$same,useNA='ifany') # Show that there are NAs in dataset$same.

> FALSE  TRUE  <NA>
>  216    70     2

dataset$same[is.na(dataset$a) | is.na(dataset$b)] <- 0 # Fix the NAs. A and B can't be the same if one of them is NA.
table(dataset$same,useNA='ifany') # Show that there are no longer NAs

>   0   1
> 218  70

dataset$a_bigger[dataset$same] <- -1
dataset$b_bigger[dataset$same] <- -1
table(dataset[,c('a_bigger','b_bigger')],useNA='ifany') # Wait, there should be 70 changed, not 1...?

>         b_bigger
> a_bigger  -1   0   1 <NA>
>    -1     1   0   0    0
>    0      0  69 144    0
>    1      0  72   0    0
>    <NA>   0   0   0    2

Up to this point, I have figured out what happened. Setting a few values of "same" to 0 changed it from logical true/false to 0/1, and then when I used it to index another variable, the "1s" were taken to mean "overwrite the first row" instead of as logical trues.

This had me confused since in other contexts R will treat 0/1 as equivalent to true/false (in fact, if I rewrite the assignment line as dataset$a_bigger[dataset$same & dataset$same] <- -1, that works) but at least I can understand what's happening now.

But I still don't understand why it does this:

dataset$even_weirder[dataset$same] <- -1 # But now if I do the assignment on a column/variable that's not initialized...
table(dataset[,'even_weirder'],useNA='ifany') # They all change!!!

>  -1
> 288

If it really thinks that when I write dataset$somevar[dataset$same] I'm referring to position 0 (which it ignores) and position 1 (which it overwrites over and over), then when I do it with an uninitialized column why does it assign -1 to every row instead of assigning it to the first row and leaving the rest NA?

1 Answer 1

0

The issue is basically

class(dataset$same)
#[1] "numeric"

which is not logical but binary i.e. 0 and 1

head(dataset$same)
#[1] 1 1 1 0 0 0

Instead it should be

as.logical(dataset$same)

Because the assignment is haappening at index position 1 i.e. the value -1 is getting updated on the 1st element and not anywhere else

dataset$a_bigger[as.logical(dataset$same)] <- -1
dataset$b_bigger[as.logical(dataset$same)] <- -1

table(dataset[,c('a_bigger','b_bigger')],useNA='ifany')
#        b_bigger
#a_bigger  -1   0   1 <NA>
#    -1    70   0   0    0   #### 70 is showing up now
#    0      0   0 144    0
#    1      0  72   0    0
#    <NA>   0   0   0    2

Regarding the 'even_weider', it is created on the fly and because of that the first element when it gets assigned to -1, gets recycled to the entire length of the column

dataset$even_weirder[dataset$same]
#NULL
dataset$even_weirder[dataset$same] <- -1
sum(dataset$same)
#[1] 70
table(dataset[,'even_weirder'],useNA='ifany')

# -1 
#288 
dataset$even_weirder
#  [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
# [39] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
# [77] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
#[115] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
#[153] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
#[191] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
#[229] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
#[267] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
4
  • Thanks! I understand that part now, but what I haven't figured out is the bottom part with the uninitialized column where instead of thinking the "1" refers to row 1, it writes to every row.I understand that 0/1 aren't functioning as logical here, but they don't seem to be functioning as numeric indexes either (at least in the second part) and I'm curious what it's doing! Commented Feb 15, 2020 at 3:34
  • @TiredSquirrel Consider this v1 <- c(1, 2, 3, 4, 5); i1 <- c(1, 0, 1, 0, 1); v1[i1] <- -1
    – akrun
    Commented Feb 15, 2020 at 3:36
  • The value changed only v1# [1] -1 2 3 4 5 because 1 is. only positioin it is mentioned and 0 is not considered as R indexing starts from 1
    – akrun
    Commented Feb 15, 2020 at 3:37
  • In this part, it seems to think that my 0s and 1s refer to rows other than row 1: dataset$even_weirder[dataset$same] <- -1 and table(dataset[,'even_weirder'],useNA='ifany') shows 288 instances of -1, instead of one -1 and 287 NAs. Commented Feb 15, 2020 at 3:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.