英文:
Replace multiple columns in a dataframe with a new column that indicates if the original columns contained any non-missing data
问题
我有一个类似下面简化版的数据框。我希望用一个新列(new_column)替换A:C列,对于有数据的行提供1,对于没有数据的行提供NA。
A B C
NA NA NA
1 0 0
0 1 0
0 0 1
结果应该类似这样:
new_column
NA
1
1
1
我尝试使用dplyr中的mutate命令:
library(dplyr)
df %>%
mutate(new_column = apply(is.na(df[, c("A","B","C")]), 1, all),
.keep = "unused",
.before = "D" ) # 其中D是数据框中的下一列
英文:
I have a dataframe that resembles the simplified one below. I am hoping to replace columns A:C with a new column (new_column) that provides a 1 for a row with data and an NA for a row without data.
A B C
NA NA NA
1 0 0
0 1 0
0 0 1
The outcome would look something like this:
new_column
NA
1
1
1
I tried using the mutate command in dplyr
library(dplyr)
df %>%
mutate(new_column = apply(is.na(df[, c("A","B","C")]), 1, all),
.keep = "unused",
.before = "D" ) # where D is the next column in the data frame
答案1
得分: 1
请尝试以下代码:
library(tidyverse)
data %>% mutate(new=ifelse(!is.na(rowSums(across(c(A:C)))),1,NA))
基于R的方法:
data$new_column <- ifelse(rowSums(is.na(data)) == ncol(data), NA, 1)
英文:
Please try the below code
library(tidyverse)
data %>% mutate(new=ifelse(!is.na(rowSums(across(c(A:C)))),1,NA))
A B C new
1 NA NA NA NA
2 1 0 0 1
3 0 1 0 1
4 0 0 1 1
5 0 0 0 1
base r approach
data$new_column <- ifelse(rowSums(is.na(data)) == ncol(data), NA, 1)
答案2
得分: 0
你可以使用 if_all()
+ is.na
来进行操作:
library(dplyr)
df %>%
mutate(new_column = ifelse(if_all(A:C, is.na), NA, 1),
.keep = "unused")
# new_column
# 1 NA
# 2 1
# 3 1
# 4 1
数据
df <- structure(list(A = c(NA, 1, 0, 0), B = c(NA, 0, 1, 0), C = c(NA,
0, 0, 1)), class = "data.frame", row.names = c(NA, -4L))
英文:
You can use if_all()
+ is.na
:
library(dplyr)
df %>%
mutate(new_column = ifelse(if_all(A:C, is.na), NA, 1),
.keep = "unused")
# new_column
# 1 NA
# 2 1
# 3 1
# 4 1
Data
df <- structure(list(A = c(NA, 1, 0, 0), B = c(NA, 0, 1, 0), C = c(NA,
0, 0, 1)), class = "data.frame", row.names = c(NA, -4L))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论