Replace a single column values

后端 未结 2 712
再見小時候
再見小時候 2020-12-31 21:33

How do i replace values from a single column of a dataframe. For example all 0 values in column dataz to values of 1

     datay dataz
 [1,]     0   100
 [2,]         


        
2条回答
  •  失恋的感觉
    2020-12-31 22:17

    This will change values of 5 to NA. You can also use a range.

    df <- data.frame(datay = sample(1:5, 10, replace = TRUE), dataz = sample(letters, 10, replace = TRUE))
    df$datay[df$datay == 5] <- NA
    

    This will find data smaller than 3 and bigger than 1. Use assign (<-) to assign your value.

    df$datay[df$datay < 3 & df$datay > 1]
    

    Here's a quick example of ifelse.

    ifelse(df$dataz == "w", "oi!", NA)
    

提交回复
热议问题