I have a data.table like this:
data.table(a=rep(c("xx", "yy"), each=4), b=rep(c("zz", "nn"), each=2), vals=10:17)
a b vals
1: xx zz 10
2: xx zz 11
3: xx nn 12
4: xx nn 13
5: yy zz 14
6: yy zz 15
7: yy nn 16
8: yy nn 17
What i want is this, since it looks better in a table when exported to excel and then to words (I know, never use excel...):
a b vals
1: xx zz 10
2: NA NA 11
3: NA nn 12
4: NA NA 13
5: yy zz 14
6: NA NA 15
7: NA nn 16
8: NA NA 17
EDIT: forgot to say that if a numeric value is recurring, it should not be changed to NA, only character columns.
Using rleid from data.table we can create a function
library(data.table)
replace_duplicated <- function(x) {
replace(x, duplicated(rleid(x)), NA)
}
and now apply it to selected columns (Thanks to @markus)
cols = names(df)[sapply(df, is.character)]
df[,(cols) := lapply(.SD, replace_duplicated ), .SDcols = cols]
df
# a b vals
#1: xx zz 10
#2: <NA> <NA> 11
#3: <NA> nn 12
#4: <NA> <NA> 13
#5: yy zz 14
#6: <NA> <NA> 15
#7: <NA> nn 16
#8: <NA> <NA> 17
In dplyr we can use mutate_if
library(dplyr)
df %>% mutate_if(is.character, replace_duplicated)
or mutate_at
df %>% mutate_at(cols, replace_duplicated)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With