Transform duplicate values to 0 by keeping an element in R with conditionals

3

Suppose there is a database where:

x    y     z
a  2015  122.4
a  2015  122.4
b  2016  200.5
a  2014  300.6
c  2016  80.1

What I was wondering is to do a programming in the R that transforms the repeated values of z of each group x and y into 0, keeping a value of z, the final result being:

x    y     z
a  2015  122.4
a  2015    0
b  2016  200.5
a  2014  300.6
c  2016  80.1

How to do this?

    
asked by anonymous 11.09.2017 / 19:23

2 answers

3

See the duplicated function.

dados$z[duplicated(dados$z)] <- 0
dados
  x    y     z
1 a 2015 122.4
2 a 2015   0.0
3 b 2016 200.5
4 a 2014 300.6
5 c 2016  80.1

If you want to keep the original base, make a copy in advance and modify the copy, dados2 <- dados , followed by the above code (with dados2 ).

Data:

dados <-
structure(list(x = structure(c(1L, 1L, 2L, 1L, 3L), .Label = c("a", 
"b", "c"), class = "factor"), y = c(2015L, 2015L, 2016L, 2014L, 
2016L), z = c(122.4, 0, 200.5, 300.6, 80.1)), .Names = c("x", 
"y", "z"), row.names = c(NA, -5L), class = "data.frame")

EDITION.

After looking at Rafael Cunha's answer I noticed that a fundamental point of the question had failed, that repeated or duplicate values should be grouped by columns x and y . The following answer addresses this PO requirement.

dados$z <- ave(dados$z, dados$x, dados$y, FUN = function(.z)
                ifelse(duplicated(.z), 0, .z))
    
11.09.2017 / 20:44
4

Following code, using the dplyr package. In this case, I take into account the grouping of x and y

library(dplyr)
dados <-
  structure(list(
    x = structure(c(1L, 1L, 2L, 1L, 3L), 
                  .Label = c("a", "b", "c"), class = "factor"), 
    y = c(2015L, 2015L, 2016L, 2014L, 2016L), 
    z = c(122.4, 122.4, 200.5, 300.6, 80.1)), 
    .Names = c("x", "y", "z"), row.names = c(NA, -5L), class = "data.frame")

dados %>% 
  group_by(x, y) %>% 
  transmute(z = ifelse(duplicated(z), 0, z))
    
11.09.2017 / 21:58