仅将“ mutate_at”和“ na_if”一起用NA替换零

时间:2019-07-08 05:22:56

标签: r dplyr tidyverse mutate

我的数据采用以下格式:

library(tidyverse)
df <- mtcars
df <- df %>% mutate(vs_doubled = vs * 2) %>% select(mpg, cyl, vs, am, vs_doubled)

head(df)


#>    mpg cyl vs am vs_doubled
#> 1 21.0   6  0  1          0
#> 2 21.0   6  0  1          0
#> 3 22.8   4  1  1          2
#> 4 21.4   6  1  0          2
#> 5 18.7   8  0  0          0
#> 6 18.1   6  1  0          2

我正在尝试使用mutate_atna_if将0值设置为NA-但仅适用于特定列(“ vs”和“ am”)。我想在列“ vs_doubled”中保留零。

我不太正确,因为以下行不起作用:

df <- df %>% mutate_at(.vars = c("vs", "am"), .funs = na_if(y = 0))

3 个答案:

答案 0 :(得分:4)

我们可以做到

[@section Scripts{
    <script type="text/javascript">
        function editRole(id) {
            if (!id) { return false; }
            tools.AjaxPost("/Roles/Edit", { id: id }).always(function (data) {
                tools.setHtmlElement("edit", data.responseText);
                tools.showModal("roleModal");
            });
        }
    </script>
}

//partial view 
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div id="roleModal" class="modal fade" role="dialog" tabindex="-1">
        <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">تعديل الدور</h4>
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    @Html.HiddenFor(model => model.ID)
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Save" class="btn btn-default" />
                        </div>
                    </div>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>

        </div>
    </div>
}][1]

或者另一种方式是

library(dplyr)
df %>%  mutate_at(vars(vs,am), ~na_if(.,0)) %>% head

#   mpg cyl vs am vs_doubled
#1 21.0   6 NA  1          0
#2 21.0   6 NA  1          0
#3 22.8   4  1  1          2
#4 21.4   6  1 NA          2
#5 18.7   8 NA NA          0
#6 18.1   6  1 NA          2

df %>% mutate_at(vars(vs,am), na_if, 0) 是Purrr样式的公式语法,而~代表列的值。它是匿名函数调用的替代方法,使用该函数,您可以将上述函数编写为

.

另外,所示的另一种方法不需要df %>% mutate_at(vars(vs,am), function(x) na_if(x, 0)) ,我们可以直接使用附加参数传递函数(此处~为0)。


当然还有其他方法可以不使用y

na_if

或与基R相同

df %>% mutate_at(vars(vs, am), ~replace(., . == 0, NA)) 

答案 1 :(得分:1)

我们可以将case_whenmutate_at一起使用

library(tidyverse)
df %>% 
     mutate_at(vars(vs, am), ~ case_when(!. ~ NA_real_, TRUE ~ .)) %>%
     head

答案 2 :(得分:0)

我一直在寻找将数据框中的某些值(值<2 )更改为NA的方法。我在其中一列中有数据时间,但是na_if()绝对无法正常工作。我使用了Ronak的建议-我唯一的修改是将 tibble 转换为数据框。

建议的解决方案:

**df <**- df %>% mutate_at(vars(vs, am), ~replace(., . == 0, NA)) 

我的脚本中的摘录/应用程序:

wave_T_max <- wave_T_max %>% 
      mutate_at(vars(Value), ~replace(Value, Value <2, NA))