How to add a new column in an R data frame by combining two columns with a special character?


A data frame can have multiple types of column and some of them could be combined to make a single column based on their characteristics. For example, if a column has characters and the other has numbers then we might want to join them by separating with a special character to showcase them as an identity.

Example

Consider the below data frame −

 Live Demo

> ID<-LETTERS[1:20]
> Frequency<-sample(1:100,20)
> set.seed(111)
> ID<-LETTERS[1:20]
> Frequency<-sample(1:100,20)
> df<-data.frame(ID,Frequency)
> df

Output

   ID Frequency
1 A    78
2 B    84
3 C    83
4 D    47
5 E    25
6 F    59
7 G    69
8 H    35
9 I    72
10 J   26
11 K   49
12 L    45
13 M    74
14 N    8
15 O    100
16 P    96
17 Q    24
18 R    48
19 S    95
20 T    7

Creating new columns with different separators −

Example

> df$Combined_with_hash<-paste(df$ID,df$Frequency,sep="#")
> df

Output

 ID Frequency Combined_with_hash
1 A    78       A#78
2 B    84       B#84
3 C    83       C#83
4 D    47       D#47
5 E    25       E#25
6 F    59       F#59
7 G    69       G#69
8 H    35       H#35
9 I    72       I#72
10 J   26       J#26
11 K   49       K#49
12 L   45       L#45
13 M   74       M#74
14 N    8       N#8
15 O   100     O#100
16 P    96    P#96
17 Q    24    Q#24
18 R    48    R#48
19 S    95    S#95
20 T    7     T#7
> df$Combined_with_hyphen<-paste(df$ID,df$Frequency,sep="-")
> df

Output

 ID Frequency Combined_with_hash Combined_with_hyphen
1 A    78          A#78                A-78
2 B    84          B#84                B-84
3 C    83          C#83                C-83
4 D    47          D#47                D-47
5 E    25          E#25                E-25
6 F    59          F#59                F-59
7 G    69          G#69                G-69
8 H    35          H#35                H-35
9 I    72          I#72                I-72
10 J    26          J#26                J-26
11 K    49          K#49                K-49
12 L    45          L#45                L-45
13 M    74          M#74                M-74
14 N    8          N#8                   N-8
15 O    100        O#100                O-100
16 P    96          P#96                P-96
17 Q    24          Q#24                Q-24
18 R    48          R#48                R-48
19 S    95          S#95                S-95
20 T    7          T#7                   T-7
> df$Combined_with_slash<-paste(df$ID,df$Frequency,sep="/")
> df

Output

 ID Frequency Combined_with_hash Combined_with_hyphen Combined_with_slash
1 A    78       A#78                A-78                   A/78
2 B    84       B#84                B-84                   B/84
3 C    83       C#83                C-83                   C/83
4 D    47       D#47                D-47                   D/47
5 E    25       E#25                E-25                   E/25
6 F    59       F#59                F-59                   F/59
7 G    69       G#69                G-69                   G/69
8 H    35       H#35                H-35                   H/35
9 I    72       I#72                I-72                   I/72
10 J   26       J#26                J-26                   J/26
11 K   49       K#49                K-49                   K/49
12 L   45       L#45                L-45                   L/45
13 M   74       M#74                M-74                   M/74      
14 N   8        N#8                 N-8                     N/8
15 O  100       O#100               O-100                   O/100
16 P  96       P#96                P-96                      P/96
17 Q  24       Q#24                Q-24                   Q/24
18 R  48       R#48                R-48                   R/48
19 S  95       S#95                S-95                   S/95
20 T  7       T#7                   T-7                   T/7

Updated on: 04-Sep-2020

520 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements