0

I have a dataframe that has a column like this:

x       
apple 
orange  
<pear> 
orange 
<straw-berry>

i would now like to add a new column that is populated with TRUE or FALSE based on whether the value of column x contains angle brackets, or e.g. starts with <:

x               y
apple           FALSE
orange          FALSE
<pear>          TRUE
orange          FALSE
<straw-berry>   TRUE

I have tried an approach similar to this, but without success;

d$y<- "False"
d$y[d$x[grep('<', rownames(d$x)),]] <- "True"

I get an incorrect number of dimensions error with that code.

rayne
  • 523
  • 1
  • 7
  • 24
  • 9
    `d$y <- grepl("^<", d$x)`. – Rui Barradas Mar 14 '18 at 15:24
  • 2
    https://stackoverflow.com/questions/4736/learning-regular-expressions – jogo Mar 14 '18 at 15:25
  • The issue with your attempt is the comma after the grep. `d$y` isn't a data frame, it's a 1-dimensional column, but the `[ , ]` is trying to treat it like a 2-dimensional object. Delete that comma and your code will work fine. (Though using a logical `TRUE` is probably better than a string `"True"`. Also your code doesn't check if `x` *starts* with a `<`, it just checks if `x` *contains* a `<`.) – Gregor Thomas Mar 14 '18 at 15:27
  • or `substr(df$x, 1, 1)=='<'` – jogo Mar 16 '18 at 08:51

1 Answers1

1

The str_detect from the stringr package returns TRUE/FALSE if the given string matches a pattern - here "<" :

df$y <- str_detect(x, "<")
TTR
  • 129
  • 5
  • This gives `TRUE` also for elements with '<' in the middle. – jogo Mar 16 '18 at 08:12
  • Yes, you could either subtract the first letter of the string 'x' with str_sub(x, 0, 1) or define the pattern more narrowly, e.g. "^<" – TTR Mar 16 '18 at 09:54