我想在我的df中添加一列,同时我想更改标签和级别,我试过这个:
df <- data.frame(Col1 = c(1:9), Col2 = c(2:10), Col3 = c(3:11))
rownames(df)<-c("a1","a2", "a3", "b1","b2", "b3", "c1","c2", "c3")
df<-cbind(ID = factor(rownames(df),labels=c("a1"="Ctr","a2"="Ctr","a3"="Ctr",
"b1"="P","b2"="P","b3"="P",
"c1"="M","c2"="M","c3"="M"),
levels=c("Ctr", "P", "M")), df)
输出应该是:
ID Col1 Col2 Col3
a1 Ctr 1 2 3
a2 Ctr 2 3 4
a3 Ctr 3 4 5
b1 P 4 5 6
b2 P 5 6 7
b3 P 6 7 8
c1 M 7 8 9
c2 M 8 9 10
c3 M 9 10 11
最佳答案 一个选项是从行名称中删除数字(v1),创建具有唯一值’v1’的命名向量,使用它来匹配’v1’中的值并创建新列’ID’
v1 <- sub("\\d+", "", rownames(df))
transform(df, ID= setNames(c("Ctr", "P", "M"), unique(v1))[v1])[c(4, 1:3)]
# ID Col1 Col2 Col3
#a1 Ctr 1 2 3
#a2 Ctr 2 3 4
#a3 Ctr 3 4 5
#b1 P 4 5 6
#b2 P 5 6 7
#b3 P 6 7 8
#c1 M 7 8 9
#c2 M 8 9 10
#c3 M 9 10 11
或者其他选择是因素
factor(sub("\\d+", "", rownames(df)), labels = c("Ctr", "P", "M"))
#[1] Ctr Ctr Ctr P P P M M M
或者另一种选择是dplyr / tibble.我们从行名称(来自tibble的rownames_to_column)创建“ID”列,使用case_when将“ID”变为新值.
library(dplyr)
library(tibble)
rownames_to_column(df, var="ID") %>%
mutate(ID = case_when(.$ID %in% c("a1", "a2", "a3") ~ "Ctr",
.$ID %in% c("b1", "b2", "b3") ~ "P",
TRUE ~ "M"))
# ID Col1 Col2 Col3
#1 Ctr 1 2 3
#2 Ctr 2 3 4
#3 Ctr 3 4 5
#4 P 4 5 6
#5 P 5 6 7
#6 P 6 7 8
#7 M 7 8 9
#8 M 8 9 10
#9 M 9 10 11