簡體   English   中英

R lapply ifelse 與數據框列表上的多個語句

[英]R lapply ifelse with multiple statements on list of dataframes

我有一個數據幀列表(fc.list)(“t1.fc.df”、“t2.fc.df”),如下所示:

fc.list$t1.fc.df
ID   log2.FC    qval
1    5.22161    0
2    4.34383    0
3    3.764772   0.86250849
4   -3.095648   0.9412494
5   -3.489743   0.904717
6   -3.648665   0.9412494

fc.list$t2.fc.df
ID   log2.FC    qval
1    6.287703   0.034547415 
2    5.751197   0.007923771
3    5.093789   0.352390406
4   -5.337459   0.007400576
5   -5.760159   0.000000000
6   -6.793630   0.000000000

我需要在我的數據幀中創建一個名為 $test 的變量,如果 log2.FC > 1 並且 qval < 0.05 然后寫“正”,否則,如果 log2.FC < -1 並且 qval < 0.05 寫“負” ”,否則寫“NS”。

我用 lapply ifelse 編寫了這些代碼行,

fc.list <- lapply(fc.list, function(x){
  x$test <- ifelse(unlist(x[1]) >= 1 &&
                       unlist(x[2]) <= 0.05, "positive",
                      ifelse(unlist(x[1]) <= -1 &&
                               unlist(x[2]) <= 0.05, "negative", "NS"))
  return(x)
})

但是,我只得到“NS” 誰能弄清楚問題出在哪里?

您的代碼存在一些問題,但很容易修復。

  1. 您正在使用unlist(x[column]) ,使用$訪問 data.frame 列要容易得多且易讀。

  2. 您使用unlist(x[5])並且沒有第 5 列。

  3. 您使用&&而不是&運算符。 &&只評估第一個元素,而&評估每個元素

如果您進行了這些更改,您的代碼就會像魅力一樣工作。


lapply(dflist, function(x){
  x$test <- ifelse(x$log2.FC >= 1 &
                     x$qval <= 0.05, "positive",
                     ifelse(x$log2.FC <= -1 &
                           x$qval <= 0.05, "negative", "NS"))
  return(x)
})


[[1]]
  ID   log2.FC        qval     test
1  1  6.287703 0.034547415 positive
2  2  5.751197 0.007923771 positive
3  3  5.093789 0.352390406       NS
4  4 -5.337459 0.007400576 negative
5  5 -5.760159 0.000000000 negative
6  6 -6.793630 0.000000000 negative

[[2]]
  ID   log2.FC      qval     test
1  1  5.221610 0.0000000 positive
2  2  4.343830 0.0000000 positive
3  3  3.764772 0.8625085       NS
4  4 -3.095648 0.9412494       NS
5  5 -3.489743 0.9047170       NS
6  6 -3.648665 0.9412494       NS

你可以試試這個

lapply(fc.list, function(x) 
        cbind(x, type = ifelse(x$qval<0.05 & x$log2.FC>1, "positive", 
                        ifelse(x$qval<0.05 & x$log2.FC< -1, "negative", NA))))
$t1.fc.df
  ID   log2.FC      qval     type
1  1  5.221610 0.0000000 positive
2  2  4.343830 0.0000000 positive
3  3  3.764772 0.8625085     <NA>
4  4 -3.095648 0.9412494     <NA>
5  5 -3.489743 0.9047170     <NA>
6  6 -3.648665 0.9412494     <NA>

$t2.fc.df
  ID   log2.FC        qval     type
1  1  6.287703 0.034547415 positive
2  2  5.751197 0.007923771 positive
3  3  5.093789 0.352390406     <NA>
4  4 -5.337459 0.007400576 negative
5  5 -5.760159 0.000000000 negative
6  6 -6.793630 0.000000000 negative

或者一個tidyverse

library(tidyverse)
fc.list %>% 
  map(~mutate(., type=case_when(qval<0.05 & log2.FC>1 ~ "positive",
                                qval<0.05 & log2.FC< -1 ~ "negative",
                                TRUE ~ NA_character_)))

通過添加%>% map(count, type)你得到解除管制的特征的數量:

$t1.fc.df
      type n
1 positive 2
2     <NA> 4

$t2.fc.df
      type n
1 negative 3
2 positive 2
3     <NA> 1

另一種可能的解決方案:

library(tidyverse)

df1 <- read.table(text = "ID   log2.FC    qval
1    5.22161    0
2    4.34383    0
3    3.764772   0.86250849
4   -3.095648   0.9412494
5   -3.489743   0.904717
6   -3.648665   0.9412494", header = T)

df2 <- read.table(text = "ID   log2.FC    qval
1    6.287703   0.034547415 
2    5.751197   0.007923771
3    5.093789   0.352390406
4   -5.337459   0.007400576
5   -5.760159   0.000000000
6   -6.793630   0.000000000", header = T)

mylist <- list(df1, df2)

map(mylist, ~ .x %>% mutate(test = ifelse(log2.FC > 1 & qval < 0.05, "positive",
     ifelse(log2.FC < -1 & qval < 0.05, "negative", "NS"))))

#> [[1]]
#>   ID   log2.FC      qval     test
#> 1  1  5.221610 0.0000000 positive
#> 2  2  4.343830 0.0000000 positive
#> 3  3  3.764772 0.8625085       NS
#> 4  4 -3.095648 0.9412494       NS
#> 5  5 -3.489743 0.9047170       NS
#> 6  6 -3.648665 0.9412494       NS
#> 
#> [[2]]
#>   ID   log2.FC        qval     test
#> 1  1  6.287703 0.034547415 positive
#> 2  2  5.751197 0.007923771 positive
#> 3  3  5.093789 0.352390406       NS
#> 4  4 -5.337459 0.007400576 negative
#> 5  5 -5.760159 0.000000000 negative
#> 6  6 -6.793630 0.000000000 negative

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM