0

I have a column that has value which look similar to this:

data.reg2$dummy.DR58

In every row, I want to find "data.reg2$" and replace it with "beta." - all rows have that data.reg2 at the beginning.

I've tried a bunch of variations of gsub, etc., but nothing is actually doing the replacement for some reason. This is the code I currently have (which isn't working):

cluster.model <- gsub('data.reg2$', 'beta.', cluster.model$betas)

Any thoughts?

Luna
  • 39
  • 1
  • 5

1 Answers1

2

Try escaping the dollar sign:

cluster.model$beta2 <- gsub("data\\.reg2\\$", "beta.", cluster.model$betas)

The dollar sign is a special character in regular expressions. The period is too, although in this case it would probably work fine since the period just matches any single character, including a period.

Alternatively, set the fixed parameter to TRUE to disable regular expressions and match literally:

cluster.model$beta2 <- gsub("data.reg2$", "beta.", cluster.model$betas, fixed=TRUE)
BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • thank you. However, it is still not working. I thought it was because my betas variable was stored as a factor, but I have converted it to character now. When I run the gsub syntax the dataset actually disappears and does into my "Values" area in my environment. I've not seen this happen before (but I'm still learning R)...is there something going on I'm not considering maybe? – Luna Apr 17 '14 at 12:20
  • @Luna Minor edit made that hopefully will help (now assigning to `beta2`), but to help you further you'll need to provide a **[reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)**. – BrodieG Apr 17 '14 at 12:24