vcat
vcat

Reputation: 263

Unite two numerical columns into one in R

I have a df1

X          Y    
CA23-11    002 0033
CA67-55    011 0245

I would like to create df2

Z
CA2311-2-33
CA6755-11-245

My code to do this is df2 <- df %>% unite(Z, X:Y, remove = "0", sep="-") and my error is: Error in if(remove) { : argument in not interpertable as logical

Any assistance in this would be appreciated. Thanks!

Upvotes: 1

Views: 420

Answers (2)

B. Christian Kamgang
B. Christian Kamgang

Reputation: 6489

Here is another solution using built-in functions:

df2 <- data.frame(Z = paste0(sub("-", "", df1$X), gsub("^0*| 0*", "-", df1$Y)))
df2
#               Z
# 1   CA2311-2-33
# 2 CA6755-11-245

Upvotes: 1

akrun
akrun

Reputation: 886948

We could use base R to return the expected output. Read the 'Y' column with read.table so that it automatically reads into numeric columns splitting at the whitespace, cbind with - removed 'X' column, change the format with sprintf

data.frame(Z = do.call(sprintf, c(fmt = '%s-%d-%d', 
      cbind(sub("-", "", df1$X), 
     read.table(text = df1$Y, header = FALSE)))))

-ouptut

          Z
1   CA2311-2-33
2 CA6755-11-245

Or using tidyverse

  1. We separate the 'Y' by splitting at space, and convert the type to numeric
  2. Remove the - in 'X' - str_remove
  3. Then unite the 3 columns together
library(dplyr)
library(tidyr)
library(stringr)
df1 %>% 
   separate(Y, into = c("Y1", "Y2"), convert = TRUE) %>%
   mutate(X = str_remove(X, '-')) %>% 
   unite(Z, X, Y1, Y2, sep= '-')
              Z
1   CA2311-2-33
2 CA6755-11-245

data

df1 <- structure(list(X = c("CA23-11", "CA67-55"), Y = c("002 0033", 
"011 0245")), class = "data.frame", row.names = c(NA, -2L))

Upvotes: 1

Related Questions