简体   繁体   English

如何合并 R 中的两列?

[英]How can I merge two columns in R?

My dataframe has 2 columns which look similar to this:我的数据框有 2 列,看起来与此类似:

a    b

NA   NA
yes  NA
no   NA
yes  NA
NA   yes
NA   no 
NA   NA
Na   yes

What I would then like as output is:我想要的输出是:

ab

NA
yes
no
yes
yes
no
NA
yes

Note that:注意:

  • In the original columns, there will always be an NA in any given row.在原始列中,任何给定行中总会有一个 NA。
  • For certain rows both columns will be NA对于某些行,两列都是 NA

Any idea how I could get to the desired output?知道如何获得所需的输出吗?

  • dplyr::coalesce
> with(dat, dplyr::coalesce(a, b))
[1] NA    "yes" "no"  "yes" "yes" "no"  NA    "yes"
  • ifelse for base R基 R 的ifelse
> with(dat, ifelse(!is.na(a), a, ifelse(!is.na(b), b, NA)))
[1] NA    "yes" "no"  "yes" "yes" "no"  NA    "yes"
  • max.col for base R基数 R 的max.col
> dat[cbind(1:nrow(dat), max.col(!is.na(dat)))]
[1] NA    "yes" "no"  "yes" "yes" "no"  NA    "yes"

Using dplyr you can wrap it pretty neatly:使用dplyr你可以把它包装得非常整齐:

library(dplyr)

df %>% rowwise() %>% summarize(ab = max(a,b, na.rm = T))
dat <- data.frame(a = c(NA, "yes", "no", "yes", NA, NA, NA, NA),
                  b = c(NA, NA, NA, NA, "yes", "no", NA, "yes"))

require(tidyverse)

dat %>% 
  rowwise() %>% 
  mutate(ab = max(a,b, na.rm = TRUE))

Use apply :使用apply

> apply(df, 1, max, na.rm=TRUE)
[1] NA    "yes" "no"  "yes" "yes" "no"  NA    "yes"

Assignment:任务:

df$ab <- apply(df, 1, max, na.rm=TRUE)
# Import data: df => data.frame 
df <- structure(list(a = c(NA, "yes", "no", "yes", NA, NA, NA, NA), 
b = c(NA, NA, NA, NA, "yes", "no", NA, "yes")), class = "data.frame", row.names = c(NA, 
-8L))

# Function to coalesce vectors: br_coalesce => function
br_coalesce <- function(...){
   # Coalesce vectors or data.frames: res => vector
   res <- Reduce(function(x, y) {
         x <- replace(x, is.na(x), y[is.na(x)])
      },
      list(...)
   )
   # Explicitly define returned vectors: character vector => env
   return(res)
}

# Apply function: character vector / data.frame => stdout(console)
br_coalesce(df$a, df$b)

Tidyverse solution: Tidyverse 解决方案:

library(tidyverse)
df %>% 
   transmute(res = coalesce(a, b))

data.table solution:数据表解决方案:

library(data.table)
fcoalesce(df$a, df$b)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM