Saturday, 7 October 2023

Big Data-Slip15-Write an R program to extract the five of the levels of factor created from a random sample from the LETTERS

 

Slip15

Write an R program to extract the five of the levels of factor created from a random sample from the LETTERS

 

# Create a random sample from LETTERS

set.seed(123) 

sample_letters <- sample(LETTERS, 20, replace = TRUE)

 

# Create a factor from the random sample

sample_factor <- factor(sample_letters)

 

# Extract the first five levels

first_five_levels <- levels(sample_factor)[1:5]

 

# Print the first five levels

print(first_five_levels)

 

Output

 

> set.seed(123) 

> sample_letters <- sample(LETTERS, 20, replace = TRUE)

>

> # Create a factor from the random sample

> sample_factor <- factor(sample_letters)

>

> # Extract the first five levels

> first_five_levels <- levels(sample_factor)[1:5]

>

> # Print the first five levels

> print(first_five_levels)

[1] "C" "E" "I" "J" "K"