Search code examples
rdataframeconditional-statementsmultiple-columns

Replace column based on column names


I have 65 columns, but a sample of data could be as follows:

 df<-read.table (text="  Name   D   A   D   E
    Rose    D   D   C   B
    Smith   B   A   D   D
    Lora    A   A   D   D
    Javid   A   D   D   B
    Ahmed   C   A   E   A
    Helen   B   A   D   D
    Nadia   A   A   D   A

", header=TRUE)

I want to get the following table:

Name    D   A   D   E
Rose    2   1   1   1
Smith   1   2   2   1
Lora    1   2   2   1
Javid   1   1   2   1
Ahmed   1   2   1   1
Helen   1   2   2   1
Nadia   1   2   2   1

The numbers follow the first raw. For example, the second column is D, so all Ds should read 2 and else should read 1. Or in the third column, which is A, all As should read 2 and else should read 1 and so on. Please consider I have 65 columns. I understand I should have different names for the columns, but In this case, I cannot change them as you understand it.


Solution

  • With ifelse and sapply:

    df[2:ncol(df)] <- sapply(2:ncol(df), function(i) ifelse(df[i] == colnames(df[i]), 2, 1))
    

    output

    #> df
       Name D A D E
    1  Rose 2 1 1 1
    2 Smith 1 2 2 1
    3  Lora 1 2 2 1
    4 Javid 1 1 2 1
    5 Ahmed 1 2 1 1
    6 Helen 1 2 2 1
    7 Nadia 1 2 2 1
    

    data

    df <- structure(list(Name = c("Rose", "Smith", "Lora", "Javid", "Ahmed", 
    "Helen", "Nadia"), D = c("D", "B", "A", "A", "C", "B", "A"), 
        A = c("D", "A", "A", "D", "A", "A", "A"), D = c("C", "D", 
        "D", "D", "E", "D", "D"), E = c("B", "D", "D", "B", "A", 
        "D", "A")), class = "data.frame", row.names = c(NA, -7L))