在 pivot_longer 的 names_pattern 参数中为多个变量编码正则表达式,这些变量通过存在或不存在前缀来区分

Coding a regexp in the names_pattern argument of pivot_longer for multiple variables differentiated by presence or absence of a prefix

我有一个需要 pivot_longer() 的数据集,其列中有两种变量:数量和比例。数量在物种名称下输入,比例在 P 下输入,然后是物种名称。每行代表一个小时采样周期的数据。

这是我正在处理的数据的简化版本。 (编辑:我在代码中添加了另一列,RH_percent,以帮助完善正则表达式。)

#code to recreate input data
sampledata <- read_csv("date,time,RH_percent,Cx_tarsalis,Ps_columb,PCx_tarsalis,PPs_columb
2020-07-20,19:00:00,0.25,3,4,0.03,0.04
2020-07-20,20:00:00,0.5,6,8,0.06,0.08
2020-07-20,21:00:00,0.75,9,12,0.09,0.12")

这就是我希望输出的样子:

#code to recreate desired output data
sampleoutput <- read_csv("date,time,RH_percent,species,quantity,P
         2020-07-20,19:00:00,0.25,Cx_tarsalis,3,0.03
         2020-07-20,19:00:00,0.25,Ps_columb,4,0.04
         2020-07-20,20:00:00,0.5,Cx_tarsalis,6,0.06
         2020-07-20,20:00:00,0.5,Ps_columb,8,0.08
         2020-07-20,21:00:00,0.75,Cx_tarsalis,9,0.09
         2020-07-20,21:00:00,0.75,Ps_columb,12,0.12")

我知道代码看起来像这样,我知道我需要在 names_pattern 参数中指定一个正则表达式:

sampledata %>% pivot_longer(cols = -c(date,time),
                            names_to = c(".value","species"),
                            names_pattern = "")

我一直在研究在线示例,包括 Pivoting vignette and Roger Peng's regexp videos on youtube,但没有找到正确的 names_pattern 来获得我需要的输出。

解决了类似的问题:,但是变量在列中的表示方式以及正则表达式中的模式与我需要的有很大不同。谢谢!

这是 renameing

的一种方法
  1. 通过添加后缀重命名第 3 列和第 4 列 -quantity
  2. 通过添加后缀 -P
  3. 重命名第 5 列和第 6 列
  4. names_sep用作-并在species中指定names_to,在pivot_longer[=49=中指定.value ]
library(dplyr)
library(stringr)
library(tidyr)
sampledata %>%
    rename_with(~ str_c(., '-quantity'), 3:4) %>%
    rename_with(~ str_c(str_remove(., '^P'), '-P'), 5:6) %>%    
   pivot_longer(cols = -c(date,time),
                            names_to = c("species", ".value"),
                            names_sep = "-")

-输出

# A tibble: 6 x 5
  date       time   species     quantity     P
  <date>     <time> <chr>          <dbl> <dbl>
1 2020-07-20 19:00  Cx_tarsalis        3  0.03
2 2020-07-20 19:00  Ps_columb          4  0.04
3 2020-07-20 20:00  Cx_tarsalis        6  0.06
4 2020-07-20 20:00  Ps_columb          8  0.08
5 2020-07-20 21:00  Cx_tarsalis        9  0.09
6 2020-07-20 21:00  Ps_columb         12  0.12

或者另一种方法是添加一个前缀,其中列名在 _ 之前只有两个字符,即那些是数量列 'Q'。然后,在 names_pattern 中捕获第一个字符 ((.)) 作为第一个捕获组,然后将其余字符 ((.*)) 作为第二个捕获组,这将表示为“.value”和 names_to

中指定的“物种”
sampledata %>%
     rename_with(~ str_c('Q', .), matches('^.._')) %>% 
     pivot_longer(cols = -c(date, time), 
        names_to = c(".value", "species"), names_pattern = "^(.)(.*)") %>% 
     rename(quantity = Q)
# A tibble: 6 x 5
  date       time   species     quantity     P
  <date>     <time> <chr>          <dbl> <dbl>
1 2020-07-20 19:00  Cx_tarsalis        3  0.03
2 2020-07-20 19:00  Ps_columb          4  0.04
3 2020-07-20 20:00  Cx_tarsalis        6  0.06
4 2020-07-20 20:00  Ps_columb          8  0.08
5 2020-07-20 21:00  Cx_tarsalis        9  0.09
6 2020-07-20 21:00  Ps_columb         12  0.12

更新

使用 OP 的新数据集,在 matches 中使用 ignore.case = FALSE,因为默认情况下它是 TRUE

sampledata %>%
     rename_with(~ str_c('Q', .), matches('^[A-Z][a-z]_[a-z]', 
       ignore.case = FALSE)) %>% 
     pivot_longer(cols = -c(date, time, RH_percent), 
        names_to = c(".value", "species"), names_pattern = "^(.)(.*)") %>% 
     rename(quantity = Q)

-输出

# A tibble: 6 x 6
  date       time   RH_percent species     quantity     P
  <date>     <time>      <dbl> <chr>          <dbl> <dbl>
1 2020-07-20 19:00        0.25 Cx_tarsalis        3  0.03
2 2020-07-20 19:00        0.25 Ps_columb          4  0.04
3 2020-07-20 20:00        0.5  Cx_tarsalis        6  0.06
4 2020-07-20 20:00        0.5  Ps_columb          8  0.08
5 2020-07-20 21:00        0.75 Cx_tarsalis        9  0.09
6 2020-07-20 21:00        0.75 Ps_columb         12  0.12