r – Splitting strings into components

For example, I have a data table with several columns:

column A                      column B
key_500:station               2
spectra:key_600:type          9
alpha:key_100:number          12

I want to split the rows of column A into components and create new columns, guided by the following rules:

  • the value between “key_” and “:” will be var1,
  • the next value after “:” will be var2,
  • the original column A should retain the part of string that is prior to “:key_“. If it is empty (as in the first line), then replace "" with an “effect” word.

My expected final data table should be like this one:

column A   column B  var1  var2
effect     2         500   station
spectra    9         600   type
alpha      12        100   number