See the function duplicated
.
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 database, make a copy in advance and modify the copy, dados2 <- dados
, followed by the above code (with dados2
).
Dice:
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 seeing Rafael Cunha’s answer I noticed that a fundamental point of the question had failed, that the repeated or duplicated values should be grouped by the columns x
and y
. The following reply gives account of this request of the PO.
dados$z <- ave(dados$z, dados$x, dados$y, FUN = function(.z)
ifelse(duplicated(.z), 0, .z))