gregV
gregV

Reputation: 1107

replace zeros with NA conditionally in

In the below df, there are only 2 farms, so each fruit is duplicated. i'd like to replace zeros with NA as follows

df[df==0] <- NA

However, whenever there is a value for either of the fruits, such as for a pear at y2019 with values c(0, 7), i'd like not to replace 0. dplyr solution would be great.

sample data:

df <- data.frame(fruit = c("apple", "apple", "peach", "peach", "pear", "pear", "lime", "lime"),
                    farm = as.factor(c(1,2,1,2,1,2,1,2)), 'y2019' = c(0,0,3,12,0,7,4,6), 
                    'y2018' = c(5,3,0,0,8,2,0,0),'y2017' = c(4,5,7,15,0,0,0,0) )
> df
  fruit farm y2019 y2018 y2017
1 apple    1     0     5     4
2 apple    2     0     3     5
3 peach    1     3     0     7
4 peach    2    12     0    15
5  pear    1     0     8     0
6  pear    2     7     2     0
7  lime    1     4     0     0
8  lime    2     6     0     0

Upvotes: 0

Views: 145

Answers (1)

r2evans
r2evans

Reputation: 161085

library(dplyr)
df %>%
  group_by(fruit) %>%
  mutate_at(vars(starts_with("y20")), ~ if (any(. != 0)) . else NA_real_) %>%
  ungroup()
# # A tibble: 8 x 5
#   fruit farm  y2019 y2018 y2017
#   <chr> <fct> <dbl> <dbl> <dbl>
# 1 apple 1        NA     5     4
# 2 apple 2        NA     3     5
# 3 peach 1         3    NA     7
# 4 peach 2        12    NA    15
# 5 pear  1         0     8    NA
# 6 pear  2         7     2    NA
# 7 lime  1         4    NA    NA
# 8 lime  2         6    NA    NA

Upvotes: 2

Related Questions