简体   繁体   English

如何使用正则表达式将 data.frame 重塑为具有多个值列的长格式

[英]How do I reshape a data.frame to long format with multiple value cols using regex expression

How do I reshape the data.frame input to result ?如何将 data.frame input重塑为result Basically the first part of the column name before "dosis" should be the new variable with two value columns value and dosis containing the data of the columns ending with/without "dosis" .基本上, "dosis"名的第一部分应该是具有两个值列valuedosis的新变量,其中包含以/不以"dosis"结尾的列的数据。

This should not be too difficult, but I have difficulties finding the correct regex to use with pivot_longer or melt.data.table .这应该不会太难,但我很难找到与pivot_longermelt.data.table一起使用的正确正则表达式。

library(tibble)
library(tidyr)
library(magrittr)
library(data.table)

input <-
  tribble(
    ~"abc", ~"abcdosis", ~"def", ~"defdosis", ~"ghi", ~"ghidosis",
    1, 0, 9, NA, 1, 2
  )

result  <-
  tribble(
    ~"variable", ~"value", ~"dosis",
    "abc", 1, 0,
    "def", 9, NA,
    "ghi", 1, 2
  )

# Not working
pivot_longer(input, 
             everything(), 
             names_to = c("variable", "dosis"),
             names_pattern = "(^dosis)?(dosis)")

# Also not working
melt.data.table(as.data.table(input), measure.vars = patterns("^(?!.*dosis).*$", "dosis$"))

Using dplyr::rename_with() you can paste "value" onto the end of of the non- "dosis" columns, then use then use the ".value" sentinel in pivot_longer() .使用dplyr::rename_with()您可以将"value"粘贴到非"dosis"列的末尾,然后在pivot_longer()中使用".value"标记。 Of course, may have to be more specific with the columns in rename_with() if you data is more complex.当然,如果您的数据更复杂,可能必须更具体地使用rename_with()中的列。

library(dplyr)
library(tidyr)

input %>%
  rename_with(~paste0(., "value"), -ends_with("dosis")) %>%
  pivot_longer(everything(), names_to = c("variable", ".value"), names_pattern = "(.*?)(value|dosis)$")

# A tibble: 3 x 3
  variable value dosis
  <chr>    <dbl> <dbl>
1 abc          1     0
2 def          9    NA
3 ghi          1     2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM