📜  如何根据 R 数据框中的列值更改行值?

📅  最后修改于: 2022-05-13 01:55:34.176000             🧑  作者: Mango

如何根据 R 数据框中的列值更改行值?

在本文中,我们将看到如何在 R 编程语言中根据 Dataframe 中的列值更改行中的值。

以下代码片段是根据 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)


输出:

这种方法可以优化,以防我们知道要进行评估的列的索引值。在这种情况下,我们不会迭代整个数据框,而只会迭代列值。

例子:

电阻

# 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)

输出: