Big_Data_Slip_9

Slip No.7

1) Write an R program to change the first level of a factor with another level of a given factor.

1. Open RStudio.

2. Create a New Script:
Go to File > New File > R Script, or press Ctrl + Shift + N (Windows).

3. Type these code in the Script:
    # Create a factor
    factor_vector <- factor(c("Low", "Medium", "High", "Medium", "Low", "High"))

    # Display the original factor and its levels
    print("Original Factor:")
    print(factor_vector)
    print("Levels of Original Factor:")
    print(levels(factor_vector))

    # Change the first level to another level
    # In this case, let's change the first level "High" to "Very High"
    new_level <- "Very High"
    levels(factor_vector)[1] <- new_level

    # Display the modified factor and its levels
    print("Modified Factor:")
    print(factor_vector)
    print("Levels of Modified Factor:")
    print(levels(factor_vector))


4. Save the Script:
Save the script by going to File > Save, or press Ctrl + S (Windows). Name the file something like modify_factor_level.R.

5. Run the Script:
You can run the script by clicking the Source button at the top of the script editor, or by selecting the entire script and pressing Ctrl + Enter (Windows).

No comments:

Post a Comment