Built with Tutify.io

    AIStudyLabs

    FUN in R - Usage with Example

    FUN in R - Usage with Example

    FUN translates to function in R, in most cases, it is used when you want to apply a function over a List or Vector. You will find this in many R method syntaxes. For example, you can find this on apply(), lapply(), vapply(), sapply() e.t.c

    In this article, I will explain some examples of where FUN is used in R Programming and how to create a function and use it with vectors and lists.

    1. FUN in R

    Let's see some function syntaxes that have FUN in their method syntaxes. Let's quickly see what each function does.

    
    lapply(X, FUN, ...)
    
    sapply(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE)
    
    vapply(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE)
    

    Below are some details about the arguments I will be presenting.

    2. Apply FUN on List or Vector in R

    Let's use the FUN param of the R lapply() function to convert the elements of the vector to lowercase. Since lapply() returns the list, our result will be in a list with lowercase strings. You can use unlist() to convert list to vector in R.

    
    # Using lapply() with FUN
    vec <- c('JAVA','R','PYTHON','PHP')
    print(vec)
    
    # apply function to vector
    res <- lapply(vec, FUN=tolower)
    res
    

    Yields below output.

    3. Use FUN=fun() in R (Custom function)

    Now let's create a custom function in R and call it from the lapply() function by passing the function name to the FUN argument.

    Here, the getLang() function is called for each element of the vector, and the function getLang() splits the string by delimiter '_' and returns the first part of the string.

    
    # custom function
    # function to split and get the first part
    getLang <- function(str) { 
      return (unlist(strsplit(str,'_'))[1])
    }
    
    # create vector
    vec <- c('JAVA_LANG','R_LANG','PYTHON_LANG','PHP_LANG')
    vec
    
    # apply function
    res <- lapply(vec, FUN=getLang)
    res
    

    Yields below output.

    4. FUN with Arguments in R

    Finally, let's call the function with arguments. In the previous example, I hardcoded the '_' delimiter in the getLang() function to split the string. In the example below, I am taking the delimiter as an argument to the getLang() function. This way, I can change the delimiter at runtime by passing it as an argument when calling the function.

    
    # function to split and get the first part
    # Pass as an argument if you wanted to use different delimiter to split
    getLang <- function(str="", delimiter=',') { 
      return (unlist(strsplit(str,delimiter))[1])
    }
    
    # create vector
    vec <- c('JAVA_LANG','R_LANG','PYTHON_LANG','PHP_LANG')
    vec
    
    # apply function
    res <- lapply(vec, FUN=getLang,delimiter='_')
    res
    

    Yields the same output as above.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned what is FUN in R and how to use it. FUN translates to function in R, in most cases, it is used when you want to apply a function over a List or Vector. You will find this in many R method syntaxes. For example, you can find this on lapply(), vapply(), sapply() e.t.c.

    Related Articles