1
votes

Below dataframe has both string and character,

Input

I need to detect a particular character(Here its L or H) in column 'Data'and display the same in new column.

My sample output should be

Output

Please share your sample data as copy/pasteable R syntax, e.g. dput(your_data[1:4, ]) to make a copy/pasteable version of the first 4 rows. It's very hard to test a solution on a picture of data. - Gregor Thomas
Also, you've asked quite a few questions, but only Accepted a single answer. I'd suggest going back to some of your old questions and accepting answers that worked--it's a nice way to keep the site tidy (so people know when questions are resolved) and to thank the answerers. - Gregor Thomas
Here's a start, though not exactly the output you present (which is, btw, inconsistent with the text of your question): cbind(dat, lapply(setNames(nm=c("C","L")), function(z) grepl(paste0("\\b",z,"\\b"), dat$Data))) - r2evans
or perhaps better cbind(dat, lapply(setNames(nm=c("C","L")), function(z) ifelse(grepl(paste0("\\b",z,"\\b"), dat$Data),z,NA))) - r2evans