根据特定条件在R中跨列交换值

qoefvg9y  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(68)

我有一个dataframe,看起来像这样:

example <- data.frame(
  date = as.Date(c('2023-03-05', 
                   '2023-02-24', '2023-02-25',
                   '2023-03-01', '2023-03-02', 
                   '2023-03-03', 
                   '2023-03-04')),
  CID = c(137,
          157, 157,
          222, 222,
          133,
          260),
  PID_A = c(2137,
            2157, 2157,
            2222, 2222,
            1133,
            1260),
  PID_B = c(NA, NA, NA, NA, NA, NA, 2260),
  give_A = c('a', NA, 'b',
             'c', 'd', 'e', 'g'),
  receive_B = c(NA, NA, NA, NA, NA, 'f', 'h'),
  rating_1 = c(1,2,3,4,5,6,7),
  rating_2 = c(2,5,6,2,3,1,1)
)

字符串
我想修改数据框,使得当且仅当PID_A以'2'开头且PID_B为NA时,将PID_A中的值移位到PID_B,并将give_A中的文本与receive_B交换。
以下是示例输出:

example_solution <- data.frame(
  date = as.Date(c('2023-03-05', 
                   '2023-02-24', '2023-02-25',
                   '2023-03-01', '2023-03-02', 
                   '2023-03-03', 
                   '2023-03-04')),
  CID = c(137,
          157, 157,
          222, 222,
          133,
          260),
  PID_A = c(NA, NA, NA, NA, NA,
            1133,
            1260),
  PID_B = c(2137,
            2157, 2157,
            2222, 2222, NA, 2260),
  give_A = c(NA, NA, NA, NA, NA, 'e', 'g'),
  receive_B = c('a', NA, 'b',
                'c', 'd', 'f', 'h'),
  rating_1 = c(1,2,3,4,5,6,7),
  rating_2 = c(2,5,6,2,3,1,1)
)


有人知道一个有效的方法来解决这个问题吗?
谢谢你,谢谢

up9lanfz

up9lanfz1#

我能找到的最简单的方法是使用for循环:

processor <- function(df) {
for (row in seq_along(df[-1])) { # oddly, seq_along(df) returns a vector one longer than the number of rows in df???
  if (substr(df[row, 'PID_A'], 1, 1) == '2' &
      is.na(df[row, 'PID_B'])) {
    new_b <- df[row, 'PID_A']
    df[row, 'PID_A'] <- df[row, 'PID_B']
    df[row, 'PID_B'] <- new_b
    new_give <- df[row, 'give_A']
    df[row, 'give_A'] <- df[row, 'receive_B']
    df[row, 'receive_B'] <- new_give
  }
}
return(df)
}
processor(example)

字符串

pbgvytdp

pbgvytdp2#

下面是我的基本R解决方案within

example_solution <- within(example, {
  ind <- startsWith(as.character(PID_A),"2")&is.na(PID_B)
  PID_B[ind] <- PID_A[ind]
  PID_A[ind] <- NA
  g <- give_A[ind]
  give_A[ind] <- receive_B[ind]
  receive_B[ind] <- g
  rm(g, ind)
})

字符串

相关问题