[Solved] Replacing 0 values with the minimum value of the row in r [closed]


mutate is used to operate on data column-wise. If you want to do this rowwise one way in base R would be to use apply with margin as 1.

gapFill_data <- data

gapFill_data[] <- t(apply(data, 1, function(x) 
                          replace(x, x == 0, min(x[x > 0], na.rm = TRUE))))

gapFill_data

#  a b c d
#1 1 3 1 1
#2 2 2 3 2
#3 3 4 2 2
#4 4 2 1 1
#5 5 3 4 3

data

data <- data.frame(a = 1:5, b = c(3, 0, 4, 2, 3), c = c(0, 3, 2, 1, 4), d = 0)
data
#  a b c d
#1 1 3 0 0
#2 2 0 3 0
#3 3 4 2 0
#4 4 2 1 0
#5 5 3 4 0

3

solved Replacing 0 values with the minimum value of the row in r [closed]