Replace a single column values

后端 未结 2 711
再見小時候
再見小時候 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)
    
    0 讨论(0)
  • 2020-12-31 22:33

    Load your data by (for others to easily do that for your reproducible example):

    > text <- textConnection("     datay dataz
    +  [1,]     0   100
    +  [2,]     2   101
    +  [3,]     3   102
    +  [4,]     4   103
    +  [5,]    10     0
    +  [6,]    11     0
    +  [7,]     0     0
    +  [8,]     0     0
    +  [9,]     0     0
    + [10,]    12    11
    + [11,]    45    12")
    > df <- read.table(text, header=TRUE)
    

    Or by using the output of dput applied to your data frame:

    > df <- structure(list(datay = c(0L, 2L, 3L, 4L, 10L, 11L, 0L, 0L, 0L, 
    12L, 45L), dataz = c(100L, 101L, 102L, 103L, 0L, 0L, 0L, 0L, 
    0L, 11L, 12L)), .Names = c("datay", "dataz"), class = "data.frame", row.names = c("[1,]", 
    "[2,]", "[3,]", "[4,]", "[5,]", "[6,]", "[7,]", "[8,]", "[9,]", 
    "[10,]", "[11,]"))
    

    To change "all 0 values in column dataz to values of 1":

    > df$dataz[df$dataz == 0] <- 1
    > df
          datay dataz
    [1,]      0   100
    [2,]      2   101
    [3,]      3   102
    [4,]      4   103
    [5,]     10     1
    [6,]     11     1
    [7,]      0     1
    [8,]      0     1
    [9,]      0     1
    [10,]    12    11
    [11,]    45    12
    
    0 讨论(0)
提交回复
热议问题