The percent sign in R

后端 未结 2 1324
猫巷女王i
猫巷女王i 2021-02-06 04:05

I recently read some source code for a R package called \'pathifier\'. In the source code, it uses the percent sign.

if (0 %in% xs) {
si <- NULL
cat(file = l         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-06 04:42

    The in reserved word can only be used in for loops. The %in% function is different. As noted in the documentation at ?"%in%", is defined as:

    "%in%" <- function(x, table) match(x, table, nomatch = 0) > 0
    

    So, it is essentially match. In English, x %in% y returns a vector of logical of the same length as x, with TRUE every time the corresponding element of x exists at least once in y.

    The reason why there are % around it is to mark it as a "infix" operator. (I don't know if that is the exact term.)

提交回复
热议问题