what does that mean %in% in this code?

v<-data$Age%in%seq(5,85,5) & data$Sex==1
prediction<-predict(model,newdata = data.frame(Sex=1,Age=seq(5,85,5)),type="response")

Hello :slight_smile:
it means evaluate if the contents of data$Age are in the sequence of numbers from 5 to 85 in steps of 5 i.e. 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85


Any operator surrounded by % is called an "infix function". You can force the R parser to evaluate an infix function "normally", i.e. to look up the documentation for it, by putting the function call in backticks. It's hard for me to type a backtick because this comments forum turns it into a code block but on a standard keyboard the backtick is the key above the left tab. e.g. typing ?<backtick>%in%<backtick> in the console will give you the documentation for the %in% function.

Infix functions operate on the expressions to the left and right of themselves, rather than on arguments provided inside (). Normally expressed infix operators in base R include %o% (outer product) and %% (modulo, i.e. remainder after division) + - > < are other examples of infix functions, but these examples are not expressed as e.g. %+% because the parser has special exceptions for handling them.

Infix functions are otherwise totally normal functions that take two arguments and you can call them like normal functions. a %in% b is the same as calling <backtick>%in%<backtick>(a,b).

In your example: data$Age %in% seq(5,85,5) & data$Sex==1 will return a vector with the same length as data$Age. Any given element of the vector will be the logical value TRUE if the element at the corresponding position in data$Age is a number divisible by 5 between 5 and 85 (this is what %in% seq(5,85,5) does) AND the value of the element at that position in data$Sex is equal to 1 (this is what data$Sex==1 does.) The & operator serves to evaluate that both cases are TRUE. Otherwise the element will be FALSE.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.