I want to create a new variable by extracting elements that contain a specific string in the contents of a column in R language.

Asked 2 years ago, Updated 2 years ago, 35 views

ID Date Day
11/1 Sunday 22/1 Tuesday/Holiday
3 Wednesday, March 1 ·
·
·
I would like to treat data like this as a different variable if it includes holidays.

ph<-grep("holiday", data)

I got the line as , but I'm at a loss how to create a variable after this.

r

2022-09-30 17:49

1 Answers

grep has been able to get the column number, so

ph<-data [grep("holiday", data),]

Alternatively, use grepl to return TRUE or FALSE

ph<-subset(data, grepl("holiday", data))


2022-09-30 17:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.