I have a dataframe with 2 columns GL
and GLDESC
and want to add a 3rd column called KIND
based on some data that is inside of column GLDESC
.
The dataframe is as follows:
GL GLDESC
1 515100 Payroll-Indir Salary Labor
2 515900 Payroll-Indir Compensated Absences
3 532300 Bulk Gas
4 539991 Area Charge In
5 551000 Repairs & Maint-Spare Parts
6 551100 Supplies-Operating
7 551300 Consumables
For each row of the data table:
- If
GLDESC
contains the wordPayroll
anywhere in the string then I wantKIND
to bePayroll
- If
GLDESC
contains the wordGas
anywhere in the string then I wantKIND
to beMaterials
- In all other cases I want
KIND
to beOther
I looked for similar examples on stackoverflow but could not find any, also looked in R
for dummies on switch, grep, apply and regular expressions to try and match only part of the GLDESC
column and then fill the KIND
column with the kind of account but was unable to make it work.
kind
i.e I want another stringlabor
to be replaced withxyz
in a new variablenew
then do we have to run the regex individually for each such new variable. Also you did not definekind
in the latest iterative function. Thanks! – Madelynmademoiselle