Amar
Amar

Reputation: 1373

Paste every two columns together in R

Objective: Part of a function I'm writing requires each value of each variable/column be duplicated. I have code that duplicates each column but now I need to concatenate them together. eg 123 to 123123.

With that in mind, given a dataframe:

df <- data.frame(a = c(rep(c(111,222,333),2)), b = c(rep(c(111,222,333),2)), 
                 c = c(rep(c(444,555,666),2)), d = c(rep(c(444,555,666),2)))

How do you paste every two columns together to achieve this or similar:

"111111" "222222" "333333" "111111" "222222" "333333" "444444" "555555" "666666" "444444" "555555" "666666"

In other words, concatenate df[, 1] and df[, 2], df[, 3] and df[, 4] etc.

I have tried this code from here:

i <- seq.int(1L,length(df),by = 2L)
paste0(df[, i],df[ , i])

but it returns this:

[1] "c(111, 222, 333, 111, 222, 333)c(111, 222, 333, 111, 222, 333)"
[2] "c(444, 555, 666, 444, 555, 666)c(444, 555, 666, 444, 555, 666)"

I'm not sure why paste is returning a the values of df[, i] as "c(...)".

Using:

platform       x86_64-apple-darwin15.6.0   
arch           x86_64                      
os             darwin15.6.0                
system         x86_64, darwin15.6.0        
status                                     
major          3                           
minor          6.1                         
year           2019                        
month          07                          
day            05                          
svn rev        76782                       
language       R                           
version.string R version 3.6.1 (2019-07-05)
nickname       Action of the Toes   

Upvotes: 1

Views: 435

Answers (2)

Khaynes
Khaynes

Reputation: 1986

A method using data.table ...

library(data.table)

d <- rbind(data.table(df[, 1:2]), data.table(df[, 3:4]), use.names = F)

paste0(d$a, d$b)
# [1] "111111" "222222" "333333" "111111" "222222" "333333" "444444" "555555" "666666" "444444" "555555" "666666"

Upvotes: 1

Ronak Shah
Ronak Shah

Reputation: 388817

You could use mapply to paste every two columns together.

i <- seq.int(1L,length(df),by = 2L)
c(mapply(paste0, df[i], df[i + 1]))

#[1] "111111" "222222" "333333" "111111" "222222" "333333" "444444" "555555" 
#    "666666" "444444" "555555" "666666"

Upvotes: 5

Related Questions