0

I have some data that looks like this:

SS <- structure(list(rn = 
c("Exp.618.1.7..ABC.TRE854.HS.2...1.Saline...1...A.", 
"Exp.618.1.7..ABC.TRE854.HS.2...4.Res..Reference...1...A.", "Exp.618.1.7..ABC.TRE854.HS.2...8.ABC.TRE854.HS.2..100nM...1...A.", 
"Exp.618.1.7..ABC.TRE854.HS.2...12.ABC.TRE854.HS.2..1.00uM...1...A.", 
"Exp.618.1.7..ABC.TRE854.HS.2...16.ABC.TRE854.HS.2..10.0uM...1...A.", 
"Exp.618.2.5..ABC.TRE854.HS.2...1.Saline...1...A.", "Exp.618.2.5..ABC.TRE854.HS.2...4.Res..Reference...1...A.", 
"Exp.618.2.5..ABC.TRE854.HS.2...8.ABC.TRE854.HS.2..300nM...1...A.", 
"Exp.618.2.5..ABC.TRE854.HS.2...12.ABC.TRE854.HS.2..3.0uM...1...A.", 
"Exp.618.2.5..ABC.TRE854.HS.2...16.ABC.TRE854.HS.2..30uM...1...A.", 
"Exp.622.1.2..ABC.TRE854.HS.2...1.Saline...1...A.", "Exp.622.1.2..ABC.TRE854.HS.2...4.Res..Reference...1...A.", 
"Exp.622.1.2..ABC.TRE854.HS.2...8.ABC.TRE854.HS.2..100nM...1...A.", 
"Exp.622.1.2..ABC.TRE854.HS.2...12.ABC.TRE854.HS.2..1.00uM...1...A.", 
"Exp.622.1.2..ABC.TRE854.HS.2...16.ABC.TRE854.HS.2..10.0uM...1...A.", 
"Exp.622.2.5..ABC.TRE854.HS.2...1.Saline...1...A.", "Exp.622.2.5..ABC.TRE854.HS.2...4.Res..Reference...1...A.", 
"Exp.622.2.5..ABC.TRE854.HS.2...8.ABC.TRE854.HS.2..300nM...1...A.", 
"Exp.622.2.5..ABC.TRE854.HS.2...12.ABC.TRE854.HS.2..3.0uM...1...A.", 
"Exp.622.2.5..ABC.TRE854.HS.2...16.ABC.TRE854.HS.2..30uM...1...A."
), V1 = c(6.08174172247795, -273.068131175906, -38.0098754654436, 
-44.1874819464636, -126.058280657819, 28.7111941404515, -326.124708404277, 
-61.0348906065704, -63.7440680070101, -62.8961106505329, 18.9484530926351, 
-607.977222113268, -212.18247673418, -179.193611578799, -230.372071747453, 
11.6278896202125, -258.129269330527, -26.634614887808, -29.8940173506221, 
-63.2992704853608), Exp = c("Exp.618.1.", "Exp.618.1.", "Exp.618.1.", 
"Exp.618.1.", "Exp.618.1.", "Exp.618.2.", "Exp.618.2.", "Exp.618.2.", 
"Exp.618.2.", "Exp.618.2.", "Exp.622.1.", "Exp.622.1.", "Exp.622.1.", 
"Exp.622.1.", "Exp.622.1.", "Exp.622.2.", "Exp.622.2.", "Exp.622.2.", 
"Exp.622.2.", "Exp.622.2."), Value_norm = c(-0.0222718839298028, 
1, 0.139195574751849, 0.16181852402981, 0.461636735546466, -0.0880374697180561, 
1, 0.187151997483457, 0.195459179768711, 0.192859078228946, -0.0311663865083172, 
1, 0.348997411443565, 0.294737376765432, 0.3789156293499, -0.0450467692035472, 
1, 0.103183242089851, 0.115810258279326, 0.245223142069596)), .Names = c("rn", 
"V1", "Exp", "Value_norm"), row.names = c(NA, 20L), class = "data.frame")

In the rn column has some name that i need to use to create a factor so i can plot in GGplot2. Those names are :

Saline
Reference
100nM
300nM
1uM
3uM
10uM
30uM

I want the final to data to look like the example but on the end have a factor column with one of those labels above.

I apologize for only having a picture of my data but i wanted it to be formatted nicely and I couldn't do that in the dialog box here!

Thanks in advance!

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Please post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) rather than pictures of data so possible solutions can be tested. A `dput()` of part of the data would be best so we can see the actual values and data types involved. – MrFlick Nov 13 '15 at 16:24
  • Sorry about that. Edited the original post. – jbrousseau Nov 13 '15 at 16:36

1 Answers1

0

Well, it would be easier if you exactly matched the terms in the column. If that's OK, you can do

rx <- "\\b(Saline|Reference|100nM|300nM|1.00uM|3.0uM|10.0uM|30uM)\\b"
SS$type <- regmatches(SS$rn,regexpr(rx, SS$rn))

This should give the levels of

c("1.00uM", "10.0uM", "100nM", "3.0uM", "300nM", "30uM", "Reference", "Saline")

If you wanted to rename those that are different, you could do

remap <- c("1.00uM"="1uM", "3.0uM"="3uM", "10.0uM"="10uM")
SS$type[SS$type %in% names(remap)] <- remap[SS$type[SS$type %in% names(remap)]]
MrFlick
  • 195,160
  • 17
  • 277
  • 295