用值符合条件的数据框列表替换第 n 列的列值

replace column value of nth column with a list of dataframes where value matches criteria

如果我有以下数据帧列表:

d1 <- data.frame(y1=c(1,2,3), y2=c(4,5,6))
d2 <- data.frame(y1=c(3,2,1), y2=c(6,5,4))
d3 <- data.frame(y1=c(6,5,4), y2=c(3,2,1))
d4 <- data.frame(y1=c(9,9,9), y2=c(8,8,8))

my.list <- list(d1, d2, d3, d4)

my.list
[[1]]
  y1 y2
1  1  4
2  2  5
3  3  6

[[2]]
  y1 y2
1  3  6
2  2  5
3  1  4

[[3]]
  y1 y2
1  6  3
2  5  2
3  4  1

[[4]]
  y1 y2
1  9  8
2  9  8
3  9  8

如何用 "greater than 5" 替换第二列中数字大于 5 的值,即

my.list
[[1]]
  y1 y2
1  1  4
2  2  5
3  3  'greater than five'

[[2]]
  y1 y2
1  3  'greater than five'
2  2  5
3  1  4

[[3]]
  y1 y2
1  6  3
2  5  2
3  4  1

[[4]]
  y1 y2
1  9  'greater than five'
2  9  'greater than five'
3  9  'greater than five'

我知道我可以通过执行以下操作来测试此类情况:

sapply(sapply(my.list, "[[", 2), function(x) x > 5)
 [1] FALSE FALSE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE

但不知道如何在测试为真时替换原始值。

如有帮助将不胜感激

你可以选择 replace in base R:

col <- 2
lapply(my.list, function(x) 
      data.frame(cbind(x[,-col], replace(x[,col], x[,col]>5, "greater than five"))))

我们可以使用 transformlapply

lapply(my.list, transform, y2 = replace(y2, y2>5, "greater than 5"))
#[1]]
#  y1             y2
#1  1              4
#2  2              5
#3  3 greater than 5

#[[2]]
#  y1             y2
#1  3 greater than 5
#2  2              5
#3  1              4

#[[3]]
#  y1 y2
#1  6  3
#2  5  2
#3  4  1

#[[4]]
#  y1             y2
#1  9 greater than 5
#2  9 greater than 5
#3  9 greater than 5

tidyverse

library(tidyverse)
my.list %>%
    map(~mutate(., y2 = replace(y2, y2 >5, "greater than 5")))