我有这样一个数据框:

require(dplyr)

x_1=rnorm(10,0,1)
x_2=rnorm(10,0,1)
x_3=rnorm(10,0,1)
y_1=rnorm(10,0,1)
y_2=rnorm(10,0,1)

data=data.frame(cbind(x_1,x_2,x_3,y_1,y_2))
data[1,1]=NA
data[2,1]=NA
data[5,2]=NA

> data
          x_1        x_2         x_3        y_1        y_2
1          NA  0.9272000  0.29439845 -1.7856567  1.6579091
2          NA  0.2346621  1.09837343  0.3731092  0.6111779
3   0.7315300 -0.5579094 -0.08524311 -2.8661310  1.1545358
4  -0.9469221  0.6929277 -2.67173898  0.6391045 -0.5114099
5   1.5408777         NA  1.33386146 -0.5581233 -2.5733381
6  -0.2852210 -0.9532492  0.03750860 -1.0129503  0.3929722
7  -1.3821487 -2.1865094 -0.03039062  0.3960388 -1.5332137
8  -0.9447420  0.2669902  0.65167163  0.4310705 -1.5300816
9  -0.9023479  0.2068130  0.10868635 -1.1652238 -0.4892178
10 -0.9739177 -0.8094084  0.64103491  0.6063812  0.7248394

我需要创建一个新变量,它统计以"x_3;"开头的变量每行中未缺失的值的数量.为此,我使用了dplyr中的mutateacross个函数.

data=data %>% mutate(sum_no_miss=across(.cols = starts_with("x_"),~ sum(is.na(.x))))

I ran the code without getting error. But I am not getting the ourput that I want. I am getting this. enter image description here

有可能知道我做错了什么吗?

推荐答案

我们可以使用rowSums,与rowwisesum相比,rowSums是矢量化和高效的

library(dplyr)
data %>% 
   mutate(sum_no_miss = rowSums(!is.na(across(starts_with("x_")))))

-输出

          x_1        x_2         x_3        y_1        y_2 sum_no_miss
1          NA  0.9272000  0.29439845 -1.7856567  1.6579091           2
2          NA  0.2346621  1.09837343  0.3731092  0.6111779           2
3   0.7315300 -0.5579094 -0.08524311 -2.8661310  1.1545358           3
4  -0.9469221  0.6929277 -2.67173898  0.6391045 -0.5114099           3
5   1.5408777         NA  1.33386146 -0.5581233 -2.5733381           2
6  -0.2852210 -0.9532492  0.03750860 -1.0129503  0.3929722           3
7  -1.3821487 -2.1865094 -0.03039062  0.3960388 -1.5332137           3
8  -0.9447420  0.2669902  0.65167163  0.4310705 -1.5300816           3
9  -0.9023479  0.2068130  0.10868635 -1.1652238 -0.4892178           3
10 -0.9739177 -0.8094084  0.64103491  0.6063812  0.7248394           3

如果我们想使用sum,那么需要rowwise

data %>%
   rowwise %>%
   mutate(sum_no_miss = sum(!is.na(c_across(starts_with('x_'))))) %>% 
   ungroup

-输出

# A tibble: 10 × 6
      x_1    x_2     x_3    y_1    y_2 sum_no_miss
    <dbl>  <dbl>   <dbl>  <dbl>  <dbl>       <int>
 1 NA      0.927  0.294  -1.79   1.66            2
 2 NA      0.235  1.10    0.373  0.611           2
 3  0.732 -0.558 -0.0852 -2.87   1.15            3
 4 -0.947  0.693 -2.67    0.639 -0.511           3
 5  1.54  NA      1.33   -0.558 -2.57            2
 6 -0.285 -0.953  0.0375 -1.01   0.393           3
 7 -1.38  -2.19  -0.0304  0.396 -1.53            3
 8 -0.945  0.267  0.652   0.431 -1.53            3
 9 -0.902  0.207  0.109  -1.17  -0.489           3
10 -0.974 -0.809  0.641   0.606  0.725           3

在OP的代码中,函数sum在每列的acrossacross个循环中使用,因此sum将是每列中非NA元素的总和,而不是跨行

R相关问答推荐

如何识别组内的行是否在同一列中具有值?

如何将log 2刻度上的数字转换为自然log

使用rlang s arg_match判断函数输入列表

如何在R中正确对齐放射状图中的文本

使用tidyverse / Mutate的存款账户余额

在R中创建一个包含转换和转换之间的时间的列

ggplot的轴标签保存在officer中时被剪切

lightgbm发动机在tidymmodels中的L1正则化""

如何在ggplot中标记qqplot上的点?

如果某些列全部为NA,则更改列

如何通过ggplot2添加短轴和删除长轴?

计算两列中满足特定条件连续行之间的平均值

从服务器在Shiny中一起渲染图标和文本

以NA为通配符的R中的FULL_JOIN以匹配其他数据中的任何值.Frame

从多个可选列中选取一个值到一个新列中

如何创建累加到现有列累计和的新列?

在纵向数据集中创建新行

将统计检验添加到GGPUBR中的盒图,在R

快速合并R内的值

是否从列中删除★符号?