1

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

zx8754
  • 52,746
  • 12
  • 114
  • 209
rororo
  • 815
  • 16
  • 31

2 Answers2

3

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>
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
2

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>
Marco_CH
  • 3,243
  • 8
  • 25