0

I have the following dataset,

      company product_code product_number                                FullAddress             name
1     philips            p              5 Groningensingel 147,arnhem,the netherlands    dhr p. jansen
2     philips            p             43 Groningensingel 148,arnhem,the netherlands    dhr p. hansen
3     philips            x              3 Groningensingel 149,arnhem,the netherlands    dhr j. Gansen
4     philips            x             34 Groningensingel 150,arnhem,the netherlands    dhr p. mansen
5     philips            x             12 Groningensingel 151,arnhem,the netherlands   dhr p. fransen
6     philips            p             23 Groningensingel 152,arnhem,the netherlands  dhr p. franssen
7        akzo            v             43   Leeuwardenweg 178,arnhem,the netherlands    dhr p. bansen
8        akzo            v             12   Leeuwardenweg 179,arnhem,the netherlands    dhr p. vansen
9        akzo            x              5   Leeuwardenweg 180,arnhem,the netherlands   dhr p. bransen

I am trying to create two new variables (product_philips and product_akzo) based on the values of 'company' variable, i.e., philips and akzo based on the below condition.

I am trying to execute the following statements,

> refine_original %>% mutate(product_philips = (if_else(grepl(pattern='philips', x=refine_original$company), 1, 0)))

> refine_original %>% mutate(product_ = (if_else(grepl(pattern='philips', x=refine_original$company), 1, 0)))

I want to instead of running twice is there any way I can run at once, I know another way using case_when function. But I want to use the same logic and functions as used above and achieve the result of having one statement instead of running multiple statements.

I want to achieve everything using dplyr package as I want to learn it more. I would be more than happy if someone could provide their viewpoints.

cyborg
  • 431
  • 1
  • 6
  • 20
  • If you want to turn a column into several column, you might be looking for `tidyr::spread`. – Axeman Jul 06 '17 at 07:53
  • Sorry, better target is [this](https://stackoverflow.com/questions/5048638/automatically-expanding-an-r-factor-into-a-collection-of-1-0-indicator-variables). So try `model.matrix(~company - 1, data = refine_original)`. – Axeman Jul 06 '17 at 08:03
  • I want to keep the company column as it is and add a new column based on the pattern i.e., philips and akzo – cyborg Jul 06 '17 at 13:45
  • `bind_cols(refine_original, model.matrix(~company - 1, data = refine_original)` should do the trick, for any number of companies and it's really fast. – Axeman Jul 06 '17 at 13:48

0 Answers0