Lisanne
Lisanne

Reputation: 33

Replacing rownames of data frame by a sub-string

I have a large dataframe (named test) with different rownames.

> rownames(test)
[1] "U2OS.EV.2.7.9"   "U2OS.PIM.2.7.9"  "U2OS.WDR.2.7.9"  "U2OS.MYC.2.7.9"
[5] "U2OS.OBX.2.7.9"  "U2OS.EV.18.6.9"  "U2O2.PIM.18.6.9" "U2OS.WDR.18.6.9"
[9] "U2OS.MYC.18.6.9" "U2OS.OBX.18.6.9" "X1.U2OS...OBX"   "X2.U2OS...MYC"
[13] "X3.U2OS...WDR82" "X4.U2OS...PIM"   "X5.U2OS...EV"    "exp1.U2OS.EV"
[17] "exp1.U2OS.MYC"   "EXP1.U20S..PIM1" "EXP1.U2OS.WDR82" "EXP1.U20S.OBX"
[21] "EXP2.U2OS.EV"    "EXP2.U2OS.MYC"   "EXP2.U2OS.PIM1"  "EXP2.U2OS.WDR82"
[25] "EXP2.U2OS.OBX"

As you could see, part of the row names have the same partial name. For example every row with partial name MYC I want to change the whole rowname into "MYC". Overall the row names contain 5 factors: MYC, EV, PIM, WDR and OBX.

Upvotes: 2

Views: 1479

Answers (2)

Richie Cotton
Richie Cotton

Reputation: 121057

Alternative using stringr package.

str_extract(x, "MYC|EV|PIM|WDR|OBX")

Upvotes: 2

csgillespie
csgillespie

Reputation: 60462

As @teucer points out, you can't have duplicate row names. Instead, you create a new column in your data frame and use a simple regular expression to extract your factors. For example,

## Your row names
x = c("U2OS.EV.2.7.9", "U2OS.PIM.2.7.9", "U2OS.WDR.2.7.9", "U2OS.MYC.2.7.9",
      "U2OS.OBX.2.7.9", "U2OS.EV.18.6.9", "U2O2.PIM.18.6.9","U2OS.WDR.18.6.9",
      "U2OS.MYC.18.6.9","U2OS.OBX.18.6.9", "X1.U2OS...OBX","X2.U2OS...MYC")

test$rnames = gsub(".*(MYC|EV|PIM|WDR|OBX).*", "\\1", x)

Upvotes: 5

Related Questions