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

时间:2016-12-14 10:22:34

标签: r

我想在我的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

2 个答案:

答案 0 :(得分:2)

一个选项是从行名称中删除数字(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},case_when'ID'创建“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

答案 1 :(得分:0)

您可以将ifelse()grepl()结合使用,以通用方式创建ID列。

idCol <- rownames(df)
idCol <- ifelse(grepl("a", idCol), "Ctr", 
                ifelse(grepl("b", idCol), "P", "M"))
idCol
# [1] "Ctr" "Ctr" "Ctr" "P"   "P"   "P"   "M"   "M"   "M"  

df$ID <- as.factor(idCol)
df
#    Col1 Col2 Col3  ID
# a1    1    2    3 Ctr
# a2    2    3    4 Ctr
# a3    3    4    5 Ctr
# b1    4    5    6   P
# b2    5    6    7   P
# b3    6    7    8   P
# c1    7    8    9   M
# c2    8    9   10   M
# c3    9   10   11   M