Replacing occurrences of a number in multiple columns of data frame with another value in R

后端 未结 4 650
-上瘾入骨i
-上瘾入骨i 2020-12-03 03:09

ETA: the point of the below, by the way, is to not have to iterate through my entire set of column vectors, just in case that was a proposed solution (just

4条回答
  •  伪装坚强ぢ
    2020-12-03 03:42

    Basically data[, 2:3]==4 gave you the index for data[,2:3] instead of data:

    R > data[, 2:3] ==4
           var1  var2
     [1,] FALSE FALSE
     [2,] FALSE FALSE
     [3,] FALSE FALSE
     [4,]  TRUE  TRUE
     [5,] FALSE  TRUE
     [6,] FALSE  TRUE
     [7,] FALSE FALSE
     [8,] FALSE FALSE
     [9,] FALSE FALSE
    

    So you may try this:

    R > data[,2:3][data[, 2:3] ==4]
    [1] 4 4 4 4
    

提交回复
热议问题