select() 関数の中で、列名を指定する時に便利な関数があります。 starts_with() と ends_with() です。
オブジェクト %>% select(starts_with("文字列"))
サンプルが数十個あるような時に、select() 関数に全て書くのも大変です。そのような時は、列名に Sample1, Sample2 や WT1, WT2 のように、共通の文字で始まる名前を付けておくと良いです。そうすれば、 starts_with("Sample")
や starts_with("WT")
のように関数を使ってまとめて指定できます。
一方、 WT_1h, KO_1h や WT_12h, KO_12h のように、末尾に共通な文字で指定したい場合は、 ends_with() が使えます。 ends_with("_1h")
や ends_with("_12h")
のように指定します。
select() 関数の中で指定することを忘れないようにしましょう。
samples <- result_data %>% select(starts_with("Sample"))
> result_data
# A tibble: 3 x 5
Id Symbol Description Sample1 Sample2
<chr> <chr> <chr> <dbl> <dbl>
1 id3 CCC lorem ipsum c 3 6
2 id2 BBB lorem ipsum b 2 5
3 id1 AAA lorem ipsum a 1 4
>
>
> samples
# A tibble: 3 x 2
Sample1 Sample2
<dbl> <dbl>
1 3 6
2 2 5
3 1 4