How to concatenate numerical columns in an R data frame?

R ProgrammingServer Side ProgrammingProgramming

If we have values scattered in multiple columns in an R data frame then we need to combine them and create a single column, this combining process is called concatenation. The scatteredness of the values mostly happens when the data is not well formatted to be loaded in R. Therefore, to deal with this scatteredness problem we need to use apply function.

Consider the below data frame −

Example

 Live Demo

x1<-rpois(20,5)
y1<-rpois(20,5)
z1<-rpois(20,5)
a1<-rpois(20,5)
df1<-data.frame(x1,y1,z1,a1)
df1

Output

  x1 y1 z1 a1
1 3  2  1  8
2 5  2  5  8
3 9  4  1  4
4 3  4  4  4
5 5  4  4  4
6 6  5  5  3
7 5  5  5  6
8 7  3  6  4
9 5  7  6  8
10 3 3  4  5
11 7 4  4  8
12 4 3  2  4
13 6 3  6  6
14 7 5  3  1
15 6 9  8  3
16 9 6  11 6
17 5 5  6  6
18 2 4  4  9
19 1 8  2  4
20 11 4 4  2

Creating a new column in df1 by concatenating the values in all the columns of df1 −

Example

df1$all<-apply(df1,1,paste,collapse="")
df1

Output

  x1 y1 z1 a1 all
1 3  2  1  8  3218
2 5  2  5  8  5258
3 9  4  1  4  9414
4 3  4  4  4  3444
5 5  4  4  4  5444
6 6  5  5  3  6553
7 5  5  5  6  5556
8 7  3  6  4  7364
9 5  7  6  8  5768
10 3 3  4  5  3345
11 7 4  4  8  7448
12 4 3  2  4  4324
13 6 3  6  6  6366
14 7 5  3  1  7531
15 6 9  8  3  6983
16 9 6  11 6  96116
17 5 5  6  6  5566
18 2 4  4  9  2449
19 1 8  2  4  1824
20 11 4 4  2  11442

Example

 Live Demo

x2<-sample(LETTERS[1:4],20,replace=TRUE)
y2<-sample(LETTERS[1:4],20,replace=TRUE)
z2<-sample(LETTERS[1:4],20,replace=TRUE)
df2<-data.frame(x2,y2,z2)
df2

Output

  x2 y2 z2
1 D  D  C
2 D  D  C
3 C  D  A
4 D  D  D
5 A  C  B
6 A  D  A
7 B  B  D
8 B  C  A
9 D  A  C
10 A A  B
11 B D  D
12 A D  C
13 D C  C
14 C A  B
15 A D  A
16 A D  C
17 D C  A
18 A B  B
19 D A  C
20 D B  C

Creating a new column in df2 by concatenating the values in all the columns of df2 −

Example

df2$all<-apply(df2,1,paste,collapse="")
df2

Output

 x2 y2 z2 all
1 D D  C  DDC
2 D D  C  DDC
3 C D  A  CDA
4 D D  D  DDD
5 A C  B  ACB
6 A D  A  ADA
7 B B  D  BBD
8 B C  A  BCA
9 D A  C  DAC
10 A A B  AAB
11 B D D  BDD
12 A D C  ADC
13 D C C  DCC
14 C A B  CAB
15 A D A  ADA
16 A D C  ADC
17 D C A  DCA
18 A B B  ABB
19 D A C  DAC
20 D B C  DBC
raja
Published on 06-Feb-2021 10:58:31
Advertisements