Robust functions

# Define troublesome x and y

x <- c(NA, NA, NA)

y <- c( 1, NA, NA, NA)

both_na <- function(x, y) {

# Add stopifnot() to check length of x and y

stopifnot(length(x)==length(y))

sum(is.na(x) & is.na(y))

}

# Call both_na() on x and y

both_na(x, y)

col_classes <- function(df) {

class_list <- map(df, class)

# Add a check that no element of class_list has length > 1

if (any(map_dbl(class_list, length) > 1)) {

stop("Some columns have more than one class", call. = FALSE)

}

# Use flatten_chr() to return a character vector

flatten_chr(class_list)

}

big_x <- function(df, threshold) {

# Write a check for x not being in df

if(!"x" %in% names(df)){

stop("df must contain variable called x",call.=F)

}

else if("threshold" %in% names(df)){

stop("df must not contain variable called threshold",call.=F)

}

dplyr::filter(df, x > threshold)

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容