I would like to replace duplicated values per site to NA and to keep the first repeated value that appears from left to right.
For example, on site "Alz-Ettelbruck" the value "7" repeats in columns 4 and 5.That means the column 5 of only that site should return NA. The number 12 repeats in all columns on site "Our-Gemund/Vianden" so I would like to keep the 12 in column 2 but the rest should be replaced to NA.
For that I have used the duplicated function but it returns "NULL".
To reproduce the issue, I have used the following data frame and I have indicated at the end the desired output.
Any help would be truly appreciated. Thank you in advance.
df <- data.frame(stringsAsFactors = FALSE,
check.names = FALSE,
Site = c("Att-Bissen","Alz-Ettelbruck","Our-Gemund/Vianden",
"Syre Felsmuhle/Mertert","Ernz Blanche-Larochette"),
`2001-12-01 to 2021-12-01` = c(12, 1, 12, 1, 8),
`1991-12-01.to 2021-12-01` = c(5, 4, 12, 6, 14),
`1981-12-01 to 2021-12-01` = c(12, 7, 12, 20, 14),
`1971-12-01 to 2021-12-01` = c(19, 7, 12, 13, 14))
# Replace repeated values with NA per row
data <- for (i in 1:nrow(df)) {
df[i, -1][duplicated(df[i, -1])] <- NA
}
The following is what I would like the script to return:

(Edit: the first version of both base-R and dplyr+tidyr code used
duplicated, which would falsely-remove the12in row 1 column 4. It has been edited to fix that to not useduplicated.)base R
A reduction comparing column to updated-column.
I hard-coded
df[,-1]in both places, it could easily also bedf[,2:5], it just needs to be the same in both places (LHS of<-and within theReduce).dplyr+tidyr
This loses some efficiency because it double pivots.
One side-effect of the pivoting is that the order of rows and columns is not guaranteed to be restored, so I added the mostly-aesthetic
slice(.) %>% select(.)to the end to marry up with your input data. (It is not at all required.)Data