R 用相邻列中的值替换列中的NA

R 用相邻列中的值替换列中的NA,r,conditional,na,R,Conditional,Na,我有这样一个数据帧: A B C D E F G H a LOW 1.5 0.2 NA 1000 2000 NA b LOW 2.9 0.4 HIGH 6000 1000 NA c LOW 1 1.3 LOW 400 1

我有这样一个数据帧:

  A       B        C       D       E       F        G        H
  a     LOW      1.5     0.2      NA    1000     2000       NA
  b     LOW      2.9     0.4    HIGH    6000     1000       NA
  c     LOW        1     1.3     LOW     400     1111      LOW 
  d     LOW        2      10     LOW    1000      400     HIGH
如何使用条件语句替换
NA

对于E列,我想取C列和D列的差值,如果小于0,则显示“小幅减少”,如果大于0,则显示“小幅增加”

然后对于H列,除了使用F列和G列的差值外,执行相同的操作。如果低于0,则显示“小幅度减少”,如果高于0,则显示“小幅度增加”

最终输出应如下所示:

  A       B        C       D                 E       F        G                    H
  a     LOW      1.5     0.2    Small Increase    1000     2000       Small Decrease
  b     LOW      2.9     0.4              HIGH    6000     1000       Small Increase
  c     LOW        1     1.3               LOW     400     1111                  LOW 
  d     LOW        2      10               LOW    1000      400                 HIGH

对其他列也执行类似的步骤

df$E <- ifelse(is.na(df$E), ifelse(df$C-df$D <0,"small decrease","small increase"), df$E)

df$E对其他列也执行类似的步骤

df$E <- ifelse(is.na(df$E), ifelse(df$C-df$D <0,"small decrease","small increase"), df$E)

df$E这里有一个选项,使用
data.table
中的
set
,这将是非常有效的,因为它将值分配到位

library(data.table)
setDT(df1)#converts 'data.frame' to 'data.table'
#loop through the index of the concerned columns
for(j in c(5L, 8L)) {
  #get the row index of NA for each column
  i1 <- which(is.na(df1[[j]])) 
  #get the value to be replaced based on the difference
  val <- c("Small Increase", "Small Decrease")[((df1[[j-2]][i1] - df1[[j-1]][i1]) < 0) + 1]
  #set the NA elements to the above val
  set(df1, i = i1, j = j, value = val)
 }

df1
#   A   B   C    D              E    F    G              H
#1: a LOW 1.5  0.2 Small Increase 1000 2000 Small Decrease
#2: b LOW 2.9  0.4           HIGH 6000 1000 Small Increase
#3: c LOW 1.0  1.3            LOW  400 1111            LOW
#4: d LOW 2.0 10.0            LOW 1000  400           HIGH
库(data.table)
setDT(df1)#将“data.frame”转换为“data.table”
#循环浏览相关列的索引
对于(c中的j(5L,8L)){
#获取每列NA的行索引

i1这里有一个选项,使用
数据表中的
设置
,这将是非常有效的,因为它在适当的位置分配值

library(data.table)
setDT(df1)#converts 'data.frame' to 'data.table'
#loop through the index of the concerned columns
for(j in c(5L, 8L)) {
  #get the row index of NA for each column
  i1 <- which(is.na(df1[[j]])) 
  #get the value to be replaced based on the difference
  val <- c("Small Increase", "Small Decrease")[((df1[[j-2]][i1] - df1[[j-1]][i1]) < 0) + 1]
  #set the NA elements to the above val
  set(df1, i = i1, j = j, value = val)
 }

df1
#   A   B   C    D              E    F    G              H
#1: a LOW 1.5  0.2 Small Increase 1000 2000 Small Decrease
#2: b LOW 2.9  0.4           HIGH 6000 1000 Small Increase
#3: c LOW 1.0  1.3            LOW  400 1111            LOW
#4: d LOW 2.0 10.0            LOW 1000  400           HIGH
库(data.table)
setDT(df1)#将“data.frame”转换为“data.table”
#循环浏览相关列的索引
对于(c中的j(5L,8L)){
#获取每列NA的行索引
i1