DSSS
DSSS

Reputation: 2051

Changing data frame by row and column name suffix

I have two data frames:

     dput (df1)
     structure(list(ID = c(1, 2, 3, 4, 5), Age = c(29, 40, 60, 30, 
     39), BMI = c(27, 26, 31, 33, 26), A.1 = c(1L, 0L, 1L, 1L, 0L), 
     A.2 = c(0L, 0L, 0L, 0L, 0L), A.3 = c(0L, 1L, 0L, 0L, 1L), 
     A.4 = c(0L, 1L, 0L, 0L, 1L), B.1 = c(1L, 1L, 1L, 0L, 0L), 
     B.2 = c(1L, 1L, 1L, 1L, 1L), B.3 = c(1L, 0L, 1L, 0L, 0L), 
     B.4 = c(1L, 1L, 0L, 1L, 1L)), .Names = c("ID", "Age", "BMI", 
     "A.1", "A.2", "A.3", "A.4", "B.1", "B.2", "B.3", "B.4"), row.names = c(NA, 
     5L), class = "data.frame")

  df1
    ID Age BMI A.1 A.2 A.3 A.4 B.1 B.2 B.3 B.4
  1  1  29  27   1   0   0   0   1   1   1   1
  2  2  40  26   0   0   1   1   1   1   0   1
  3  3  60  31   1   0   0   0   1   1   1   0
  4  4  30  33   1   0   0   0   0   1   0   1
  5  5  39  26   0   0   1   1   0   1   0   1

    dput (df2)
   structure(list(ID = c(1, 2, 3, 4, 5), outcome = c(NA, NA, 2, 
   NA, NA)), .Names = c("ID", "outcome"), row.names = c(NA, -5L), class = "data.frame")


 df2
   ID outcome
  1  1      NA
  2  2      NA
  3  3       2
  4  4      NA
  5  5      NA

My task is to change the data in df1 in accordance with outcome value from df2. If outcome value in df2 is n, all values in df1 columns having >n suffix should be changed to NA for the corresponding ID:

     df1
      ID Age BMI A.1 A.2 A.3 A.4 B.1 B.2 B.3 B.4
    1  1  29  27   1   0   0   0   1   1   1   1
    2  2  40  26   0   0   1   1   1   1   0   1
    3  3  60  31   1   0   NA  NA  1   1   NA  NA
    4  4  30  33   1   0   0   0   0   1   0   1
    5  5  39  26   0   0   1   1   0   1   0   1

Could you please help me to find a solution,I am really stuck with it.

Upvotes: 0

Views: 76

Answers (1)

akrun
akrun

Reputation: 886938

Assuming that the two datasets are ordered by ID

indx1 <- grep("\\.\\d+", colnames(df1))
Suf <- as.numeric(gsub('.*\\.', "", colnames(df1)[indx1]))
indx2 <- !is.na(df2$outcome)
df1[,indx1][indx2,Suf>df2$outcome[indx2]] <- NA
df1
#   ID Age BMI A.1 A.2 A.3 A.4 B.1 B.2 B.3 B.4
#1  1  29  27   1   0   0   0   1   1   1   1
#2  2  40  26   0   0   1   1   1   1   0   1
#3  3  60  31   1   0  NA  NA   1   1  NA  NA
#4  4  30  33   1   0   0   0   0   1   0   1
#5  5  39  26   0   0   1   1   0   1   0   1

Or

df1[indx1] <- Map(function(x,y,z) {x[y >z] <- NA; x } ,
                      df1[indx1], Suf, list(df2$outcome))

Upvotes: 1

Related Questions