R Programming: Convert a given pH levels of soil to an ordered factor

R Programming: Factors Exercise-5 with Solution

Write a R program to convert a given pH levels of soil to an ordered factor.

Note: Soil pH is a measure of the acidity or basicity of a soil. pH is defined as the negative logarithm of the activity of hydronium ions in a solution.
In soils, it is measured in a slurry of soil mixed with water, and normally falls between 3 and 10, with 7 being neutral.

Sample Solution :

R Programming Code :

ph = c(1,3,10,7,5,4,3,7,8,7,5,3,10,10,7)
print("Original data:")
ph_f = factor(ph,levels=c(3,7,10),ordered=TRUE)
print("pH levels of soil to an ordered factor:")

Sample Output:

[1] "Original data:"
 [1]  1  3 10  7  5  4  3  7  8  7  5  3 10 10  7
[1] "pH levels of soil to an ordered factor:"
 [1] <NA> 3    10   7    <NA> <NA> 3    7    <NA> 7    <NA> 3    10   10   7   
Levels: 3 < 7 < 10                         

R Programming Code Editor:

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

Previous: Write a R program to concatenate two given factor in a single factor.
Next: Write a R program to extract the five of the levels of factor created from a random sample from the LETTERS (Part of the base R distribution.)

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.