0

I want to create 2 variables from 1 variable in R.

I have following character variable for gas station:

station

Valero 1810 N Foster Rd & IH-10 E

from this variable I want to create 2: station_id and address

station_id

Valero

address

1810 N Foster Rd & IH-10 E

In my data set all strings in station variable begin with words (up to 3 words, eg: EZ Mart) and all addresses begin with numeric value.

I was trying to achieve this goal using gsub for last couple hours but I couldn't do it.

Thank you

Behzod A
  • 173
  • 1
  • 2
  • 14
  • Check the above link from @MaxFt. The top suggestion using `tidyr::separate` will solve your problem nicely – divibisan Apr 03 '18 at 21:14

1 Answers1

1

Base R solution: This works for the sample string you give. You need to test if this works for your other cases. It would've been good to include more than one sample string.

ss <- "Valero 1810 N Foster Rd & IH-10 E";
station_id <- trimws(gsub("(\\w+\\s+){1,3}(\\d+.+)$", "\\1", ss));
address <- gsub("(\\w+\\s+){1,3}(\\d+.+)$", "\\2", ss);
station_id;
#[1] "Valero"
address;
#[1] "1810 N Foster Rd & IH-10 E"
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68