Saturday 7 October 2023

Bigdata -Slip14 Write an R program to concatenate two given factor in a single factor and display in descending order.

 Slip14

Write an R program to concatenate two given factor in a single factor and display in descending order.

factor1 <- factor(c("Apple", "Banana", "Cherry", "Date"))

factor2 <- factor(c("Fig", "Grape", "Honeydew"))

 

# Concatenate the two factors

combined_factor <- c(factor1, factor2)

 

# Sort the combined factor in descending order

combined_factor <- factor(combined_factor, levels = rev(unique(combined_factor)))

 

# Display the sorted combined factor

print(combined_factor)

 

Output

 

> factor1 <- factor(c("Apple", "Banana", "Cherry", "Date"))

> factor2 <- factor(c("Fig", "Grape", "Honeydew"))

>

> # Concatenate the two factors

> combined_factor <- c(factor1, factor2)

>

> # Sort the combined factor in descending order

> combined_factor <- factor(combined_factor, levels = rev(unique(combined_factor)))

>

> # Display the sorted combined factor

> print(combined_factor)

[1] Apple    Banana   Cherry   Date     Fig      Grape    Honeydew

Levels: Honeydew Grape Fig Date Cherry Banana Apple