合并列以删除 NA [英] Combine column to remove NA's
问题描述
我在 R 中有一些列,对于每一行,其中只有一个值,其余的将是 NA.我想将这些与非 NA 值组合成一列.有谁知道这样做的简单方法.例如我可以有如下:
data <- data.frame('a' = c('A','B','C','D','E'),'x' = c(1,2,NA,NA,NA),'y' = c(NA,NA,3,NA,NA),'z' = c(NA,NA,NA,4,5))
所以我会
'a' 'x' 'y' 'z'A 1 NA NAB 2 NA NAC NA 3 NADNA NA 4E NA NA 5
我会得到
'a' 'mycol'1乙 2C 345
包含 NA 的列的名称根据查询中较早的代码而变化,因此我将无法显式调用列名称,但我将包含 NA 的列的列名称存储为向量,例如在这个例子中 cols <- c('x','y','z')
,所以可以使用 data[, cols]
调用列.>
任何帮助将不胜感激.
谢谢
基于 dplyr::coalesce
的解决方案可以是:
data %>% mutate(mycol = coalesce(x,y,z)) %>%选择(一个,mycol)# 一个霉菌# 1 一个 1# 2 乙 2# 3 C 3# 4 D 4# 5 E 5
数据
data <- data.frame('a' = c('A','B','C','D','E'),'x' = c(1,2,NA,NA,NA),'y' = c(NA,NA,3,NA,NA),'z' = c(NA,NA,NA,4,5))
I have some columns in R and for each row there will only ever be a value in one of them, the rest will be NA's. I want to combine these into one column with the non-NA value. Does anyone know of an easy way of doing this. For example I could have as follows:
data <- data.frame('a' = c('A','B','C','D','E'),
'x' = c(1,2,NA,NA,NA),
'y' = c(NA,NA,3,NA,NA),
'z' = c(NA,NA,NA,4,5))
So I would have
'a' 'x' 'y' 'z'
A 1 NA NA
B 2 NA NA
C NA 3 NA
D NA NA 4
E NA NA 5
And I would to get
'a' 'mycol'
A 1
B 2
C 3
D 4
E 5
The names of the columns containing NA changes depending on code earlier in the query so I won't be able to call the column names explicitly, but I have the column names of the columns which contains NA's stored as a vector e.g. in this example cols <- c('x','y','z')
, so could call the columns using data[, cols]
.
Any help would be appreciated.
Thanks
A dplyr::coalesce
based solution could be as:
data %>% mutate(mycol = coalesce(x,y,z)) %>%
select(a, mycol)
# a mycol
# 1 A 1
# 2 B 2
# 3 C 3
# 4 D 4
# 5 E 5
Data
data <- data.frame('a' = c('A','B','C','D','E'),
'x' = c(1,2,NA,NA,NA),
'y' = c(NA,NA,3,NA,NA),
'z' = c(NA,NA,NA,4,5))
这篇关于合并列以删除 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!