web_1920
web_1920

Reputation: 23

R: Loop should return numeric element from string

I have a question how to write a loop in r which goes checks if a certain expression occurs in a string . So I want to check if the the expression “i-sty” occurs in my variable for each i between 1:200 and, if this is true, it should give the corresponding i.

For example if we have “4-sty” the loop should give me 4 and if there is no “i-sty” in the variable it should give me . for the observation.

I used

for (i in 1:200){
  datafram$height <- ifelse(grepl("i-sty", dataframe$Description), i, ".")
}

But it did not work. I literally only receive points. Attached I show a picture of the string variable. enter image description here

Upvotes: 0

Views: 77

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145785

"i-sty" is just a string with the letter i in it. To you use a regex pattern with your variable i, you need to paste together a string, e.g., grepl(paste0(i, "-sty"), ...). I'd also recommend using NA rather than "." for the "else" result - that way the resulting height variable can be numeric.

for (i in 1:200){
  dataframe$height <- ifelse(grepl("i-sty", dataframe$Description), i, ".")
}

The above works syntactically, but not logically. You also have a problem that you are overwriting height each time through the loop - when i is 2, you erase the results from when i is 1, when i is 3, you erase the results from when i is 2... I think a better approach would be to extract the match, which is easy using stringr (but also possible in base). As a benefit, with the right pattern we can skip the loop entirely:

library(stringr)

dataframe$height = str_match(string = dataframe$Description, pattern = "[0-9]+-sty")[, 2]
# might want to wrap in `as.numeric`

You use both datafram and dataframe. I've assumed dataframe is correct.

Upvotes: 1

Related Questions