-1

I need to create a new variable and assign values to the row based on another categorical variable. The data table looks like this

Specifically, I want to create a variable called channel_num. If the strings in channelGrouping equal to "Direct", "Display" and "Paid Search", I will assign 0 to this row; if they equal to "Organic Search" and "Social", I will assign 1.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Ryan Tao
  • 33
  • 1
  • 6
  • Welcome to StackOverflow. Please take a look at these tips on how to produce a [minimum, complete, and verifiable example](http://stackoverflow.com/help/mcve), as well as this post on [creating a great example in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Perhaps the following tips on [asking a good question](http://stackoverflow.com/help/how-to-ask) may also be worth a read. – lmo Jan 05 '17 at 20:50

1 Answers1

0

Assuming your datatable is named df:

df$channel_num <- ifelse(df$channelGrouping %in% c("Direct","Display","Paid Search"), 0, ifelse(df$channelGrouping %in% c("Organic Search","Social"), 1, NA))
krish
  • 1,388
  • 2
  • 18
  • 28