user11916948
user11916948

Reputation: 954

Based on a condition in a column in data frame, remove data in other columns of the same row

I wonder how I remove all the data from all other columns in a row when trt=C. I'm interested in a solution that would work for a lot of columns!

A <- sample(1:100,9)
B <- sample(1:100,9)
trt <- rep(c("A", "B", "C"),3) 

df <- data.frame(trt, A, B)
df

    trt A  B
1   A  32 43
2   B  29 79
3   C  94 95
4   A 100 24
5   B  63 38
6   C  80 92
7   A  22 98
8   B  61 56
9   C  69 81


to look like this:
       trt A  B
    1   A 90  50
    2   B 84 100
    3   C NA  NA
    4   A 42  17
    5   B 61  43
    6   C NA  NA
    7   A 78  84
    8   B 27  36
    9   C NA  NA

Something like...

if(df$trt=="C") {
  df[,i] <- NA
}

Upvotes: 1

Views: 35

Answers (3)

Vishal Katti
Vishal Katti

Reputation: 652

    for (i in 1:nrow(df)) {
      if (df$trt[i] == "C") {
        df[i, ] <- NA
        df$trt[i] <- "C"
      }
    }

Hacky, but works!

Upvotes: 1

Rui Barradas
Rui Barradas

Reputation: 76402

Here is a solution with function is.na<-.

df[-1] <- lapply(df[-1], `is.na<-`, df$trt == "C")
df
#  trt  A  B
#1   A 28 90
#2   B 80 70
#3   C NA NA
#4   A  9 78
#5   B  5 14
#6   C NA NA
#7   A 16 62
#8   B  4  4

Upvotes: 1

Sotos
Sotos

Reputation: 51582

We can use the [ to select rows and columns to alter, i.e.

df[df$trt == 'C', -1] <- NA

which gives,

  trt  A  B
1   A 87 56
2   B 53 18
3   C NA NA
4   A  8  1
5   B 79 38
6   C NA NA
7   A 48 36
8   B 55 22
9   C NA NA

You can also use it to choose specific columns,

df[df$trt == 'C', -c(1, 3)] <- NA

which gives,

  trt  A  B
1   A 96 27
2   B 21 13
3   C NA 34
4   A 43 98
5   B 70 93
6   C NA 64
7   A  2 60
8   B 40 94
9   C NA 11

Upvotes: 2

Related Questions