如何通过将两个具有特殊字符的列合并在R数据帧中添加新列?

数据框可以具有多种类型的列,并且可以根据它们的特征将其中一些组合成一个列。例如,如果一列包含字符,而另一列包含数字,则我们可能希望通过用特殊字符分隔来将它们连接起来以将其显示为身份。

示例

请看以下数据帧-

> 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

输出结果

   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

创建具有不同分隔符的新列-

示例

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

输出结果

 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

输出结果

 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

输出结果

 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