r - R根据其他列值设置列值
问题描述
我需要根据其他列的值将列的值设置为 0 或 1。
如果它们为 0 或 NA,则新列应为 1。
我想过:
ifelse(df[,53:62]==0|NA, df$newCol <- 1, df$newCol <- 0)
但我最后我在新列中只得到 1
谢谢你的帮助
解决方案
我认为 tidyverse 非常适合这个常见用例
library(tidyverse)
df_example <- matrix(c(0,1),ncol = 100,nrow = 100) %>%
as_tibble()
df_example %>%
mutate(across(.cols = 53:62,
.fns = ~ if_else(.x == 0|is.na(.x),
1,
0))
) %>%
select(V54) # example**
推荐阅读
- node.js - 发生事件时发送服务器响应,但这会导致(无法修改标头,标头已发送)错误
- python - 如何使用python检测耳机输入
- fiware-orion - 实体值属性包含特殊字符时的问题
- logging - 从 kubernetes 多个 pod 中获取特定文本的 Grep
- python - 如何在 django 中处理特定查询?
- angular - 角度应用程序中的ngrx导入
- c# - 记录中的数据注释在单元测试中失败
- java - 从服务到 dao 的线程“主”java.lang.NullPointerException 中的异常
- schema.org - 来自 schema.org 的 JSON LD 序列化
- object - Google Sheets Script,如何创建工作表标签的新松散对象