如何根据 R 数据框中的列值更改行值?
在本文中,我们将看到如何在 R 编程语言中根据 Dataframe 中的列值更改行中的值。
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.
以下代码片段是根据 R 中的列值更改行值的示例。它检查 C3 列中的单元格值是否小于 11,它替换相应的行值,保持列与 NA 相同。这种方法需要的二次时间等于数据框的维度。
例子:
R
# declaring a data frame in 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)
# replace the row value with NA if the col
# value in C3 is less than 11 looping over
# the data frame values
for (i in 1:nrow(data_frame)){
for(j in 1:ncol(data_frame)) {
# checking if the column is C3 that is
# j index is 3
if(j==3){
# checking if the row value of c3 is
# less than 11
if(data_frame[i,j]<11){
# changing the row value in the
# data frame
data_frame[i,j] <- NA
}
}
}
}
# printing modified data frame
print ("Modified data frame")
print (data_frame)
R
# declaring a data frame in 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)
# replace the row value with 0 if the
# data element at col index 2 is divisible
# by 2 looping over the rows of data frame
for (i in 1:nrow(data_frame)){
# iterate over the 2nd column only of the
# data frame and check if divisible by 2
if(data_frame[i,2]%%2){
# replace the value with 0
data_frame[i,2]<-0
}
}
# printing modified data frame
print ("Modified data frame")
print (data_frame)
R
# declaring a data frame in 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)
# check if c1 value is greater than
# equal to 1, replaced by 3
data_frame[data_frame$C1>=1 ,] <- 3
print("Modified data frame")
print(data_frame)
输出:
[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
这种方法可以优化,以防我们知道要进行评估的列的索引值。在这种情况下,我们不会迭代整个数据框,而只会迭代列值。
例子:
电阻
# declaring a data frame in 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)
# replace the row value with 0 if the
# data element at col index 2 is divisible
# by 2 looping over the rows of data frame
for (i in 1:nrow(data_frame)){
# iterate over the 2nd column only of the
# data frame and check if divisible by 2
if(data_frame[i,2]%%2){
# replace the value with 0
data_frame[i,2]<-0
}
}
# printing modified data frame
print ("Modified data frame")
print (data_frame)
输出:
[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 还提供了一种处理这些行转换的内置方法,只需将要评估的条件指定为数据框的行索引即可。重新分配的值在数据框中被替换。在这种情况下不需要对数据帧进行显式迭代。
例子:
电阻
# declaring a data frame in 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)
# check if c1 value is greater than
# equal to 1, replaced by 3
data_frame[data_frame$C1>=1 ,] <- 3
print("Modified data frame")
print(data_frame)
输出:
[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