- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
mtcars1 <- mtcars %>%
mutate(blah = LETTERS[seq(1,26, length.out = nrow(.))],
blah2 = letters[seq(1,26, length.out = nrow(.))])
# sprinkle some random NA values
mtcars1$blah[c(1,3,5,10,11)] <- NA
mtcars1$blah2[c(1,2,5,15,20)] <- NA
mtcars1 %>%
mutate_at(blah:blah2, function(x) {
if_else(is.na(x), 0, x)
})
返回:
Error in check_dot_cols(.vars, .cols) : object 'blah' not found
如何使用 dplyr/tidyverse 方法在多个列中将 NA 替换为 0?
最佳答案
我们可以将列名的 ranger 包装在 vars
中,并确保返回 type
与 if_else
相同(或者case_when
)基于?if_else
Compared to the base ifelse(), this function is more strict. It checks that true and false are the same type. This strictness makes the output type more predictable, and makes it somewhat faster.
library(dplyr)
mtcars1 %>%
mutate_at(vars(blah:blah2), ~ if_else(is.na(.), '0', .))
这里,使用带有 ~
的 tidyverse 简洁选项,而不是匿名函数调用 (function(x)
),并且 'blah'、'blah2' 分别是character
类型,true
的返回类型也带引号 ('0'
)
专门用于替换 NA
的另一个选项是 tidyr
中的 replace_na
,而不是 if_else
library(tidyr)
mtcars1 %>%
mutate_at(vars(blah:blah2), replace_na, '0')
关于r - mutate_at 将 NA 替换为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58563691/
我有一个问题: df = tibble(one = list('a', 'b'), two = list(c('p1', 'p2', 'p3'), NA_character_), three = li
我试图从所有其他列中减去一列,但并非所有列都被修改。 test % mutate_at(vars(-a), funs(. - !!as.name(col))) a b c
我刚问了a question about generating multiple columns at once with dplyr ,我是个笨蛋,把问题简单化了,还有另一个问题。我想找到一种 dp
使用 dplyr , 有没有办法在不使用 rowwise() 的情况下逐行选择性地改变列? 例如,给定下面的输入,我想在以“pre_”为前缀的列中用零替换负数: df % rowwise() %
我正在尝试收紧 %>%管道工作流,我需要将相同的函数应用于多列,但每次更改一个参数。我觉得purrr的 map或 invoke函数应该会有所帮助,但我无法理解它。 我的数据框包含预期生命周期、贫困率和
我一直在玩dplyr::mutate_at通过对某些列应用相同的函数来创建新变量。当我命名我的函数 在 .funs参数,mutate 调用创建带有后缀的新列而不是替换现有列,这是我在 this thr
这个问题已经有答案了: Create new variables with mutate_at while keeping the original ones (1 个回答) 已关闭 3 年前。 我有
如果我有这样的数据框 set.seed(1) df % mutate(Y = if_else(X > 10, -Y, Y), Z = if_else(X > 10,
我定义了必须应用于不同数据框列集的函数。例如,mtcars 我想将 as.integer() 函数应用于列 c("mpg", "cyl") 和 >as.logic() 到 c("vs", "am")
如果我有这样的数据框 set.seed(1) df % mutate(Y = if_else(X > 10, -Y, Y), Z = if_else(X > 10,
有一个 data.frame (tibble) 如下: require(dplyr) df_org 1 1 A 2 2 B 使用 mutate_at 会抛出以下错误
我经常遇到必须重新编码遵循相同结构的多个列并将它们保存到具有不同名称的列中的问题。如果我可以覆盖它们,这将只是 dplyr 中的一行,但由于我还想保留原始列,我不知道一个好的解决方案。下图。 这将是我
这个问题在这里已经有了答案: Applying a function to every row of a table using dplyr? (8 个回答) 关闭 3 年前。 我有一个列,我希望将其
我可以在单个 mutate_at 步骤中在同一列上连续使用多个函数,例如:(sqrt(log(x))) library(dplyr) head(mtcars) %>% select(mpg,
我正在尝试重新编码几个变量,但使用不同的重新编码方案。重新编码方案保存在一个列表中,其中每个元素都是 old = new 形式的命名向量。每个元素是数据框中每个变量的重新编码方案 我正在使用 muta
我正在尝试对 R 中数据框的多个列应用相同的函数。 我有一个单独的数据框,其中包含主数据框中的一些列。当主数据框中的列在另一个数据框中时,我想将这两列加在一起,但我似乎无法弄清楚如何访问 mutate
我有一个 data.frame具有大量名称遵循模式的列。如: df % mutate( x = ifelse(is.na(x_1), x_2, x_1), y = ifelse(i
require(dplyr) df % mutate_at(.vars = col_var, .funs = list(~ ifelse(df[, col_var+1] =
这是我的数据: ID a b c d 1 x 1 2 3 2 y 1 2 3 3 z NA NA NA 4 z 1 2 3 5 y NA NA NA 现在,如果我想用 a 组的 b 平均值替
我想创建一个函数,可以根据用户提供的输入变量和截止值列表以编程方式添加变量。 具体来说,我想定义一个函数 myfun 1
我是一名优秀的程序员,十分优秀!