Home > database >  How to join 3 columns (strings) into a one column (list of strings) in R? (excluding NA)
How to join 3 columns (strings) into a one column (list of strings) in R? (excluding NA)

Time:10-06

I have the same question as here, but I have 3 columns (type chr) which I want to combine as one column Z which is a list of the other 3 strings.

|   X   |   Y   |  R     |         Z              |
|-------|-------|--------|------------------------|
|   x1  |   y1  |   r1   |     c(x1,y1, r1)       |  
|   x2  |   y2  |   r2   |     c(x2,y2, r2)       |
|   x3  |   y3  |   NA   |     c(x3,y3)           |
|  ...  |  ...  |   ...  |        ...             |
| x1000 | y1000 | r1000  |  c(x1000,y1000, r1000) |

The base Map(f, ...) function worked for me:

df <- tibble::tribble(
  ~X,    ~Y,    ~R,
  "x1",  "y1", "r1",
  "x2",  "y2", "r2",
  "x3",  "y3", NA,
)

df$Z <-  Map(c, df$X, df$Y, df$R)

class(df$Z) # [1] "list"

df$Z[3] # [1] "x3" "y3" NA  

Now I need to deal with NA which I do NOT want in Z. Basically, I need df$Z[3] to be c(x3,y3) NOT c(x3,y3, NA). Any idea?

CodePudding user response:

Remove the NA values after c()oncatenating:

Map(\(...) {o <- c(...); o[!is.na(o)]}, df$X, df$Y, df$R)
#$x1
#[1] "x1" "y1" "r1"
#
#$x2
#[1] "x2" "y2" "r2"
#
#$x3
#[1] "x3" "y3"

Or without writing out each variable in df:

do.call(Map, c(\(...) {o <- c(...); o[!is.na(o)]}, unname(df)))

CodePudding user response:

Using pmap

library(purrr)
library(dplyr)
df %>% 
    mutate(Z = pmap(across(everything()), ~ na.omit(c(...))))

CodePudding user response:

Do you just need to remove the NA value? If so, this dplyr approach should work:

df %>% filter(!is.na(Z))

If you need to replace it with an empty string, try this:

df$Z[is.na(df$Z)] <- ""
  •  Tags:  
  • r
  • Related