Skip to content
Prev 380554 / 398500 Next

How to create a new column based on the values from multiple columns which are matching a particular string?

Hi Bert,

see inline.
On 7/30/19 1:12 AM, Bert Gunter wrote:
fun4 <- function(df, what)
{
   as.integer(rowSums(df == what, na.rm = TRUE) > 0)
}

The function above works for data.frame and matrix inputs as well. It is 
slower than fun3() if 'df' is a data.frame, but is faster if 'df' is a 
matrix (which is a more efficient representation of the data if it 
contains only character columns).

A note to Ana: 'df' is the name of a function in R (see ?stats::df); not 
a perfect choice for a variable name.

Cheers,
Denes