[英]R nested functions
I have to calculate the number of missing values per observation in a data set.我必须计算数据集中每个观察值的缺失值数量。 As there are several variables across multiple time periods, I thought it best to try a function to keep my syntax clean.
由于跨多个时间段有多个变量,我认为最好尝试一个函数来保持语法清晰。 The first part of looking up the number of missing values works fine:
查找缺失值数量的第一部分工作正常:
data$NMISS <- data %>%
select('x1':'x4') %>%
apply(1, function(x) sum(is.na(x)))
But when I try turn it into a function I get "Error in select():! NA/NaN argument"但是,当我尝试将其转换为函数时,我得到“select() 错误:!NA/NaN 参数”
library(dplyr)
library(tidyverse)
data <- data.frame(x1 = c(NA, 1, 5, 1),
x2 = c(7, 1, 1, 5),
x3 = c(9, NA, 4, 9),
x4 = c(3, 4, 1, 2))
NMISSfunc <- function (dataFrame,variables) {
dataFrame %>% select(variables) %>%
apply(1, function(x) sum(is.na(x)))
}
data$NMISS2 <- NMISSfunc(data,'x1':'x4')
I think it doesn't like the :
in the range as it will accept c('x1','x2','x3','x4')
instead of 'x1':'x4'
我认为它不喜欢范围内的
:
因为它将接受c('x1','x2','x3','x4')
而不是'x1':'x4'
Some of the ranges are over twenty columns so listing them doesn't really provide a solution to keep the syntax neat.有些范围超过二十列,因此列出它们并不能真正提供保持语法整洁的解决方案。
Any suggestions?有什么建议么?
You are right that you can't use "x4":"x4"
, as this isn't valid use of the :
operator in this context.你是对的,你不能使用
"x4":"x4"
,因为在这种情况下这不是:
运算符的有效使用。 To get this to work in a tidyverse-style, your variables
variable needs to be selectively unquoted inside select
.为了使它以 tidyverse 风格工作,您的
variables
变量需要在select
中有选择地取消引号。 Fortunately, the tidyverse has the curly-curly notation {{variables}}
for handling exactly this situation:幸运的是,tidyverse 有卷曲的符号
{{variables}}
来处理这种情况:
NMISSfunc <- function (dataFrame, variables) {
dataFrame %>%
select({{variables}}) %>%
apply(1, function(x) sum(is.na(x)))
}
Now we can use x1:x4
(without quotes) and the function works as expected:现在我们可以使用
x1:x4
(不带引号)并且该函数按预期工作:
NMISSfunc(data, x1:x4)
#> [1] 1 1 0 0
Created on 2022-12-13 with reprex v2.0.2创建于 2022-12-13,使用reprex v2.0.2
Why not simply,为什么不简单地,
data %>%
mutate(NMISS = rowSums(is.na(select(., x1:x4))))
x1 x2 x3 x4 NMISS
1 NA 7 9 3 1
2 1 1 NA 4 1
3 5 1 4 1 0
4 1 5 9 2 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.