I've been teaching myself R from scratch, basically by undertaking something, then reading posts like these, and trial-and-error based on that. Sometimes I hit a wall and reach out.
I've hit a wall. I have dplyr 0.7 installed. I have a tibble with a column - call it contract_key
- I added by applying mutate(coalesce()) to three other columns in the tibble. Here's sample data:
product <- c("655393265191","655393265191","168145850127","168145850127","350468621217","350468621217","977939797847","NA","928893912852")
supplier <- c("person5","person3","person10","person5","person11","person5","person11","person14","person5")
vendor <- c("org2","org3","org3","org2","org1","org2","org1","org5","org2")
quantity <- c(7,5,6,1,2,1,18,2,2)
gross <- c(0.0419,0.0193,0.0439,0.0069,0.0027,0.0055,0.0233,NA,0.0004)
df <- data_frame(product,supplier,vendor,quantity,gross)
Here's how I generated contract_key
:
df <- df %>%
mutate(contract_key = coalesce(product,supplier,vendor))
I now want to add another column that categorizes the contents of contract_key
based on which of the three columns provided the content (through coalesce()). So if contract_key ="person5", for example, the new column, contract_level, would be "supplier". And contract_key="org2" would map to contract_level = "vendor", etc.
Essentially, I'll be using contract_level
as a join variable to another tibble.
I'm stumped. I've tried if_else
, and I see that I shouldn't bother trying case_when
(because it's inside mutate()). I've also tried nesting if_else
's to no avail.
It's probably basic R syntax that I just don't know. Something to do with dot notation and grammar. If someone supplies the answer, I will backtrace until I figure out what you did. (And I'll have learned a new lesson in R!)
Thanks!