Stefan
Stefan

Reputation: 895

Find and replace in R

In the following example, how can I replace those instances when italic(P) == 0 with italic(P) < 0.001?

df <- structure(list(STRING = c("italic(R)^2 == 0.15 * \",\" ~ italic(P) == 0", "italic(R)^2 == 0 * \",\" ~ italic(P) == 0", 
"italic(R)^2 == 0.17 * \",\" ~ italic(P) == 0", "italic(R)^2 == 0.15 * \",\" ~ italic(P) == 0", 
"italic(R)^2 == 0 * \",\" ~ italic(P) == 0", "italic(R)^2 == 0.07 * \",\" ~ italic(P) == 0.002", 
"italic(R)^2 == 0.12 * \",\" ~ italic(P) == 0", "italic(R)^2 == 0.11 * \",\" ~ italic(P) == 0", 
"italic(R)^2 == 0.06 * \",\" ~ italic(P) == 0.006", "italic(R)^2 == 0.08 * \",\" ~ italic(P) == 0.001"
)), .Names = c("STRING"), row.names = c(NA, -10L), class = "data.frame")

I tried gsub("== 0", "< 0.001", df$STRING) but that is obviously wrong since it will replace also all the other instances were the pattern is found. I only want to replace those that are 0 and not 0.

I can manually change it with within fix(df) popup window but that is not good practice.

Upvotes: 1

Views: 1702

Answers (2)

hwnd
hwnd

Reputation: 70732

Based on your comment, you would need to modify your regular expression:

gsub('== 0(?=\\h|$)', '< 0.001', df$STRING, perl=TRUE)

CodeBunk

Upvotes: 2

SimonG
SimonG

Reputation: 4871

I think you almost had it. Putting $ at the end of the expression matches only those strings where == 0 occur at the end of the string.

gsub("== 0$", "< 0.001", df$STRING)

This assumes that == 0 actually does occur only at the end (as in your example). If it occurs also "mid-string", then more complex expressions can be found. However, I think that cleaning up those character strings and working with "== 0$" is the cleaner solution.

EDIT:

Acommodating more complicated strings requires more complicated expressions. As per your updated question, the simplest solution I can think of is:

gsub("== 0( |$)", "< 0.001\\1", df$STRING)

This assumes that each match is either followed by a space or the end of the string. () is a grouping, \\1 is a backreference to that group. Please also have a look at the answer by @hwnd who has another solution.

Upvotes: 3

Related Questions