【问题标题】:Selecting specific sequences from a column of sequences从序列列中选择特定序列
【发布时间】:2021-07-27 11:03:46
【问题描述】:

我的数据如下:

data <- as.data.frame(c("[0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;17;24;18;25;15;26;16;19]", 
                        "[0;21;16;27;15;28;8;9;10;11;12;13;14;29;1;2;3;4;5;6;7;30;18;31;17;19]", 
                        "[0;20;15;22;16;23;18;24;17;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]", 
                        "[0;20;8;9;10;11;12;13;14;22;1;2;3;4;5;6;7;23;15;24;16;25;17;26;18;19]", 
                        "[0;21;18;27;17;28;15;29;16;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19]", 
                        "[0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;15;24;16;25;17;26;18;19]", 
                        "[0;21;1;2;3;4;5;6;7;27;8;9;10;11;12;13;14;28;17;29;18;30;16;31;15;19]", 
                        "[0;20;8;9;10;11;12;13;14;22;1;2;3;4;5;6;7;23;16;24;15;25;18;26;17;19]", 
                        "[0;21;17;27;18;28;16;29;15;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19]", 
                        "[0;20;15;22;16;23;18;24;17;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19]", 
                        "[0;21;18;27;17;28;16;29;15;30;1;2;3;4;5;6;7;31;8;9;10;11;12;13;14;19]", 
                        "[0;20;1;2;3;4;5;6;7;22;8;9;10;11;12;13;14;23;18;24;17;25;16;26;15;19]", 
                        "[0;21;15;27;16;28;1;2;3;4;5;6;7;29;8;9;10;11;12;13;14;30;18;31;17;19]", 
                        "[0;21;1;2;3;4;5;6;7;27;8;9;10;11;12;13;14;28;15;29;16;30;18;31;17;19]", 
                        "[0;20;16;22;15;23;17;24;18;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]", 
                        "[0;20;18;22;17;23;15;24;16;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19]", 
                        "[0;21;15;27;16;28;8;9;10;11;12;13;14;29;1;2;3;4;5;6;7;30;18;31;17;19]", 
                        "[0;21;15;27;16;28;1;2;3;4;5;6;7;29;8;9;10;11;12;13;14;30;17;31;18;19]", 
                        "[0;21;18;27;17;28;15;29;16;30;1;2;3;4;5;6;7;31;8;9;10;11;12;13;14;19]", 
                        "[0;20;16;22;15;23;17;24;18;25;8;9;10;11;12;13;14;26;1;2;3;4;5;6;7;19]", 
                        "[0;21;8;9;10;11;12;13;14;27;1;2;3;4;5;6;7;28;17;29;18;30;16;31;15;19]", 
                        "[0;20;16;22;15;23;18;24;17;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]", 
                        "[0;21;8;9;10;11;12;13;14;27;1;2;3;4;5;6;7;28;18;29;17;30;16;31;15;19]", 
                        "[0;20;15;22;16;23;1;2;3;4;5;6;7;24;8;9;10;11;12;13;14;25;18;26;17;19]", 
                        "[0;21;16;27;15;28;18;29;17;30;8;9;10;11;12;13;14;31;1;2;3;4;5;6;7;19]", 
                        "[0;20;15;22;16;23;17;24;18;25;1;2;3;4;5;6;7;26;8;9;10;11;12;13;14;19]"
))

我想要做的是在 data.frame 中添加一个额外的列,它只是告诉我,如果第一个条件、第二个条件、以下两个条件都满足或都不满足:

  1. 17 的行在序列中比15 晚。
  2. 18 的行在序列中比16 晚。

类似:

Neither
Both
First
Second
Neither
Both
Both
Second

谁能帮帮我?

【问题讨论】:

    标签: r


    【解决方案1】:

    要添加新列,您可以在case_when 中包含条件-

    library(dplyr)
    
    data %>%
      mutate(result = case_when(grepl('15.*17', col) & grepl('16.*18', col) ~ 'Both', 
                                grepl('15.*17', col) ~ 'First', 
                                grepl('16.*18', col) ~ 'Second', 
                                TRUE ~ 'Neither'))
    

    如果你想对数据进行子集化,可以根据条件使用grepl寻找模式-

    data1 <- data[grepl('15.*17', data$col), , drop = FALSE]
    data2 <- data[grepl('16.*18', data$col), , drop = FALSE]
    

    【讨论】:

    • 非常感谢。我只是在编辑我的问题,但我已经害怕有人会回答。是否也可以添加一列,告诉我条件是否适用。如果您愿意,我也可以提出一个新问题。
    • 你是最棒的!
    • 再次感谢,我收到以下错误。知道为什么吗? Error: Problem with mutate() input result. x cannot coerce type 'closure' to vector of type 'character' i Input result is case_when(...). Run rlang::last_error() to see where the error occurred.
    • 是的..这是因为在您共享的数据中没有列名(或非常奇怪的列名)。我以col为例,将其用作data.frame(col = ......)
    猜你喜欢
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多