How to change row values based on a column value in R dataframe ?
Last Updated :
21 Apr, 2021
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 upon
- expression – Expression to evaluate the cell data based on a column value
- newrowvalue – The modified value to replace the old value with
Returns : Doesn’t return anything, but makes changes to the data frame.
The following code snippet is an example of changing the row value based on a column value in R. It checks if in C3 column, the cell value is less than 11, it replaces the corresponding row value, keeping the column the same with NA. This approach takes quadratic time equivalent to the dimensions of the data frame.
Example:
R
data_frame = data.frame (C1= c (5:8),C2 = c (1:4),
C3 = c (9:12),C4 = c (13:16))
print ( "Original data frame" )
print (data_frame)
for (i in 1: nrow (data_frame)){
for (j in 1: ncol (data_frame)) {
if (j==3){
if (data_frame[i,j]<11){
data_frame[i,j] <- NA
}
}
}
}
print ( "Modified data frame" )
print (data_frame)
|
Output:
[1] “Original data frame”
C1 C2 C3 C4
1 5 1 9 13
2 6 2 10 14
3 7 3 11 15
4 8 4 12 16
[1] “Modified data frame”
C1 C2 C3 C4
1 5 1 NA 13
2 6 2 NA 14
3 7 3 11 15
4 8 4 12 16
This approach can be optimized, in case we know the index value of the column to carry out the evaluation. In that case, we will not iterate over the entire data frame but only over the column values.
Example:
R
data_frame = data.frame (C1= c (5:8),C2 = c (1:4),
C3 = c (9:12),C4 = c (13:16))
print ( "Original data frame" )
print (data_frame)
for (i in 1: nrow (data_frame)){
if (data_frame[i,2]%%2){
data_frame[i,2]<-0
}
}
print ( "Modified data frame" )
print (data_frame)
|
Output:
[1] “Original data frame”
C1 C2 C3 C4
1 5 1 9 13
2 6 2 10 14
3 7 3 11 15
4 8 4 12 16
[1] “Modified data frame”
C1 C2 C3 C4
1 5 0 9 13
2 6 2 10 14
3 7 0 11 15
4 8 4 12 16
R also provides an inbuilt way of handling these row transformations, by simply specifying the condition to be evaluated as the row index of the data frame. The reassigned values are replaced within the data frame. Explicit iteration over the data frame is not required in this case.
Example:
R
data_frame = data.frame (C1= c (1,2,2,1),C2 = c (1:4),
C3 = c (9:12),C4 = c (13:16))
print ( "Original data frame" )
print (data_frame)
data_frame[data_frame$C1>=1 ,] <- 3
print ( "Modified data frame" )
print (data_frame)
|
Output:
[1] “Original data frame”
C1 C2 C3 C4
1 1 1 9 13
2 2 2 10 14
3 2 3 11 15
4 1 4 12 16
[1] “Modified data frame”
C1 C2 C3 C4
1 3 3 3 3
2 3 3 3 3
3 3 3 3 3
4 3 3 3 3
Share your thoughts in the comments
Please Login to comment...