w3resource

R Programming - Extract five Levels from a Factor sample


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.)

Sample Solution :

R Programming Code :

# Generate a random sample of 50 letters from LETTERS with replacement
L = sample(LETTERS, size=50, replace=TRUE)

# Print a message indicating the following output is the original data
print("Original data:")

# Print the random sample of letters
print(L)

# Convert the random sample to a factor
f = factor(L)

# Print a message indicating the following output shows the original factors
print("Original factors:")

# Print the factor created from the random sample
print(f)

# Print a message indicating the following output shows only five levels
print("Only five of the levels")

# Print the frequency table of the first five elements in the sample
print(table(L[1:5]))

Output:

[1] "Original data:"
 [1] "H" "N" "O" "D" "L" "E" "H" "U" "W" "W" "S" "Q" "A" "O" "I" "G" "G" "W" "T"
[20] "Z" "I" "S" "B" "P" "I" "F" "L" "B" "X" "A" "J" "V" "X" "C" "U" "A" "C" "W"
[39] "D" "J" "X" "C" "U" "O" "F" "V" "Y" "Z" "W" "Z"
[1] "Original factors:"
 [1] H N O D L E H U W W S Q A O I G G W T Z I S B P I F L B X A J V X C U A C W
[39] D J X C U O F V Y Z W Z
Levels: A B C D E F G H I J L N O P Q S T U V W X Y Z
[1] "Only five of the levels"

D H L N O 
1 1 1 1 1                          

Explanation:

  • Generate Random Sample:
    • L = sample(LETTERS, size=50, replace=TRUE)
    • Creates a random sample of 50 letters from the LETTERS vector, allowing for replacement.
  • Print Original Data:
    • print("Original data:")
    • Prints a message indicating that the following output is the original data.
  • Display Original Data:
    • print(L)
    • Prints the randomly sampled letters.
  • Convert to Factor:
    • f = factor(L)
    • Converts the random sample L into a factor variable f.
  • Print Factors:
    • print("Original factors:")
    • Prints a message indicating that the following output shows the original factors.
  • Display Factors:
    • print(f)
    • Prints the factor representation of the sample L.
  • Print Levels of Interest:
    • print("Only five of the levels")
    • Prints a message indicating that the following output shows only a subset of the levels.
  • Display Table of First Five Elements:
    • print(table(L[1:5]))
    • Prints a frequency table of the first five elements of the sample L.

    R Programming Code Editor:



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

    Previous: Write a R program to convert a given pH levels of soil to an ordered factor.
    Next: Write a R program to create a factor corresponding to height of women data set, which contains height and weights for a sample of women

    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.