How to change the name of variables in a list in R?

R ProgrammingServer Side ProgrammingProgramming

The name of variables in a list are actually the list elements. These elements can be either named or unnamed. The naming can be done with the help of names function and renaming can be done in the same way as well. For example, if we have a list called LIST then the names of the element in LIST can be done by using the below command: names(LIST)<−c("New_name1"," New_name2"," New_name3")

Example1

 Live Demo

List1<−list(x1=rnorm(50),x2=rnorm(50),x3=rnorm(50),x4=rnorm(50))
List1

Output

$x1
[1] 0.22045578 0.87621081 −2.14896295 −0.23375135 −1.99849285 −1.23478040
[7] 0.84630372 −0.04649482 0.40364277 0.76745466 0.86710150 −1.25458115
[13] −0.15779799 −0.51231678 0.41302087 0.56425626 0.72606595 −0.41510222
[19] 0.04161530 0.22429205 0.78640914 −0.42336284 −0.36753396 −1.76702031
[25] −0.18353788 0.83487200 −0.58720051 −0.35893240 2.11412884 0.64541105
[31] −0.31231996 1.18544828 0.55379896 −0.05375394 2.12746303 −0.18770648
[37] 0.09875784 1.91037815 1.62145572 2.09306799 −0.69223593 −2.28719811
[43] −1.65560167 0.56950942 −0.47122259 −0.03320968 0.37736707 1.15771800
[49] 1.39865000 2.11850393
$x2
[1] −1.08745987 1.21603339 0.67722530 −0.72637691 0.11284394 0.06521357
[7] −0.63062721 0.51885546 −0.80294450 −0.68162320 −0.03716272 −0.28727432
[13] −1.02602717 1.39835918 0.83669025 −0.32989626 0.53480226 −1.20725319
[19] 0.62876732 0.57616473 1.23622388 0.47553964 −1.54714749 0.05546880
[25] 0.68006209 −1.35020157 0.24510244 −0.30734854 0.73136457 0.20138181
[31] −1.57546283 0.02353878 0.80164111 −2.55562737 −0.52691068 −1.31285889
[37] 0.80086135 0.71310077 1.03191486 1.68180850 −0.22631040 −0.19792903
[43] 1.04370225 −0.44173735 −1.63662910 0.21156259 −0.46483173 −0.66235721
[49] −0.13295360 −1.32170171
$x3
[1] 0.07669278 −0.38499088 0.94730644 0.02168627 −0.45829401 0.22886214
[7] −1.39640763 0.92429420 0.06621604 0.80289084 −0.06052526 0.08932862
[13] 0.40131138 −0.88856060 1.44916943 0.20551047 −0.05611997 −0.63985946
[19] 0.71778100 −0.06450029 0.13242960 0.03471508 0.28165263 0.91927825
[25] −1.04272315 −0.21362268 1.77794708 1.46094086 −0.36455082 −0.61316042
[31] 0.49590678 0.24563859 −0.12790481 −1.25043942 1.63435096 0.97543576
[37] 1.08407794 −0.35938727 −1.01551967 1.33552147 −0.15556228 0.92660850
[43] 0.88924569 0.14511699 −0.81864663 −1.23682286 2.31722565 0.05072431
[49] 0.29083551 −1.81128974
$x4
[1] −1.14514769 1.28087282 −1.35947002 1.88497074 −1.36860156 0.54394041
[7] −1.23803017 1.87713234 −1.44581007 −0.15336740 −0.94568862 0.31240499
[13] 0.27472438 −0.31544232 −0.38436760 −1.42893573 0.96341769 0.61378162
[19] −0.36855443 −1.80704466 1.35543844 0.44568017 0.16479479 0.02997777
[25] −0.67611291 1.13604965 2.16933213 −1.08901929 −1.10272610 1.30263617
[31] −2.77832551 0.34131156 −0.22354979 −0.19877160 1.63330268 −1.86962151
[37] −0.25008885 −1.37075933 1.45126184 −2.13790977 −0.57726105 0.23644945
[43] 1.09985656 0.56128271 2.30362701 2.02089590 −0.05787852 0.44209338
[49] −1.30333114 −0.03522043

Example

names(List1)<−c("Norm_default1","Norm_default2","Norm_default3","Norm_default4")
List1

Output

$Norm_default1
[1] 0.22045578 0.87621081 −2.14896295 −0.23375135 −1.99849285 −1.23478040
[7] 0.84630372 −0.04649482 0.40364277 0.76745466 0.86710150 −1.25458115
[13] −0.15779799 −0.51231678 0.41302087 0.56425626 0.72606595 −0.41510222
[19] 0.04161530 0.22429205 0.78640914 −0.42336284 −0.36753396 −1.76702031
[25] −0.18353788 0.83487200 −0.58720051 −0.35893240 2.11412884 0.64541105
[31] −0.31231996 1.18544828 0.55379896 −0.05375394 2.12746303 −0.18770648
[37] 0.09875784 1.91037815 1.62145572 2.09306799 −0.69223593 −2.28719811
[43] −1.65560167 0.56950942 −0.47122259 −0.03320968 0.37736707 1.15771800
[49] 1.39865000 2.11850393
$Norm_default2
[1] −1.08745987 1.21603339 0.67722530 −0.72637691 0.11284394 0.06521357
[7] −0.63062721 0.51885546 −0.80294450 −0.68162320 −0.03716272 −0.28727432
[13] −1.02602717 1.39835918 0.83669025 −0.32989626 0.53480226 −1.20725319
[19] 0.62876732 0.57616473 1.23622388 0.47553964 −1.54714749 0.05546880
[25] 0.68006209 −1.35020157 0.24510244 −0.30734854 0.73136457 0.20138181
[31] −1.57546283 0.02353878 0.80164111 −2.55562737 −0.52691068 −1.31285889
[37] 0.80086135 0.71310077 1.03191486 1.68180850 −0.22631040 −0.19792903
[43] 1.04370225 −0.44173735 −1.63662910 0.21156259 −0.46483173 −0.66235721
[49] −0.13295360 −1.32170171
$Norm_default3
[1] 0.07669278 −0.38499088 0.94730644 0.02168627 −0.45829401 0.22886214
[7] −1.39640763 0.92429420 0.06621604 0.80289084 −0.06052526 0.08932862
[13] 0.40131138 −0.88856060 1.44916943 0.20551047 −0.05611997 −0.63985946
[19] 0.71778100 −0.06450029 0.13242960 0.03471508 0.28165263 0.91927825
[25] −1.04272315 −0.21362268 1.77794708 1.46094086 −0.36455082 −0.61316042
[31] 0.49590678 0.24563859 −0.12790481 −1.25043942 1.63435096 0.97543576
[37] 1.08407794 −0.35938727 −1.01551967 1.33552147 −0.15556228 0.92660850
[43] 0.88924569 0.14511699 −0.81864663 −1.23682286 2.31722565 0.05072431
[49] 0.29083551 −1.81128974
$Norm_default4
[1] −1.14514769 1.28087282 −1.35947002 1.88497074 −1.36860156 0.54394041
[7] −1.23803017 1.87713234 −1.44581007 −0.15336740 −0.94568862 0.31240499
[13] 0.27472438 −0.31544232 −0.38436760 −1.42893573 0.96341769 0.61378162
[19] −0.36855443 −1.80704466 1.35543844 0.44568017 0.16479479 0.02997777
[25] −0.67611291 1.13604965 2.16933213 −1.08901929 −1.10272610 1.30263617
[31] −2.77832551 0.34131156 −0.22354979 −0.19877160 1.63330268 −1.86962151
[37] −0.25008885 −1.37075933 1.45126184 −2.13790977 −0.57726105 0.23644945
[43] 1.09985656 0.56128271 2.30362701 2.02089590 −0.05787852 0.44209338
[49] −1.30333114 −0.03522043

Example2

 Live Demo

List2<−list(y1<−rpois(100,5),y2=rpois(100,5),y3=rpois(100,5),y4=rpois(100,5),y5=rpois(100,5))
List2

Output

[[1]]
[1] 6 6 4 6 8 8 3 4 6 4 6 6 6 1 2 3 2 6 3 5 3 8 3 10 5
[26] 4 5 7 2 1 7 2 7 0 5 7 7 5 10 5 7 4 4 3 4 2 7 6 3 4
[51] 10 4 5 5 2 3 5 7 7 5 4 1 3 5 4 4 2 4 5 7 4 2 5 9 6
[76] 5 4 4 8 6 3 2 4 6 2 2 5 4 7 5 6 2 8 6 5 5 5 7 8 6
$y2
[1] 4 5 3 5 4 6 8 6 5 4 6 3 3 3 3 5 3 6 4 11 7 7 6 8 5
[26] 1 6 6 1 9 3 3 4 6 8 7 3 5 1 2 3 5 7 3 1 3 7 4 5 3
[51] 5 5 7 9 11 3 5 4 6 4 2 6 4 3 7 7 1 6 3 4 9 6 6 4 6
[76] 3 4 8 8 3 3 4 1 7 7 5 1 5 2 3 4 6 3 3 3 5 4 6 8 4
$y3
[1] 4 5 0 4 9 3 7 1 5 3 0 5 8 4 3 8 9 3 7 3 6 6 5 10 9
[26] 6 2 5 3 6 5 3 6 5 6 4 6 2 5 5 2 6 2 6 4 4 7 5 5 5
[51] 5 5 3 7 6 5 3 4 1 7 8 7 5 2 2 5 6 3 5 5 5 9 3 8 5
[76] 6 4 4 4 4 1 5 5 5 4 6 6 5 5 3 2 8 2 5 5 5 3 9 2 2
$y4
[1] 4 3 3 7 3 8 5 2 4 2 0 4 7 7 8 11 8 8 1 5 4 3 8 6 5
[26] 5 7 10 2 4 5 5 6 4 8 5 8 9 5 6 4 4 2 7 5 2 7 3 3 3
[51] 4 3 5 5 6 8 2 6 6 2 4 1 5 1 5 6 2 1 3 2 5 5 6 4 7
[76] 4 3 6 4 3 3 3 3 7 3 7 3 4 3 7 7 5 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