w3resource

R Programming: Change the first level of a factor with another level of a given factor

R Programming: Factors Exercise-2 with Solution

Write a R program to change the first level of a factor with another level of a given factor.

Sample Solution :

R Programming Code :

v = c("a", "b", "a", "c", "b")
print("Original vector:")
print(v)
f = factor(v)
print("Factor of the said vector:")
print(f)
levels(f)[1] = "e"
print(f)

Sample Output:

[1] "Original vector:"
[1] "a" "b" "a" "c" "b"
[1] "Factor of the said vector:"
[1] a b a c b
Levels: a b c
[1] e b e c b
Levels: e b c                         

R Programming Code Editor:



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

Previous: Write a R program to find the levels of factor of a given vector.
Next: Write a R program to create an ordered factor from data consisting of the names of months.

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.