【问题标题】:Split a data-frame based in ordered multi factorial column基于有序多因子列拆分数据框
【发布时间】:2016-10-31 16:15:09
【问题描述】:

我想在数据框列表中拆分数据框。拆分它的原因是我们将始终有father 后面跟着mother,而后面跟着offspring。但是,这些家庭成员可能有不止一行(它们总是在后面。例如,father 数字 1 在第 1 行和第 2 行)。在下面的示例中,我有两个家庭,然后我试图获取一个包含两个数据框的列表。

我的意见:

df <- 'Chr  Start   End Family
1   187546286   187552094   father
3   108028534   108032021   father
1   4864403 4878685 mother
1   18898657    18904908    mother
2   460238  461771  offspring
3   108028534   108032021   offspring
1   71481449    71532983    father
2   74507242    74511395    father
2   181864092   181864690   mother
1   71481449    71532983    offspring
2   181864092   181864690   offspring
3   160057791   160113642   offspring'

df <- read.table(text=df, header=T)

因此,我的预期输出 dfout[[1]] 将如下所示:

dfout <- 'Chr   Start   End Family
1   187546286   187552094   father
3   108028534   108032021   father
1   4864403 4878685 mother
1   18898657    18904908    mother
2   460238  461771  offspring
3   108028534   108032021   offspring'

dfout - read.table(text=dfout, header=TRUE)

【问题讨论】:

  • 我不明白当您从一个家庭转到数据框各行的下一个家庭时,您有什么逻辑。

标签: r split subsequence


【解决方案1】:

要将每个族拆分为单独的数据框,您需要一个索引来指示一个族的结束位置和另一个族的开始位置。对于索引,我使用“父亲”作为更改点。但我们不能简单地使用indx &lt;- df$Family == "father",因为一行中可能有多个“父亲”条目。相反,我们通过搜索等于 1 的位置来测试从“后代”切换到“父亲”的位置。

indx <- cumsum(c(1L, diff(df$Family == "father")) == 1L)
split(df, indx)
# $`1`
#   Chr     Start       End    Family
# 1   1 187546286 187552094    father
# 2   3 108028534 108032021    father
# 3   1   4864403   4878685    mother
# 4   1  18898657  18904908    mother
# 5   2    460238    461771 offspring
# 6   3 108028534 108032021 offspring
# 
# $`2`
#    Chr     Start       End    Family
# 7    1  71481449  71532983    father
# 8    2  74507242  74511395    father
# 9    2 181864092 181864690    mother
# 10   1  71481449  71532983 offspring
# 11   2 181864092 181864690 offspring
# 12   3 160057791 160113642 offspring

【讨论】:

    【解决方案2】:

    如果您发布用于生成实际数据框的代码会更有帮助。我没有时间重做所有事情,但我会向您展示它是如何工作的,一般来说。

    gender <- c("M","M","F","F","F","F","M","M","M","M","F","F")
    values <- c(20,22,24,19,9,17,18,22,12,14,7,8)
    fruit <- c("apple","pear","mango","mango","mango","apple","banana","banana","banana","mango","apple","apple")
    df <- data.frame(gender, values, fruit)
    
    
    > df
       gender values  fruit
    1       M     20  apple
    2       M     22   pear
    3       F     24  mango
    4       F     19  mango
    5       F      9  mango
    6       F     17  apple
    7       M     18 banana
    8       M     22 banana
    9       M     12 banana
    10      M     14  mango
    11      F      7  apple
    12      F      8  apple
    
    split(df, df$gender)
    
    $F
       gender values fruit
    3       F     24 mango
    4       F     19 mango
    5       F      9 mango
    6       F     17 apple
    11      F      7 apple
    12      F      8 apple
    
    $M
       gender values  fruit
    1       M     20  apple
    2       M     22   pear
    7       M     18 banana
    8       M     22 banana
    9       M     12 banana
    10      M     14  mango
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2023-02-16
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多