I have a dataset of beer sales describing different promotions. In the column sale, they describe if the sales was either a bonus buy "B", a coupon "C" or a sale "S". If there was no sale, the sale is empty. I want to crate a sales dummy. If there is a sale, either B, C or S, I want the number "1" in the column. I need that for further work. Sale is a factor vector.

I've tried this formula below. However, R tells me, that this is only working for numeric, logical or complex types. As "sale" was a factor vector, I tried converting "sale" to a numeric vector. That didn't work either.

##sales dummy: if beer$sale = "B", "C" or "S", write "1"
beer$sales <- as.character(beer$sale)
beer$sale <-as.numeric(beer$sale)

beer$sale <- ifelse(beer$sale== "B"|"C"|"S", 1, 0)

If there is a sale, either B, C or S, I want the number "1" in the column. I expected the letter B,C or S to change to 1, but that didn't happen. Thank you so much in advance for helping us!

0 Answers