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

Ana Marija @okov|c@@n@m@r|j@ @end|ng |rom gm@||@com
Mon Jul 29 19:45:06 CEST 2019


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,1,1,0
Thanks
Ana

	[[alternative HTML version deleted]]



More information about the R-help mailing list