Why does is.na() change its argument?

前端 未结 1 875
自闭症患者
自闭症患者 2021-02-20 02:16

I just discovered the following behaviour of the is.na() function which I don\'t understand:

df <- data.frame(a = 5:1, b = \"text\")
df
##   a            


        
相关标签:
1条回答
  • 2021-02-20 02:45

    The actual function being used here is not is.na() but the assignment function `is.na<-`, for which the default method is `is.na<-.default`. Printing that function to console we see:

    function (x, value) 
    {
        x[value] <- NA
        x
    }
    

    So clearly, value is supposed to be an index here. If you index a data.frame like df["0"], it will try to select the column named "0". If you assign something to df["0"], the column will be created and filled with (in this case) NA.

    To clarify, `is.na<-` sets values to NA, it does not replace NA values with something else.

    0 讨论(0)
提交回复
热议问题