John Smith John Smith - 3 months ago 15
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(ref.dg.safe[,'safewords'], collapse="|")
"c(\"NO BATTERIES\", \"COSTUME\", \"CABLE\", \"BAG\", \"CLOTHING\)"

b <- paste(ref.dg.safe$safewords, collapse="|")
NO BATTERIES|COSTUME|CABLE|BAG|CLOTHING|


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

UPDATE:

Originally i imported the data set using the line

ref.dg.safe <- unique(tbl_df(read.csv("~/Projects/foo_project/REF_SafeList.txt", sep = "\t", as.is = TRUE, strip.white=TRUE)))


The dput looks like

structure(list(safewords = c("NO BATTERIES", "COSTUME", "CABLE",
"BAG", "CLOTHING", "BRACELET", "FAUCET", "IRON", "CASE", "NO BATTERY",
"BELT", "JACKET", "CONVERTER", "HAIR", "GLASS", "SHOE", "ROUTER",
"LABEL", "ADAPTOR", "SILICONE", "EARPHONE", "SPONGE", "WOOD",
"TANKTOP", "WALLET", "TUBE", "TRIPODS", "STONE", "LAMP", "HEADPHONES",
"COOKIECUTTERS", "CONVERTERS", "COWLEATHER", "INFLATABLETOY",
"HEADPHONE", "LABLE", "ROMPER", "POLE", "PROBE", "FIBEROPTIC",
"APRON", "TABLECLOTH", "AVR", "TABLEBASE", "DESK", "BEAUTYGOODS",
"SEAT", "NOBATTERIES", "SHEOS", "CHARGERS", "STAPLER", "SATCHEL"
)), .Names = "safewords", class = c("tbl_df", "data.frame"), row.names = c(NA,
-52L))

Answer

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="|")
[1] "NO BATTERIES|COSTUME|CABLE|BAG|CLOTHING|BRACELET[...]"

I removed part of the output to keep it readable here