由于没有其他人更详细,我会(希望是对的):
#random data to simulate yours (but with shorter names...)
DF <- data.frame(Name = c("Exp1", "Exp2_1", "Exp3_1",
"Exp2_1", "Exp1", "Exp3_1", "Dat1", "Dat2_1", "Dat3_1"), stringsAsFactors = F)
#> DF
# Name
#1 Exp1
#2 Exp2_1
#3 Exp3_1
#4 Exp2_1
#5 Exp1
#6 Exp3_1
#7 Dat1
#8 Dat2_1
#9 Dat3_1
#turn the column to factor
DF$Name <- factor(DF$Name)
#> DF$Name
#[1] Exp1 Exp2_1 Exp3_1 Exp2_1 Exp1 Exp3_1 Dat1 Dat2_1 Dat3_1
#Levels: Dat1 Dat2_1 Dat3_1 Exp1 Exp2_1 Exp3_1
#change the levels like the way you want
levels(DF$Name) <- list(C_1 = "Exp1", C_2 = "Exp2_1",
C_3 = "Exp3_1", T_1 = "Dat1", T_2 = "Dat2_1", T_3 = "Dat3_1")
值会自动更改:
#> DF$Name
#[1] C_1 C_2 C_3 C_2 C_1 C_3 T_1 T_2 T_3
#Levels: C_1 C_2 C_3 T_1 T_2 T_3
#> DF
# Name
#1 C_1
#2 C_2
#3 C_3
#4 C_2
#5 C_1
#6 C_3
#7 T_1
#8 T_2
#9 T_3