应用列的每个单元格条件并替换 R 中单元格中的值

问题描述 投票:0回答:1

我有下面的数据框:

    > last_t_df3

      A1BG       A2M     NAT1      NAT2  SERPINA3
    1 1.943765 0.0000000 1.271231 0.0000000 0.0000000
    2 1.419931 0.3045321 2.507409 0.0000000 4.8022908
    3 0.000000 0.0000000 2.993064 0.5276899 2.0130009
    4 0.000000 1.2882883 3.048454 2.7974017 11.2820992
    5 0.000000 0.0000000 3.642756 0.0000000 9.5866993

以及下面每列的截止列表:

> cutfoo2
$A1BG
[1] 1.176439

$A2M
[1] 5.307673

$NAT1
[1] 4.867072

$NAT2
[1] 3.425465

$SERPINA3
[1] 9.234962

我想为每个相应的列应用截止值,如果单元格低于其截止值,我想将其替换为“否”。

我尝试了如下循环:

for (row in 1:nrow(last_t_df3)) {
for (col in 1:ncol(last_t_df3)) {
  g <- last_t_df3[row, col]  
  if (g < cutfoo2[[col]]) {
    last_t_df3[row, col] <- "NO"
  }
}
}

它适用于小尺寸的数据帧(last_t_df3),但实际上我的真实数据帧尺寸为 33 12623,而我的 for 循环不适用于该数据帧,例如,甚至 SERPINA3 列中的 11.2820992 也奇怪地被真实数据帧中的 NO 替换。

您对 for 循环有什么建议或替代方法吗?

提前致谢!

r dataframe conditional-statements apply
1个回答
0
投票

肯定有更优雅的方法,但这里有两种:

ExampleData <- data.frame(FirstCol = c(1, 3, 5, 8, 15),
                          SecondCol = c(20, 21, 30, 35, 56),
                          ThirdCol = c(0.1, 0.2, 0.1, 0.8, 0.1),
                          FourthCol = c(2, 7, 6, 5, 9))

Cutoffs_list <- list(FirstCol = 7,
                     SecondCol = 21,
                     ThirdCol = 0.5,
                     FourthCol = 8)

for (i in 1:length(ExampleData)) {
  ExampleData[, i] <- ifelse(ExampleData[, i] < Cutoffs_list[[i]], "NO", ExampleData[, i])
}

library(tidyverse)

ExampleData <- data.frame(FirstCol = c(1, 3, 5, 8, 15),
                          SecondCol = c(20, 21, 30, 35, 56),
                          ThirdCol = c(0.1, 0.2, 0.1, 0.8, 0.1),
                          FourthCol = c(2, 7, 6, 5, 9))

ExampleData <- ExampleData %>%
  mutate(FirstCol_bis = ifelse(FirstCol < Cutoffs_list[[1]], "NO", FirstCol),
         SecondCol_bis = ifelse(SecondCol < Cutoffs_list[[2]], "NO", SecondCol),
         ThirdCol_bis = ifelse(ThirdCol < Cutoffs_list[[3]], "NO", ThirdCol),
         FourthCol_bis = ifelse(FourthCol < Cutoffs_list[[4]], "NO", FourthCol))
© www.soinside.com 2019 - 2024. All rights reserved.