簡體   English   中英

如何根據 R 中其他列的特定條件創建新列?

[英]How do I create a new column based off of specific conditions of other columns in R?

我有以下關於捕食者、獵物相互作用、SS = surf.smelt、SL = sandlance 和 H = 鯡魚的數據框。

基本上,我需要的只是另一列,說明在相互作用期間是否有多個物種可供捕食者使用。 例如,如果您查看索引 12,獵物是 SL,但有 1000 條沙矛和 100 條鯡魚可用,我需要一個列,如果有超過 2 個物種可供捕食者使用,我需要一個可以簡單地顯示為 1 或 0 的列。

如果可能的話,我還想以長格式顯示其他物種

我擁有的日期框架:

index   date     site  Pred  Prey  attack  passive surf.smelt  sandlance  herring
 17   2015-06-06  cb    JCK   SS     0       1         20         0          0
 26   2015-07-05  cb    JCK   SS     0       1         100        0          0
 12   2016-07-26  cb    JCK   SL     1       0         0         1000       100
 88   2016-07-26  cb    JCK   H      1       0         0         1000       1000
 89   2016-07-26  cb    JCK   H      1       0         0          0         100
 90   2018-08-21  cb    JCO   SL     1       0        100        500         0
100   2018-08-26  cb    JCO   SL     1       0         0         1000       100
108   2019-06-22  cb    JCO   SS     0       1        50          0         100

我想要的數據框:

index   date     site  Pred  Prey  attack  passive surf.smelt  sandlance  herring OtherPrey?
 17   2015-06-06  cb    JCK   SS     0       1         20         0          0       0
 26   2015-07-05  cb    JCK   SS     0       1         100        0          0       0
 12   2016-07-26  cb    JCK   SL     1       0         0         1000       100      1  
 88   2016-07-26  cb    JCK   H      1       0         0         1000       1000     1
 89   2016-07-26  cb    JCK   H      1       0         0          0         100      0
 90   2018-08-21  cb    JCO   SL     1       0        100        500         0       1
100   2018-08-26  cb    JCO   SL     1       0         0         1000       100      1
108   2019-06-22  cb    JCO   SS     0       1        50          0         100      1

如果可能的話,我想定義其他可用的物種:

Data frame I want:

index   date     site  Pred  Prey  attack  passive surf.smelt  sandlance  herring OtherAvailable
 17   2015-06-06  cb    JCK   SS     0       1         20         0          0       0
 26   2015-07-05  cb    JCK   SS     0       1         100        0          0       0
 12   2016-07-26  cb    JCK   SL     1       0         0         1000       100      H  
 88   2016-07-26  cb    JCK   H      1       0         0         1000       1000     SL
 89   2016-07-26  cb    JCK   H      1       0         0          0         100      0
 90   2018-08-21  cb    JCO   SL     1       0        100        500         0       SS
100   2018-08-26  cb    JCO   SL     1       0         0         1000       100      H
108   2019-06-22  cb    JCO   SS     0       1        50          0         100      H

我們可以使用dplyr::if_any

library(dplyr)

df %>% mutate(other_prey = +if_any(surf.smelt:herring))

對於“OtherAvailable”,我們可以使用toString

library(dplyr)

df %>% rowwise %>%
       mutate(OtherAvailable = toString(names(across(surf.smelt:herring))[as.logical(surf.smelt:herring)]))

使用dplyr

df %>% 
   rowwise() %>% 
   mutate(Other_Prey=ifelse(sum(c_across(surf.smelt:herring)>0)>=2,1,0))
# A tibble: 8 x 11
# Rowwise: 
  index date       site  Pred  Prey  attack passive surf.smelt sandlance herring Other_Prey
  <int> <chr>      <chr> <chr> <chr>  <int>   <int>      <int>     <int>   <int>      <dbl>
1    17 2015-06-06 cb    JCK   SS         0       1         20         0       0          0
2    26 2015-07-05 cb    JCK   SS         0       1        100         0       0          0
3    12 2016-07-26 cb    JCK   SL         1       0          0      1000     100          1
4    88 2016-07-26 cb    JCK   H          1       0          0      1000    1000          1
5    89 2016-07-26 cb    JCK   H          1       0          0         0     100          0
6    90 2018-08-21 cb    JCO   SL         1       0        100       500       0          1
7   100 2018-08-26 cb    JCO   SL         1       0          0      1000     100          1
8   108 2019-06-22 cb    JCO   SS         0       1         50         0     100          1

它不是很優雅,但這里有一個鏡頭。

library(dplyr)
library(stringr)


df <- data.frame(Prey = c("SS", "SS", "SL", "H", "H", "SL", "SL", "SS"),
             SS = c(20, 100, 0, 0, 0, 100, 0 ,50),
             SL = c(0,0,1000, 1000, 0, 500, 1000, 0),
             H = c(0,0,100,1000,100,0,100,100) )

tb <- df %>%
    mutate(SS = SS > 0, SL = SL > 0, H  = H > 0,
    OtherPrey =  (rowSums(across(where(is.logical))))-1)
    
tb1 <- which(as.matrix(tb[,2:4]), arr.ind = TRUE)
tb$Available <- tapply(names(tb[,2:4])[tb1[,2]], tb1[,1], paste, collapse=",")        


tb <- tb %>%
    mutate(other = str_replace(Available,Prey,""),
           AvailableOther = str_replace(other,",","")) %>%
    select(Prey, OtherPrey, AvailableOther) 

df_new <- cbind(df, tb)

df_new

在此處輸入圖像描述

暫無
暫無

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

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