Built with Tutify.io

    AIStudyLabs

    R with() and within() Functions

    R with() and within() Functions

    What are with() and within() functions in R and how to use them? These two functions take the DataFrame and expression as input and evaluate the expression with the data from the DataFrame.

    The with() function returns the vector with the result of the expression and within() returns a new DataFrame either by adding or updating a column with the result of the expression.

    <a id="h-1-quick-examples"></a>

    1. Quick Examples

    Following are quick examples of with() and within() functions.

    
    # Quick examples
    
    # use of with()
    with(df, X * Y)
    
    # add as new column
    df$mult = with(df, X * Y)
    df
    
    # Using within()
    df2 <- within(df, sub <- Y-X)
    df2
    

    <a id="with-in-r"></a>

    2. with() in R

    The with() in R is a base function that works with the DataFrame. This function takes the DataFrame and the expression as input and returns the Vector. The expression uses the data from the DataFrame to evaluate.

    2.1 Syntax of with()

    The following is the syntax of the with() function.

    
    # Syntax
    with(dataframe, expression)
    

    2.2 R with() Example

    By using with() function you can execute the expression with the data from the DataFrame. In the below example I am multiplying columns X and Y. This function returns the result of the expression as an R Vector.

    The with() function does not update the original data frame.

    
    # Create DataFrame
    df <- data.frame(X = c(2, 4, 3),     
                     Y = c(3, 5, 2))
    df   
    
    # Use with()
    with(df, X * Y)
    

    Yields below output.

    2.3 Result of with() Add as a New Column

    Since the with() returns a vector, you can easily add it as a column to DataFrame.

    
    # Assign it as a new column
    df$mult = with(df, X * Y)
    df
    

    <a id="within-in-r"></a>

    3. within() in R

    The within() function in R is used to add the evaluated expression as a new column to the DataFrame and returns the new DataFrame with the new column. As you learned above, the with() function doesn't change the DataFrame in any way. The within() function also doesn't update the existing DataFrmae instead it returns a new DataFrame with a new column

    3.1 Syntax of within()

    The following is the syntax of the with() function.

    
    # Syntax
    within(dataframe, new-column <- expression)
    

    3.2 R within() Example

    The within() function returns a new DataFrame with the added column from the expression result. By using the existing column, you can also update the existing column with the expression result.

    The following example adds a new column sub to the DataFrame.

    
    df <- data.frame(X = c(2, 4, 3),     
                     Y = c(6, 8, 9))
    df  
    
    # Using within()
    df2 <- within(df, sub <- Y-X)
    df2
    

    Yields below output.

    4. Conclusion

    In this article, you have learned the syntax of with() and within() base functions in R and learned how to use them with examples. The with() function returns the vector with the result of the expression and within() returns a new DataFrame either by adding or updating a column with the result of the expression.

    Related Articles