gpt4 book ai didi

r - 在更改项目标签和级别时添加列

转载 作者:行者123 更新时间:2023-12-04 12:11:23 26 4
gpt4 key购买 nike

我想在我的 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 )中删除数字,使用 uniques 创建一个命名向量'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
factor(sub("\\d+", "", rownames(df)), labels = c("Ctr", "P", "M"))
#[1] Ctr Ctr Ctr P P P M M M

或者另一个选项是 dplyr/tibble .我们从行名( rownames_to_column 来自 tibble )、 mutate 创建“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

关于r - 在更改项目标签和级别时添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41140139/

26 4 0