Friday, 6 October 2023

BigDATA-Slip9 Write an R program to change the first level of a factor with another level of a given factor.

 

Slip9

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

 

V1 = c("a", "b", "a", "c", "b")

print("Original vector:")

print(V1)

f1 = factor(V1)

print("Factor of the said vector:")

print(f1)

levels(f1)[1] = "e"

print(f1)

OUTPUT

> V1 = c("a", "b", "a", "c", "b")

> print("Original vector:")

[1] "Original vector:"

> print(V1)

[1] "a" "b" "a" "c" "b"

> f1 = factor(V1)

> print("Factor of the said vector:")

[1] "Factor of the said vector:"

> print(f1)

[1] a b a c b

Levels: a b c

> levels(f1)[1] = "e"

> print(f1)

[1] e b e c b

Levels: e b c