Tutorials  /  Databases

R with() and within() function - A Comprehensive Guide

Ccentron Redaktion · February 2024 ·3 min read ·Databases, Tutorial

In this article, we will be having a look at two closely related yet different functions of R programming - R with() and within() function, in detail. So, let us begin!! :)

1. R with() Function

We often come across situations wherein we feel the need to build customized/user-defined functions to conduct out a certain operation. With R with() function, we can operate on R expressions as well as the process of calling that function in a single go!

That is with() function enables us to evaluate an R expression within the function to be passed as an argument. It works on data frames only. That is why the outcome of the evaluation of the R expression is done with respect to the data frame passed to it as an argument.

Syntax:

Code
with(data-frame, R expression)

Example:

Code
rm(list = ls())

Num <- c(100,100,100,100,100)
Cost <- c(1200,1300,1400,1500,1600)

data_A <- data.frame(Num,Cost,stringsAsFactors = FALSE)

with(data_A, Num*Cost)
with(data_A, Cost/Num)

In the above example, we have calculated the expression ‘Num*Cost’ for the data frame data_A directly in the with() function.

After which, we have calculated the expression ‘Cost/Num’ within the function as well.

The reason behind having these two statements one after another is to highlight that the with() function does not alter the original data frame at any cost. It gives the output separately for every value associated with the columns of the data frame.

Output:

Code
> with(data_A, Num*Cost)
[1] 120000 130000 140000 150000 160000
> with(data_A, Cost/Num)
[1] 12 13 14 15 16
DB

Matching infrastructure at centron

Databases need care in production: centron can take over updates, backups and monitoring – from a single instance to a full cluster. Explore managed clusters →

2. R within() Function

Having read about with() function, now let us focus on its twin! Haha! Just joking! Though the names of the functions sound similar, they differ in the functioning.

R within() function calculates the outcome of the expression within itself but with a slight difference. It allows us to create a copy of the data frame and add a column that would eventually store the result of the R expression.

Syntax:

Code
within(data frame, new-column <- R expression)

Example:

Code
rm(list = ls())

Num <- c(100,100,100,100,100)
Cost <- c(1200,1300,1400,1500,1600)

data_A <- data.frame(Num,Cost,stringsAsFactors = FALSE)

within(data_A, Product <- Num*Cost)
within(data_A, Q <- Cost/Num)

Here, we have performed the evaluation of the same expressions that we had used for the with() function. But, here we have created a new column to store the outcome of the expression.

Code
> within(data_A, Product <- Num*Cost) Num Cost Product 1 100 1200 120000 2 100 1300 130000 3 100 1400 140000 4 100 1500 150000 5 100 1600 160000 > within(data_A, Q <- Cost/Num)
  Num Cost  Q
1 100 1200 12
2 100 1300 13
3 100 1400 14
4 100 1500 15
5 100 1600 16

Conclusion

By this, we have come to the end of this topic. R with() and within() function – A Comprehensive Guide

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Databases
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren