Skip to content
Prev 349828 / 398513 Next

how to Subset based on partial matching of columns?

Hi,

Please don't put quotes around your code. It makes it hard to copy and
paste. Alternatively, don't post in HTML, because it screws up your
code.
On Wed, Apr 8, 2015 at 8:57 PM, samarvir singh <samarvir1996 at gmail.com> wrote:
That's a character vector, not a list. A list is a specific type of object in R.
That doesn't work, and I have no idea what you expect to have there,
so I'm deleting the extra comma. Also, your vector is named code, not
CODE.

code <- c("MY GM+", "LGTY", "RS","TY")
x <- c(1:4)
You problably actually want
mydf <- data.frame(x, code, stringsAsFactors=FALSE)

Note I changed the name, because df() is a base R function.
grepl() will give you a logical match

data.frame(mydf, sapply(code, function(x)grepl(x, mydf$code)),
stringsAsFactors=FALSE, check.names=FALSE)

Sarah