-1

column1 with list of all oscar winning films and column2 with random films which includes oscar won and not won films.

column1       column2       oscar won films of column2(expected result)          
 Moonlight     Avatar           1
Hacksawridge   Spectre          0
Lala land      John carter      0

I want to find the oscar winning films of my random movies column2. so basically i want to compare columns 1 and 2, find the duplicates,which will be the oscar won films and populate oscar won films of column2 with 0 for no oscar won and 1 for oscar won.I tried duplicated command and it only is giving me true or false.

  • Maybe you just need `with(dat, column2 %in% column1)` . If not, can you make your example a bit more clear, and add the expected result. [ ps by adding data to your question in a nice format makes it easier to geta nice answer : pls have a read of http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ] – user20650 Mar 26 '17 at 17:13
  • 1
    please see the edit – Nithin Nampoothiry Mar 26 '17 at 17:16
  • he meant give code that generates your toy data so we don't have to. but i think i wrote what you need in the answers – jwells Mar 26 '17 at 17:19
  • 1
    Can you explain the `oscar won` values. Do we assume that "Avatar" appears somewhere (but not shown) in `column1` but that the "Spectre" and "John carter" do not? – user20650 Mar 26 '17 at 17:21
  • please see the edit. – Nithin Nampoothiry Mar 26 '17 at 17:29
  • okay, so does the code in my first comment work??? [ ps to convert `TRUE` and `FALSE` to integer use `as.integer` eg. `x = c(TRUE, TRUE, FALSE) ; as.integer(x)` ] – user20650 Mar 26 '17 at 17:32

1 Answers1

0
df$col3 <- ifelse(df$col1 %in% df$col2, 1, 0)

this will return a vector of length col1 and gives 1 if oscar in random to return length col2 and gives 1 if random in oscar just reverse the cols in the ifelse

jwells
  • 92
  • 2
  • 11