Kalenji
Kalenji

Reputation: 407

R data frame - replace 0 with calculated values based on condition

Data Frame:

y <- c(1,2,3,1,2,3,1,2,3,1)
Cal <- c(0,0,0,0,0,1,1,1,1,1)
x <- c(seq(20,38,2))

df <- data.frame(y,x,Cal)

What I am trying to do is to replace 0 in Cal column with the mean for each y based on x. For example, in row 1, 0 will be replaced by (1 * 20 + 1 * 26 + 1 *32 + 1 * 38) / 4. The same for 2 and 3 in y.

Upvotes: 0

Views: 135

Answers (1)

www
www

Reputation: 39174

A solution using dplyr.

library(dplyr)

df2 <- df %>%
  group_by(y) %>%
  mutate(Cal = ifelse(Cal == 0, mean(y * x), Cal))
df2
# A tibble: 10 x 3
# Groups:   y [3]
       y     x   Cal
   <dbl> <dbl> <dbl>
 1     1    20    29
 2     2    22    56
 3     3    24    90
 4     1    26    29
 5     2    28    56
 6     3    30     1
 7     1    32     1
 8     2    34     1
 9     3    36     1
10     1    38     1

Upvotes: 1

Related Questions