How to create a duplicate column in an R data frame with different name?

R ProgrammingServer Side ProgrammingProgramming

The easiest way to create a duplicate column in an R data frame is setting the new column with $ sign and if we want to have a different name then we can simply pass a new name. For example, if we have a data frame df that contains a column x and we want to have a new column x1 having same values as in x then it can be done as df$x1<-df$x.

Example

Live Demo

> set.seed(254)
> x<-LETTERS[1:20]
> y<-rnorm(20,1,0.5)
> z<-rpois(20,5)
> a<-rexp(20,3.15)
> b<-runif(20,1,10)
> c<-rnorm(20,25,2)
> df<-data.frame(x,y,z,a,b,c)
> df

Output

x y z a b c
1 A 0.8709244 9 0.072625990 5.125432 26.84561
2 B 1.7993156 3 0.012325435 8.709408 29.25686
3 C 1.0673554 3 0.116407319 6.162522 24.45550
4 D 0.7591570 6 0.132844837 2.233053 22.78211
5 E 1.5101376 11 0.143086285 7.034326 23.05859
6 F 1.1484377 3 0.244212366 2.148795 26.76058
7 G 0.9359269 7 0.453025589 2.572604 25.09759
8 H 1.8053553 4 0.405635819 2.211568 27.35471
9 I 0.0227141 1 0.681061571 1.939878 23.64751
10 J 1.1996667 3 0.073740911 1.018387 24.03023
11 K 0.7770920 3 0.243659953 2.834134 25.22378
12 L 1.2767936 3 0.147269006 9.057046 25.19877
13 M 1.8200664 5 0.087731612 8.750051 27.73970
14 N 0.8545241 5 0.790998463 4.542890 25.35204
15 O 0.9362344 3 0.182957462 1.883847 23.53194
16 P 1.2897848 3 0.531395356 1.718161 24.98864
17 Q 0.6280537 2 0.001632791 6.674458 29.35283
18 R 1.6190747 9 0.327203858 4.176428 25.95383
19 S 1.1918612 2 0.171366109 4.499863 24.79101
20 T 1.4844707 3 0.077982281 6.824088 26.07107

Creating ID column in df which is a duplicate of x:

Example

> df$ID<-df$x
> df

Output

x y z a b c ID
1 A 0.8709244 9 0.072625990 5.125432 26.84561 A
2 B 1.7993156 3 0.012325435 8.709408 29.25686 B
3 C 1.0673554 3 0.116407319 6.162522 24.45550 C
4 D 0.7591570 6 0.132844837 2.233053 22.78211 D
5 E 1.5101376 11 0.143086285 7.034326 23.05859 E
6 F 1.1484377 3 0.244212366 2.148795 26.76058 F
7 G 0.9359269 7 0.453025589 2.572604 25.09759 G
8 H 1.8053553 4 0.405635819 2.211568 27.35471 H
9 I 0.0227141 1 0.681061571 1.939878 23.64751 I
10 J 1.1996667 3 0.073740911 1.018387 24.03023 J
11 K 0.7770920 3 0.243659953 2.834134 25.22378 K
12 L 1.2767936 3 0.147269006 9.057046 25.19877 L
13 M 1.8200664 5 0.087731612 8.750051 27.73970 M
14 N 0.8545241 5 0.790998463 4.542890 25.35204 N
15 O 0.9362344 3 0.182957462 1.883847 23.53194 O
16 P 1.2897848 3 0.531395356 1.718161 24.98864 P
17 Q 0.6280537 2 0.001632791 6.674458 29.35283 Q
18 R 1.6190747 9 0.327203858 4.176428 25.95383 R
19 S 1.1918612 2 0.171366109 4.499863 24.79101 S
20 T 1.4844707 3 0.077982281 6.824088 26.07107 T

Creating Rating column in df which is a duplicate of z:

Example

> df$Rating<-df$z
> df

Output

x y z a b c ID Rating
1 A 0.8709244 9 0.072625990 5.125432 26.84561 A 9
2 B 1.7993156 3 0.012325435 8.709408 29.25686 B 3
3 C 1.0673554 3 0.116407319 6.162522 24.45550 C 3
4 D 0.7591570 6 0.132844837 2.233053 22.78211 D 6
5 E 1.5101376 11 0.143086285 7.034326 23.05859 E 11
6 F 1.1484377 3 0.244212366 2.148795 26.76058 F 3
7 G 0.9359269 7 0.453025589 2.572604 25.09759 G 7
8 H 1.8053553 4 0.405635819 2.211568 27.35471 H 4
9 I 0.0227141 1 0.681061571 1.939878 23.64751 I 1
10 J 1.1996667 3 0.073740911 1.018387 24.03023 J 3
11 K 0.7770920 3 0.243659953 2.834134 25.22378 K 3
12 L 1.2767936 3 0.147269006 9.057046 25.19877 L 3
13 M 1.8200664 5 0.087731612 8.750051 27.73970 M 5
14 N 0.8545241 5 0.790998463 4.542890 25.35204 N 5
15 O 0.9362344 3 0.182957462 1.883847 23.53194 O 3
16 P 1.2897848 3 0.531395356 1.718161 24.98864 P 3
17 Q 0.6280537 2 0.001632791 6.674458 29.35283 Q 2
18 R 1.6190747 9 0.327203858 4.176428 25.95383 R 9
19 S 1.1918612 2 0.171366109 4.499863 24.79101 S 2
20 T 1.4844707 3 0.077982281 6.824088 26.07107 T 3

Creating Weight column in df which is a duplicate of c:

Example

> df$Weight<-df$c
> df

Output

x y z a b c ID Rating Weight
1 A 0.8709244 9 0.072625990 5.125432 26.84561 A 9 26.84561
2 B 1.7993156 3 0.012325435 8.709408 29.25686 B 3 29.25686
3 C 1.0673554 3 0.116407319 6.162522 24.45550 C 3 24.45550
4 D 0.7591570 6 0.132844837 2.233053 22.78211 D 6 22.78211
5 E 1.5101376 11 0.143086285 7.034326 23.05859 E 11 23.05859
6 F 1.1484377 3 0.244212366 2.148795 26.76058 F 3 26.76058
7 G 0.9359269 7 0.453025589 2.572604 25.09759 G 7 25.09759
8 H 1.8053553 4 0.405635819 2.211568 27.35471 H 4 27.35471
9 I 0.0227141 1 0.681061571 1.939878 23.64751 I 1 23.64751
10 J 1.1996667 3 0.073740911 1.018387 24.03023 J 3 24.03023
11 K 0.7770920 3 0.243659953 2.834134 25.22378 K 3 25.22378
12 L 1.2767936 3 0.147269006 9.057046 25.19877 L 3 25.19877
13 M 1.8200664 5 0.087731612 8.750051 27.73970 M 5 27.73970
14 N 0.8545241 5 0.790998463 4.542890 25.35204 N 5 25.35204
15 O 0.9362344 3 0.182957462 1.883847 23.53194 O 3 23.53194
16 P 1.2897848 3 0.531395356 1.718161 24.98864 P 3 24.98864
17 Q 0.6280537 2 0.001632791 6.674458 29.35283 Q 2 29.35283
18 R 1.6190747 9 0.327203858 4.176428 25.95383 R 9 25.95383
19 S 1.1918612 2 0.171366109 4.499863 24.79101 S 2 24.79101
20 T 1.4844707 3 0.077982281 6.824088 26.07107 T 3 26.07107
raja
Published on 19-Nov-2020 07:53:02
Advertisements