How to add a column based on other columns in R DataFrame ?
Last Updated :
30 Apr, 2021
A data frame can be accessed and modified to store new insertions and deletions. The data frame can undergo mutations to increase its dimensions and store more data, as well as the rows and columns values, which can be modified based on other cell values.
In this article, we will see how to add columns based on other columns in DataFrame in R Programming Language. There can be various methods to do the same. Let’s discuss them in detail.
Method 1 : Using transform() function
The transform() method in R is used to modify the data and perform mutations. It transforms the first argument that is supplied to the function. New columns can also be added as a second argument of the function, where it may be either a list declared at the beginning or initialized during run times using the desired regular expression evaluation. We can specify the newly added column name on the left side of the second argument, and declare the if-else expression on the right. The if-else expression consists of three parts,
- The condition to test the data upon
- The second part is evaluated when the condition is not satisfied
- Third when it isn’t.
The result has to be explicitly into the original data frame, in order to pertain to the results.
Syntax:
transform(dataframe,x=c(..))
where x is the newly added column.
Example:
R
data_frame = data.frame (col1= c (1,2,3,-4),
col2= c (8,9,5,10),
col3= c (0,2,3,5))
print ( "Original Data Frame" )
print (data_frame)
data_frame <- transform (
data_frame, col4= ifelse (col1==col3, col1+col2, col1+col3))
print ( "Modified Data Frame" )
print (data_frame)
|
Output
[1] "Original Data Frame"
col1 col2 col3
1 1 8 0
2 2 9 2
3 3 5 3
4 -4 10 5
[1] "Modified Data Frame"
col1 col2 col3 col4
1 1 8 0 1
2 2 9 2 11
3 3 5 3 8
4 -4 10 5 1
Method 2 : Using with() method
The with() method in R can be used to evaluate expressions and then transform the data contained in a data frame. With is a generic function that evaluates expression specified as the second argument of the function in a local environment constructed from data, which is defined in the first argument of the function. Any logical expression can be provided as the first argument of the method and the value in the new column is replaced depending on the truth value of the expression after evaluating the condition in the argument parts of the with method.
Syntax:
with(data, expr, …)
Example:
R
data_frame = data.frame (col1= c (1,2,3,-4),
col2= c (8,9,5,10),
col3= c (0,2,3,5))
print ( "Original Data Frame" )
print (data_frame)
data_frame$col4 <- with (
data_frame, ifelse (col1+col3>5, col1+col3, col1+col2))
print ( "Modified Data Frame" )
print (data_frame)
|
Output
[1] "Original Data Frame"
col1 col2 col3
1 1 8 0
2 2 9 2
3 3 5 3
4 -4 10 5
[1] "Modified Data Frame"
col1 col2 col3 col4
1 1 8 0 9
2 2 9 2 11
3 3 5 3 6
4 -4 10 5 6
Method 3 : Using apply() method
apply() method in R takes a well-organized data frame or matrix as an input and gives as output a vector, list, or an array. apply() method is primarily used to avoid explicit uses of loop constructs. Any function can be specified into the apply() method. The result has to be explicitly into the original data frame, in order to pertain the results.
Syntax: apply(X, margin, FUN)
Parameter :
- x: a data frame or a matrix
- margin: take a value or range between 1 and 2 to define where to apply the function:
- margin=1 : the manipulation is performed on rows
- margin=2 : the manipulation is performed on columns
- margin=c(1,2) : the manipulation is performed on both rows and columns
- FUN: the function to apply where in built functions like mean, median, sum, min, max and even user-defined functions can be applied
Example:
R
data_frame = data.frame (col1= c (1,2,3,-4),
col2= c (8,9,5,10),
col3= c (0,2,3,5))
print ( "Original Data Frame" )
print (data_frame)
data_frame$col4 <- apply (
data_frame, 1, FUN = function (x) if ( mean (x[1])>1) x[2]+x[1] else x[3]-x[2])
print ( "Modified Data Frame" )
print (data_frame)
|
Output
[1] "Original Data Frame"
col1 col2 col3
1 1 8 0
2 2 9 2
3 3 5 3
4 -4 10 5
[1] "Modified Data Frame"
col1 col2 col3 col4
1 1 8 0 -8
2 2 9 2 11
3 3 5 3 8
4 -4 10 5 -5
Similar Reads
How to change row values based on a column value in R dataframe ?
In this article, we will see how to change the values in rows based on the column values in Dataframe in R Programming Language. Syntax: df[expression ,] <- newrowvalue Arguments : df - Data frame to simulate the modification uponexpression - Expression to evaluate the cell data based on a column
4 min read
How to Split Column Into Multiple Columns in R DataFrame?
In this article, we will discuss how to split a column from a data frame into multiple columns in the R programming Language. Method 1: Using str_split_fixed() function of stringr package library To split a column into multiple columns in the R Language, We use the str_split_fixed() function of the
3 min read
How to add column to dataframe in R ?
In this article, we are going to see how to add columns to dataframe in R. First, let's create a sample dataframe. Adding Column to the DataFrame We can add a column to a data frame using $ symbol. syntax: dataframe_name $ column_name = c( value 1,value 2 . . . , value n) Here c() function is a vect
2 min read
How to sort R DataFrame by the contents of a column ?
In this article, we will discuss how to sort DataFrame by the contents of the column in R Programming language. We can use the order() function for the same. order() function with the provided parameters returns a permutation that rearranges its first argument into ascending or descending order, bre
1 min read
How to add a prefix to column names in R DataFrame ?
In this article, we will discuss how to add prefixes to column names in DataFrame in R Programming Language. Dataset in use: First SecondThird1a72ab83cv94dsd10Method 1 : Using paste() method In order to modify the column names, the paste function in R can be used. The paste() method, can be used for
4 min read
How to Add an Empty Column to DataFrame in R?
In this article, we will discuss how to add an empty column to the dataframe in R Programming Language. Add One Empty Column to dataframe Here we are going to add an empty column to the dataframe by assigning column values as NA. Syntax: dataframe[ , 'column_name'] = NA where, dataframe is the input
1 min read
How to assign column names based on existing row in R DataFrame ?
In this article, we will discuss how assign column names or headers to a DataFrame based on rows in R Programming Language. Method 1 : Using as.character() method unlist() method in R is used to simulate the conversion of a list to vector. It simplifies to produce a vector by preserving all componen
3 min read
How to add suffix to column names in R DataFrame ?
Each of the columns in a data frame is defined by a name, known as the column name. It may be of the type of numerical or string value. In this article, we will discuss how to add a suffix to column names in DataFrame in R Programming Language. Method 1 : Using paste() method In order to modify the
4 min read
How to Compare Two Columns in R DataFrame?
In this article, we will discuss how to compare two columns in the dataframe through R Programming Language. We can compare two columns in R by using ifelse(). This statement is used to check the condition given and return the data accordingly. Syntax: ifelse(df$column1 > df$column2, statement,..
2 min read
How to Select DataFrame Columns by Index in R?
In this article, we will discuss how to select columns by index from a dataframe in R programming language. Note: The indexing of the columns in the R programming language always starts from 1. Method 1: Select Specific Columns By Index with Base R Here, we are going to select columns by using index
2 min read