w3resource

R Programming: Assign new names "a", "b" and "c" to the elements of a given list

R Programming: List Exercise-19 with Solution

Write a R program to assign new names "a", "b" and "c" to the elements of a given list.

Sample list: (g1 = 1:10, g2 = "R Programming", g3 = "HTML")

Sample Solution :

R Programming Code :

list1 = list(g1 = 1:10, g2 = "R Programming", g3 = "HTML")
print("Original list:")
print(list1)
names(list1) = c("one", "two", "three")
print("Assign new names 'one', 'two' and 'three' to the elements of the said list")
print(list1)

Sample Output:

[1] "Original list:"
$g1
 [1]  1  2  3  4  5  6  7  8  9 10

$g2
[1] "R Programming"

$g3
[1] "HTML"

[1] "Assign new names 'one', 'two' and 'three' to the elements of the said list"
$one
 [1]  1  2  3  4  5  6  7  8  9 10

$two
[1] "R Programming"

$three
[1] "HTML"                         

R Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a R program to add a new item g4 = "Python" to a given list.
Next: Write a R program to get the length of the first two vectors of a given list.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.