Search code examples
rregexdataframesubstring

Set to cell value to NA if ends with substring


I have the following dataframe:

df = data.frame(column1=c("abc", "def", "ghi"), column2=c("jki", "lmn", "opq"), column3=c("A-", "B-C", NA))

And I want to set the cell value of column3 to NA if the cell ends with -.

I only succeeded in subsetting the dataframe, which is not what I want:

subset(df, !grepl("*-$", column3))

This is my expected output:

enter image description here


Solution

  • We can use replace + endsWith

    > transform(
    +   df,
    +   column3 = replace(
    +     column3,
    +     endsWith(column3,"-"),
    +     NA
    +   )
    + )
      column1 column2 column3
    1     abc     jki    <NA>
    2     def     lmn     B-C
    3     ghi     opq    <NA>