Tell it it’s a data frame, or: How to apply a function over an entire R data frame and get a data frame back

The “apply” family of functions are very useful (and usually fast), but when running a function over an entire data frame a data frame is not returned. To get one, just tell the result it is one:

# with a data frame, "data," strip dollar signs using column-wise apply()

newdata <- data.frame(apply(data,2,function(x) gsub(“\\$”,”“,x)),stringsAsFactors=F)
Advertisements
This entry was posted in data munging, R and tagged , , , , . Bookmark the permalink.