1

我有一个数据框,例如:

df <- data.frame(
        type = c("BND", "INV", "BND", "DEL", "TRA"),
        chrom1 = c(1, 1, 1, 1, 1),
        chrom2 = c(1, 1, 2, 1, 3)
        )

我想将所有df[df$type=='BND',]实例重新分配给INVTRA取决于 和 中的chrom1chrom2

我正在尝试fct_recodeforcats包中使用:

library(forcats)

df$type <- ifelse(df$type=="BND", 
                  ifelse(df$chrom1 == df$chrom2,
                         fct_recode(df$type, BND="INV"),
                         fct_recode(df$type, BND="TRA")),
                  df$type)

但是,这将我的因素重新编码为数字:

  type chrom1 chrom2
1    1      1      1
2    3      1      1
3    1      1      2
4    2      1      1
5    4      1      3

这是我的预期结果:

  type chrom1 chrom2
1    INV      1      1 # BND -> INV as chrom1==chrom2
2    INV      1      1
3    TRA      1      2 # BND -> TRA as chrom1!=chrom2
4    DEL      1      1
5    TRA      1      3

如何以这种方式将一个因素分成两个级别?

4

4 回答 4

2

你也可以这样做case_when()

library(tidyverse)

df %>% 
  mutate(type = as.factor(case_when(
    type == 'BND' & chrom1 == chrom2 ~ 'INV', 
    type == 'BND' & chrom1 != chrom2 ~ 'TRA',
    TRUE  ~ as.character(type))))

数据:

df <- data.frame(
  type = c("BND", "INV", "BND", "DEL", "TRA"),
  chrom1 = c(1, 1, 1, 1, 1),
  chrom2 = c(1, 1, 2, 1, 3)
)
于 2018-06-05T14:11:48.283 回答
1

为了完整起见,这里还有一个简洁的data.table解决方案:

library(data.table)
setDT(df)[type == "BND" & chrom1 == chrom2, type := "INV"][type == "BND", type := "TRA"][]
   type chrom1 chrom2
1:  INV      1      1
2:  INV      1      1
3:  TRA      1      2
4:  DEL      1      1
5:  TRA      1      3

好处是通过引用type更新,例如,不复制整个对象,并且仅适用于条件适用的那些行。

于 2018-06-05T15:03:22.687 回答
1

我的想法如下:(1)索引你想改变的行,(2)你做ifelse语句。我希望这有帮助:

  df <- data.frame(
  type = c("BND", "INV", "BND", "DEL", "TRA"),
  chrom1 = c(1, 1, 1, 1, 1),
  chrom2 = c(1, 1, 2, 1, 3)
)

indexBND<-which(df$type=="BND")
df$type[indexBND]<-ifelse(df$chrom1[indexBND] == df$chrom2[indexBND], df$type[indexBND] <- "INV", "TRA")

df
#   type chrom1 chrom2
# 1  INV      1      1
# 2  INV      1      1
# 3  TRA      1      2
# 4  DEL      1      1
# 5  TRA      1      3

干杯!

于 2018-06-05T14:10:49.613 回答
0

要不就

df$type[df$type == "BND"] <- with(df, 
                                  ifelse(df[type == "BND", ]$chrom1 == 
                                           df[type == "BND", ]$chrom2,
                                         "INV", "TRA"))
> df
  type chrom1 chrom2
1  INV      1      1
2  INV      1      1
3  TRA      1      2
4  DEL      1      1
5  TRA      1      3
于 2018-06-05T14:41:44.500 回答