如何在R数据框中创建具有不同名称的重复列?


在R数据框中创建重复列最简单的方法是使用$符号设置新列,如果我们想要不同的名称,只需传入一个新名称即可。例如,如果我们有一个包含列x的数据框df,并且我们想要一个具有与x相同值的新列x1,则可以将其设置为df$x1<-df$x。

示例

在线演示

> 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

输出

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

在df中创建ID列,它是x的副本

示例

> df$ID<-df$x
> df

输出

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

在df中创建Rating列,它是z的副本

示例

> df$Rating<-df$z
> df

输出

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

在df中创建Weight列,它是c的副本

示例

> df$Weight<-df$c
> df

输出

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

更新于:2020年11月19日

1K+ 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告