I have dataframe like this
situations <- c("{17: '', 80: '', 55: '693', 29: '', 103: '19', 233: '872', 6: '', 20: '', 230: '99.3', 215: '', 102: '47.7', 56: 'Center', 146: '85.1', 147: '40.6', 23: '', 231: '47.8'}", "{103: '1.9', 18: '', 154: '', 147: '48.6', 22: '', 233: '879', 76: '', 459: '', 55: '719', 29: '', 102: '54.2', 56: 'Center', 328: '', 146: '94.7', 20: ''}", "{215: '', 22: '', 56: 'Center', 233: '731', 103: '19', 78: '', 230: '97.7', 146: '78.2', 20: '', 102: '50.4', 29: '', 18: '', 55: '899', 147: '43.3', 82: '', 231: '48.7'}")
events <- c("A", "B", "C")
df <- data.frame(situations, events)
And I want to filter out rows containing 6 (or 6: ''). Could you help me please?
I've tried grepl to select rows, but output is not desired.
df$filter <- as.integer(grepl('6:', df$situations))
It leaves all values containing 6. Like 76, 146, 56 etc
In Python I use such simple code for this task, but cannot find something similar in R.
df['is_ok'] = df['situations'].apply(lambda x: True if 6 in x else False)
Solution
I've found the desired output thanks to @MrFlick
as.integer(grepl('\\b6:', df$situations))
Thanks everyone