John Smith John Smith - 1 month ago 5x
R Question

R: Paste a dataframe column for grepl

I have two lines of code in R that should in theory do the same thing
I want to use them to set a value in a column

a <- paste([,'safewords'], collapse="|")

b <- paste($safewords, collapse="|")

I want the second output using the first line of code because i am getting a partial matching error when i use "b" in a function

I would also like to understand why the outputs are so different


Originally i imported the data set using the line <- unique(tbl_df(read.csv("~/Projects/foo_project/REF_SafeList.txt", sep = "\t", = TRUE, strip.white=TRUE)))

The dput looks like

structure(list(safewords = c("NO BATTERIES", "COSTUME", "CABLE",
)), .Names = "safewords", class = c("tbl_df", "data.frame"), row.names = c(NA,


To answer the why:

> class(df[,"safewords"])
[1] "tbl_df"     "data.frame"
> class(df$safewords)
[1] "character"

This is due to how the [ and $operators work and how they coerce their return or not (I don't see how to summarize this, have a look at the docs on data.frame and subset operators).

One workaround is making the first form loose it's data.frame status with unlist like this:

> paste(unlist(df[,"safewords"]),collapse="|")

I removed part of the output to keep it readable here