Filling certain duplicate values โ€‹โ€‹in data rows using NA

For each row of my data frame, I'm currently trying to select all duplicated values โ€‹โ€‹equal to 4 to set them to NA.

My dataframe is as follows:

dat <- read.table(text = " 1 1 1 2 2 4 4 4 1 2 1 1 4 4 4 4", header=FALSE) 

I need to get:

  1 1 1 2 2 4 NA NA 1 2 1 1 4 NA NA NA 

I found information on how to eliminate duplicate rows or columns, but I really don't know how to proceed here .. thanks so much for any help

+4
source share
3 answers

Sometimes you need to avoid apply , as it destroys the multiclass function of the dataframe objects. This is a by approach:

 > do.call(rbind, by(dat, rownames(dat), function(line) {line[ duplicated(unlist(line)) & line==4 ] <- NA; line} ) ) V1 V2 V3 V4 V5 V6 V7 V8 1 1 1 1 2 2 4 NA NA 2 1 2 1 1 4 NA NA NA 
+5
source

which and apply are useful here.

 > dat <- t(apply(dat, 1, function(X) {X[which(X==4)][-1] <- NA ; X})) > dat [1,] 1 1 1 2 2 4 NA NA [2,] 1 2 1 1 4 NA NA NA 

But there is probably a way to use the transpose ( t ) function here, can someone help me?

+3
source

duplicated can be used this way with apply :

 dat <- t(apply(dat, 1, function(x) {x[duplicated(x) & x == 4] <- NA ; x})) 
+3
source

All Articles