官术网_书友最值得收藏!

Using the replacement function

On some occasions in R, we may discover that we can assign a value to a function call, which is what the replacement function does. Here, we will show you how the replacement function works, and how to create your own.

Getting ready

Ensure that you completed the previous recipes by installing R on your operating system.

How to do it...

Perform the following steps to create a replacement function in R:

  1. First, we assign names to data with the names function:
    > x <- c(1,2,3)
    >names(x) <- c('a','b','c')
    >x
    a b c
    1 2 3
    

    What the names function actually does is similar to the following commands:

    > x <- 'names<-'(x,value=c('a','b','c'))
    >x
    a b c
    1 2 3
    
  2. Here, we can also make our replacement function:
    > x<-c(1,2,3)
    > "erase<-" <- function(x, value){
    + x[!x %in% value]
    + }
    >erase(x) <- 2
    >x
    [1] 1 3
    
  3. We can invoke the erase function in the same way that we invoke the normal function:
    >x <- c(1,2,3)
    > x <- 'erase<-'(x,value=c(2))
    >x
    [1] 1 3
    

    We can also remove multiple values with the erase function:

    > x <- c(1,2,3)
    >erase(x) = c(1,3)
    >x
    [1] 2
    
  4. Finally, we can create a replacement function that can remove values of certain positions:
    > x <- c(1,2,3)
    > y <- c(2,2,3)
    > z <- c(3,3,1)
    > a = list(x,y,z)
    > "erase<-" <- function(x, pos, value){
    + x[[pos]] <- x[[pos]][!x[[pos]] %in% value]
    + x
    + }
    >erase(a, 2) = c(2)
    >a
    [[1]]
    [1] 1 2 3
    [[2]]
    [1] 3
    [[3]]
    [1] 3 3 1
    

How it works...

In this recipe, we first demonstrated how we could use the names function to assign argument names for each value. This type of function method may appear confusing, but it is actually what the replacement function does: assigning the value to the function call. We then illustrated how this function works in a standard function form, which we achieved by placing an assignment arrow (<-) after the function name and placing the x object and value between the parentheses.

Next, we learned how to create our replacement function. We made a function named erase, which removed certain values from a given object. We invoked the function by wrapping the vector to replace within the erase function and assigning the value to remove on the right-hand side of the assignment notation. Alternatively, we can still call the replacement function by placing an assignment arrow after erase as the function name. In addition to removing a single value from a given vector object, we can also remove multiple values by placing a vector on the right-hand side of the assignment function.

Furthermore, we can remove the values of certain positions with the replacement function. Here, we only needed to add a position argument between the object and value within the parentheses. As our last step shows, we removed 2 from the second value of the list with our newly-created replacement function.

There's more...

As mentioned earlier, names<- is a replacement function. To examine whether a function is a replacement function or not, use the get function:

>get("names<-")
function (x, value) .Primitive("names<-")
主站蜘蛛池模板: 龙州县| 斗六市| 富蕴县| 财经| 宁晋县| 元阳县| 太谷县| 石棉县| 明光市| 志丹县| 二手房| 宁陵县| 吐鲁番市| 无为县| 鹤庆县| 遂昌县| 宜川县| 新乐市| 宜宾县| 客服| 民丰县| 盐亭县| 贵阳市| 新津县| 常山县| 汾阳市| 镇巴县| 铜陵市| 油尖旺区| 阿克| 德庆县| 四子王旗| 扶绥县| 定安县| 临洮县| 齐河县| 湖州市| 福鼎市| 绵竹市| 揭西县| 勐海县|