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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework