How to create new column with all non-NA values from multiple other columns?

喜夏-厌秋 提交于 2021-01-27 19:37:41

问题


I would like to create a column d, which includes all the non-NA values from the other columns.

I tried ifelse, but cannot figure out how to make it nested in the proper manner, so that the value in column c is included as well.. Perhaps something else than ifelse should be used?

Here is a "dummy" dataframe:

 a <- c(NA, NA, NA, "A", "B", "A", NA, NA)
 b <- c("D", "A", "C", NA, NA, NA, NA, NA)
 c <- c(NA, NA, NA, NA, NA, NA, "C", NA)
 data <- data.frame(a, b, c)

I would like the d column to look like this:

 data$d <- c("D", "A", "C", "A", "B", "A", "C", NA)
 View(data)

回答1:


We can use pmax

do.call(pmax, c(data, list(na.rm=TRUE)))
#[1] "D" "A" "C" "A" "B" "A" "C" NA 

data

data <- data.frame(a, b, c, stringsAsFactors=FALSE)



回答2:


Here is a bit ugly idea assuming that you only have one non-NA value in each row,

data$d <- apply(data, 1, function(i) ifelse(all(is.na(i)), NA, i[!is.na(i)]))
data
#     a    b    c    d
#1 <NA>    D <NA>    D
#2 <NA>    A <NA>    A
#3 <NA>    C <NA>    C
#4    A <NA> <NA>    A
#5    B <NA> <NA>    B
#6    A <NA> <NA>    A
#7 <NA> <NA>    C    C
#8 <NA> <NA> <NA> <NA>



回答3:


I also found this workaround, but I'm not sure I like it:

data <- as.matrix(data)
data[is.na(data)] <- " "
data <- data.frame(data)
data$d <- with(data, paste0(a, b, c), na.rm=TRUE)
View(data)



回答4:


Turns out that should've just out in "" and not " " in NA cells.

And if the space is not avoidable use trimws on the column of the dataframe to remove them afterwards:

  data$d <- trimws(data$d)



回答5:


I was working on a similar problem much later in time and thought I would provide a more generalizable solution using dplyr and stringr.

library(tidyverse)
a <- c(NA, NA, NA, "A", "B", "A", NA, NA)
b <- c("D", "A", "C", NA, NA, NA, NA, NA)
c <- c(NA, NA, NA, NA, NA, NA, "C", NA)
data <- data.frame(a, b, c)

data %>% 
  mutate_all(stringr::str_replace_na, replacement = "") %>% 
  mutate(d = stringr::str_c(a,b,c)) %>%
  mutate_all(stringr::str_replace, pattern = "^$", replacement = NA_character_)
#>      a    b    c    d
#> 1 <NA>    D <NA>    D
#> 2 <NA>    A <NA>    A
#> 3 <NA>    C <NA>    C
#> 4    A <NA> <NA>    A
#> 5    B <NA> <NA>    B
#> 6    A <NA> <NA>    A
#> 7 <NA> <NA>    C    C
#> 8 <NA> <NA> <NA> <NA>

Created on 2019-05-06 by the reprex package (v0.2.1)



来源:https://stackoverflow.com/questions/38374971/how-to-create-new-column-with-all-non-na-values-from-multiple-other-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!