| by Arround The Web | No comments

R – with() and within() Functions

In this R tutorial, we will discuss with() and within() functions performed on the data frame.

Let’s create a data frame with four rows and five columns.

#create a dataframe-market that has 3 rows and 3 columns.
market=data.frame(apples=c(34,45,45),papayya=c(100,98,98),mangoes=c(100,67,89))
#display market
print(market)

with() Function

The with() function in R is used with the data frame used to perform some operations on the columns and return the result in a vector.

Syntax:
with(dataframe_object,operation)

It takes two parameters.

  1. dataframe_object is the data frame.
  2. operation performed on the columns in the dataframe_object.

Let’s see different examples to understand this function better.

Example 1

In this example, we will perform addition, subtraction, multiplication, and division on the apples and papayya columns.

#create a dataframe-market that has 3 rows and 3 columns.
market=data.frame(apples=c(34,45,45),papayya=c(100,98,98),mangoes=c(100,67,89))

print("sum of apples and papayya")
#add apples and papayya columns using with()
print(with(market,apples+papayya))

print("difference of apples and papayya")
#subract  apples from papayya columns using with()
print(with(market,apples-papayya))

print("multiplication of apples and papayya")
#multiply  apples with papayya columns using with()
print(with(market,apples*papayya))

print("division of apples and papayya")
#divide  apples by pappaya columns using with()
print(with(market,apples/papayya))

Result:

Output is returned as a vector across rows on apples and papayya columns.

Example 2

In this example, we will use different relational operators on apples and papayya columns.

#create a dataframe-market that has 3 rows and 3 columns.
market=data.frame(apples=c(34,45,45),papayya=c(100,98,98),mangoes=c(100,67,89))

print("Does apples greater than papayya? ")
#check each value in applied column is greater than papayya column values
print(with(market,apples>papayya))

print("Does apples less than papayya? ")
#check each value in applied column is less than papayya column values
print(with(market,apples=papayya))

print("Does apples less than or equal to papayya? ")
#check each value in applied column is greater than or equal to papayya column values
print(with(market,apples<=papayya))

Result:

We performed:

  1. Greater than operation: check if each value of the apple column is greater than each value of the papayya column.
  2. Less than operation: check if each value of the apple column is less than each value of the papayya column.
  3. Greater than or equal to operation: check if each value of the apple column is greater than or equal to each value of the papayya column.
  4. Less than or equal to operation: check if each value of the apple column is less than or equal to each value of the papayya column.

within() Function

The within() function in R is used with the data frame that performs some operations on the columns and returns the result in a new column within the data frame.

So the main difference between with() and within() functions is that with() will not store the result inside the data frame, but within() will store the result.

Syntax:
within(dataframe_object,new<-operation)

It takes two parameters.

  1. dataframe_object is the data frame.
  2. operation is performed on the columns in the dataframe_object and stored in a new column name.

Make sure you use the “<-” operator to load the result into a new column.

Let's see the different examples to understand this function better.

Example 1

In this example, we will perform addition, subtraction, multiplication, and division on the apples and papayya columns and store the result in the output column.

#create a dataframe-market that has 3 rows and 3 columns.
market=data.frame(apples=c(34,45,45),papayya=c(100,98,98),mangoes=c(100,67,89))
 
print("sum of apples and papayya")
#add apples and papayya columns using within()
print(within(market,output<-apples+papayya))
 
print("difference of apples and papayya")
#subract apples from papayya columns using within()
print(within(market,output<-apples-papayya))
 
print("multiplication of apples and papayya")
#multiply apples with papayya columns using within()
print(within(market,output<-apples*papayya))
 
print("division of apples and papayya")
#divide apples by pappaya columns using within()
print(within(market,output<-apples/papayya))

Result:

The result is stored in the output column.

Example 2

In this example, we will use different relational operators on the apples and papayya columns and store the result in the output column.

#create a dataframe-market that has 3 rows and 3 columns.
market=data.frame(apples=c(34,45,45),papayya=c(100,98,98),mangoes=c(100,67,89))
 
print("Does apples greater than papayya? ")
#check each value in applied column is greater than the papayya column values and store the result in the output column
print(within(market,outputpapayya))

print("Does apples less than papayya? ")
#check each value in applied column is less than papayya column values and store the result in output column
print(within(market,output<-apples<papayya))
 
print("Does apples greater than or equal to papayya? ")
#check each value in applied column is greater than or equal to papayya column values and store the result in the output column
print(within(market,output=papayya))
 
print("Does apples less than or equal to papayya? ")
#check each value in applied column is greater than or equal to papayya column values and store the result in output column
print(within(market,output<-apples<=papayya))

Result:

We performed:

  1. Greater than operation: check if each value of the apple column is greater than each value of the papayya column and store the result in output column.
  2. Less than operation: check if each value of the apple column is less than each value of the papayya column and store the result in the output column.
  3. Greater than or equal to operation: check each value of the apple column is greater than or equal to the each value of the papayya column and store the result in output column.
  4. Less than or equal to operation: check each value of the apple column is less than or equal to each value of the papayya column and store the result in output column.

Conclusion

In this R tutorial, we saw two functions with() and within(). The with() in R is used with the data frame used to perform some operations on the columns. It returns the result in a vector.within() in R, and it is used with the data frame that is used to perform some operations on the columns and returns the result in a new column within the data frame.

So the main difference between with() and within() functions is that with() will not store the result inside the data frame, but within() will store the result.

Share Button

Source: linuxhint.com

Leave a Reply