Set to cell value to NA if ends with substring

Viewed 50

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

2 Answers

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>

You could try:

df$column3 = ifelse(grepl("*-$", df$column3), NA, df$column3)

Output:

> df
  column1 column2 column3
1     abc     jki    <NA>
2     def     lmn     B-C
3     ghi     opq    <NA>

Alternative 1: dplyr

library(dplyr)

df %>%
  mutate(column3 = ifelse(grepl("*-$", column3), NA, column3))

  column1 column2 column3
1     abc     jki    <NA>
2     def     lmn     B-C
3     ghi     opq    <NA>

Alternative 2: data.table

library(data.table)

setDT(df)
df[grepl("*-$", column3), column3:=NA]

df

   column1 column2 column3
1:     abc     jki    <NA>
2:     def     lmn     B-C
3:     ghi     opq    <NA>
Related