Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

R Combine various logical operators within Boolean or if statements

I have a dataframe listing 400 Products. Each of these Products belong to a certain category: BODY CLEANSING, HAIR, DEO, HAND BODY, FACE, MEN, LIP, SUN and FEMALE SHAVE. Furthermore I have the Sales Month, in which each of the 400 Products were launched.

I want to create a new column in my dataframe displaying TRUE if the product was launched in high season and FALSE if not.

The logic is something like this: If a product in the cateogry BODY CLEANSING or HAIR or DEO or HAND BODY or FACE or MEN or LIP was launched in the months of October or November or December high season = TRUE and if a product in the category SUN or FEMALE SHAVE is launched in the months of either April, May, June or July high season = TRUE.

I have tried the following:

Sample_15$high_season <- (Sample_15$Category == "BODY CLEANSING" | "HAIR" 
| "DEO" | "HAND BODY"| "FACE" | "MEN" | "LIP" & Sample_15$Months_extract 
== "Oktober" | "November" | "Dezember") | (Sample_15$Category == "SUN" | 
"FEMALE SHAVE" & Sample_15$Months_extract == "April" |                                              
"Mai" | "Juni" | "Juli")

And I get this Error message:

Error in Sample_15$Category == "BODY CLEANSING" | "HAIR" : 
  operations are possible only for numeric, logical or complex types

Does anybody know how to code the logic of the Boolean? Any help is appreciated!!!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sample_15$Category == "BODY CLEANSING" | Sample_15$Category == "HAIR" ... and so on

or

Sample_15$Category %in% c("BODY CLEANSING","HAIR",...)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...