Search code examples
rdataframedplyrconditional-statementsdataset

How replace values in the specific row in a dataframe using R?


I have a dataframe as follows:

df = data.frame(row.names = c("IL6", "AS-1", "KPMG", "DS55", "CD44", "Type"), Class1 = c(1, 19, 18, 11, 2, "ACC"), Class2 = c(3, 12, 20, 9, 12, "ACC"), Class3 = c(6, 5, 9, 16, 9, "BRCA"), Class4 = c(3, 9, 18, 15, 14, "BRCA"), Class5 = c(4, 20, 19, 19, 16, "BRCA"), Class6 = c(3, 12, 20, 9, 12, "SRCA"), Class7 = c(6, 5, 9, 16, 9, "SRCA"), Class8 = c(3, 9, 18, 15, 14, "GBM"))

I want to replace the values in the rowname Type based on other predefined values as ACC=1,BRCA=2,SRCA=3,GBM=4.

The final ouput will be as follows:

output <- data.frame(row.names = c("IL6", "AS-1", "KPMG", "DS55", "CD44", "Type"), Class1 = c(1, 19, 18, 11, 2, "1"), Class2 = c(3, 12, 20, 9, 12, "1"), Class3 = c(6, 5, 9, 16, 9, "2"), Class4 = c(3, 9, 18, 15, 14, "2"), Class5 = c(4, 20, 19, 19, 16, "2"), Class6 = c(3, 12, 20, 9, 12, "3"), Class7 = c(6, 5, 9, 16, 9, "3"), Class8 = c(3, 9, 18, 15, 14, "4"))

I tried like the following way:

#mapping values
mapping <- c("ACC" = "1", "BRCA" = "2", "SRCA" = "3", "GBM" = "4")

#Replace the values in the 'Type' row
data["Type", ] <- mapping[data["Type", ]]

It gave an error as follows:

Error in mapping[data["Type", ]]: invalid subscript type 'list'
Traceback:

Solution

  • Here's one way:

    library(tidyverse)
    
    df["Type",] <- case_match(as.character(df["Type",]),
                                            "ACC" ~ 1,
                                            "BRCA" ~ 2,
                                            "SRCA" ~ 3,
                                            "GBM" ~ 4)
    

    And another way:

    df["Type",] <- str_replace_all(df["Type",], mapping)