将所有NA放入新列[Mutate]

问题描述

我需要创建一个新列,该列是其他两三列的函数,其中一列包含一些丢失的数据(NA)。但是,当我使用 - name: Ansible remove file wildcard file: path: "{{ item.path }}" state: absent when: item.path is not search('important.txt') with_items: "{{ wildcard_files_to_delete.files }}" 的{​​{1}}函数时,新列包含所有NA。

请参见以下示例:

dplyr

尝试创建新列

mutate

我知道我可以简单地使用for循环在各个行之间循环并跳过那些包含NA的行,但是我想有一种更好的方法来做到这一点。

解决方法

您可以使用pmin()。

    library(dplyr)
    rand_df <- data.frame(replicate(10,sample(0:10,200,rep=TRUE))) # random df
    names(rand_df) <- letters[seq(from=1,to=10)]  #renaming header
    rand_df$c[2:20] <- NA  # introducing NAs
    head(rand_df)
    #>   a b  c d  e  f g  h  i  j
    #> 1 4 9  9 6 10  2 1 10 10 10
    #> 2 7 3 NA 2  5  9 1  2 10  6
    #> 3 0 3 NA 4  5  6 1  0 10  6
    #> 4 0 7 NA 5  3  6 6  9  4  7
    #> 5 4 4 NA 5  4 10 8  5  6  0
    #> 6 1 3 NA 3  0 10 1  3  7  4


    rand_df <- rand_df %>% mutate(k = 141 * pmin((c/88.42),1))
    head(rand_df)
    #>   a b  c d  e  f g  h  i  j        k
    #> 1 4 9  9 6 10  2 1 10 10 10 14.35196
    #> 2 7 3 NA 2  5  9 1  2 10  6       NA
    #> 3 0 3 NA 4  5  6 1  0 10  6       NA
    #> 4 0 7 NA 5  3  6 6  9  4  7       NA
    #> 5 4 4 NA 5  4 10 8  5  6  0       NA
    #> 6 1 3 NA 3  0 10 1  3  7  4       NA

<sup>Created on 2020-08-17 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

,

以下代码行失败,因为min((c/88.42),1))不会基于每一行进行计算,而是使用整个列,因此您重复的值相同:

rand_df <- rand_df %>% mutate(k = 141 * min((c/88.42),1))

这很好地说明了这种行为:

rand_df %>% mutate(k = min(f),k1 = max(f))) 

有多种解决方法,但是一种方法是将行号添加为列,然后使用group_by:

rand_df  %>% 
  mutate(row = row_number()) %>% 
  group_by(row) %>% 
  mutate(k = 141 * min((c/88.42),1))
,

似乎您在ifelse()函数中添加了额外的参数。我的意思是33.5在这里是不必要的。

此外,下次,请务必正确询问(根据本指南How to make a great R reproducible example

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...