Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

r - 为什么在使用ifelse / mutate函数时会忽略NA?(Why are the NAs being ignored while I'm using the ifelse/mutate functions?)

So I have a data frame with several occurences of different species and a "new_name" empty column that I want to fill with mutate/ifelse.

(因此,我有一个数据帧,其中出现了多个不同种类的事件,并且我想用mutate / ifelse填充一个“ new_name”空列。)

Basically I want the new_name to be filled according to these conditions: if the status is unaccepter I want the new_name to be the value of "valid_name" and if the status is accepted or NA I want the new_name to take the value of "species".

(基本上,我希望根据以下条件填充new_name:如果状态为unaccepter,则希望new_name为“ valid_name”的值;如果状态为接受或不适用,则我希望new_name采用“ species”的值。)

This is an example of how my data frame is structured: ′′′

(这是数据框结构的一个示例:“′”)

example of the data frame (数据框示例)

         species           valid_name                 new_name    status
1.  Tilapia guineensis |         NA                 |  NA       | NA

2.     Tilapia zillii  |  Hippocampus trimaculatus  |  NA       | unaccepted

3. Fundulus rubrifrons |  Hippocampus trimaculatus  |  NA       | unaccepted

4.  Eutrigla gurnardus |  Bougainvillia supercili   |  NA       | accepted

5.   Sprattus sprattus |        NA                  |  NA       | NA

6.        Gadus morhua |  Aglantha digitale         |  NA       | accepted

′′′

(′′′)

So far I tried the following:

(到目前为止,我尝试了以下方法:)

df<-df%>%
  mutate(new_name = ifelse(status=="unaccepted",valid_name,ifelse(status=="accepted" | is.na(status),species,NA)))

So this code is working only for the values of "status" that don't have NAs.

(因此,此代码仅适用于没有NA的“状态”值。)

Otherwise it just ignores the NAs and does nothing.

(否则,它只会忽略NA,而不会执行任何操作。)

So the data frame becomes somthing like this:

(因此,数据帧如下所示:)

             species           valid_name                 new_name    status
    1.  Tilapia guineensis |         NA                 |  NA                         | NA

    2.     Tilapia zillii  |  Hippocampus trimaculatus  |  Hippocampus trimaculatus   | unaccepted

    3. Fundulus rubrifrons |  Hippocampus trimaculatus  |  Hippocampus trimaculatus   | unaccepted

    4.  Eutrigla gurnardus |  Bougainvillia supercili   |  Eutrigla gurnardus         | accepted

    5.   Sprattus sprattus |        NA                  |  NA                         | NA

    6.        Gadus morhua |  Aglantha digitale         |  Gadus morhua               | accepted

Thanks in advance for any answers

(预先感谢您的任何答案)

  ask by tadeufontes translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If we use == , make sure to also add is.na to return TRUE/FALSE, otherwise, the NAs remain as NA

(如果我们使用== ,请确保还添加is.na以返回TRUE / FALSE,否则,NA仍为NA)

library(dplyr)
df%>%
  mutate(new_name = ifelse(status=="unaccepted" & !is.na(status),valid_name,
           ifelse(status=="accepted" & !is.na(status),species,NA)))
#             species               valid_name     status                 new_name
#1  Tilapia guineensis                     <NA>       <NA>                     <NA>
#2      Tilapia zillii Hippocampus trimaculatus unaccepted Hippocampus trimaculatus
#3 Fundulus rubrifrons Hippocampus trimaculatus unaccepted Hippocampus trimaculatus
#4  Eutrigla gurnardus  Bougainvillia supercili   accepted       Eutrigla gurnardus
#5   Sprattus sprattus                     <NA>       <NA>                     <NA>
#6        Gadus morhua        Aglantha digitale   accepted             Gadus morhua

Another option is to use %in% which will return FALSE for NA

(另一种选择是使用%in% ,它将为NA返回FALSE)

df%>%
  mutate(new_name = ifelse(status %in% "unaccepted" ,valid_name,
           ifelse(status %in% "accepted",species,NA)))

using a reproducible example

(使用可复制的示例)

v1 <- c('a', 'b', NA)
v1 == 'a'
#[1]  TRUE FALSE    NA  ####

v1 %in% 'a'
#[1]  TRUE FALSE FALSE

data (数据)

df <- structure(list(species = c("Tilapia guineensis", "Tilapia zillii", 
"Fundulus rubrifrons", "Eutrigla gurnardus", "Sprattus sprattus", 
"Gadus morhua"), valid_name = c(NA, "Hippocampus trimaculatus", 
"Hippocampus trimaculatus", "Bougainvillia supercili", NA, 
"Aglantha digitale"
), status = c(NA, "unaccepted", "unaccepted", "accepted", NA, 
"accepted")), class = "data.frame", row.names = c(NA, -6L))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...