jeudi 1 février 2018

Replace value of an existing column based on whether it's present in a list

I have a sample data frame as such:

dat <- data.frame(V1 = c("Non Debt Balance","Non Debt Income","Non Debt Cost"), 
              V2 = c("Average Balance","Income","Cost"), 
              V3 = c("Trade","Sales Finance","Trade"))

I would like to create a new column based on the value of column V2. If column V2 is %in% ("Income","Cost") then I would like the new column to pick column V3, if not in the list then V2.

I was thinking about using the %in% function but not sure how I would go about doing an if in this list then get this column else get this column logic.

Help is most appreciated. Regards,

Aucun commentaire:

Enregistrer un commentaire