Ana
Ana

Reputation:

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

I have data frame which looks like this:

df=data.frame(
  eye_problemsdisorders_f6148_0_1=c("A","C","D",NA,"D","A","C",NA,"B","A"),
  eye_problemsdisorders_f6148_0_2=c("B","C",NA,"A","C","B",NA,NA,"A","D"),
  eye_problemsdisorders_f6148_0_3=c("C","A","D","D","B","A",NA,NA,"A","B"),
  eye_problemsdisorders_f6148_0_4=c("D","D",NA,"B","A","C",NA,"C","A","B"),
 eye_problemsdisorders_f6148_0_5=c("C","C",NA,"D","B","C",NA,"D","D","B"))

In reality I have much more columns and they don't always match "eye_problemsdisorders_f6148" this string, and there is much more rows.

What I would like to do is create a new column, say named "case" where I would have value "1" for every row where string "A" appears at least once in any column, if not the value would be "0". So in the above example column "case" would have these values:

1,1,0,1,1,1,0,0,1,1

Upvotes: 1

Views: 184

Answers (3)

utubun
utubun

Reputation: 4505

In base R you can do it this way (another variation of @Andrew solution):

df$case <- 0^!rowSums(df == 'A', na.rm = T)

#df$case
#[1] 1 1 0 1 1 1 0 0 1 1

Upvotes: 1

M--
M--

Reputation: 29203

We can use apply to loop through each row and + before apply converts Boolean to integer.

df1$case <- +apply(df1, 1, function(x) any(x %in% "A")) 

Upvotes: 1

Andrew
Andrew

Reputation: 5138

This sounds like it is what your looking for. I am happy to explain more if it is helpful!

# Convert sample dataframe from factor to character
df[] <- lapply(df, as.character)

# Check if there at least one value that is "A"
df$case <- as.integer(rowSums(df == "A", na.rm = T) >= 1)

df$case
 [1] 1 1 0 1 1 1 0 0 1 1

Upvotes: 4

Related Questions