R语言 使用列名模式更长时间的棘手透视[重复]

x0fgdtte  于 2023-02-27  发布在  其他
关注(0)|答案(2)|浏览(130)
    • 此问题在此处已有答案**:

How to pivot_longer two groups of columns using regular expression [duplicate](1个答案)
Pivot longer using Tidyr - Multiple variables stored in column names(2个答案)
去年关闭了。
我有这样一个 Dataframe :

data.frame(id = c(1,2,3),
           first_value=c("A","B","NA"),second_value=c("A","NA","D"), 
           first_date=c("2001",2010,2003),second_date=c("2003",2014,"2007"))
id first_value second_values first_date second_date
1  1           A             A       2001        2003
2  2           B            NA       2010        2014
3  3          NA             D       2003        2007

我希望将其转换为更长的 Dataframe ,如以下示例列表所示:

id timing value date
1  1  first     A 2001
2  1 second     A 2003
3  2  first     B 2010
4  2 second    NA 2014
5  3  first    NA 2003
6  3 second     D 2007

我没有成功使用tidyr pivot_longger

k5ifujac

k5ifujac1#

您可以:

library(tidyverse)
df %>%
  pivot_longer(cols = -id,
               names_pattern = '(.*)_(.*)',
               names_to = c('timing', '.value'))

其中:

# A tibble: 6 x 4
     id timing value date 
  <dbl> <chr>  <chr> <chr>
1     1 first  A     2001 
2     1 second A     2003 
3     2 first  B     2010 
4     2 second NA    2014 
5     3 first  NA    2003 
6     3 second D     2007

注意:只有当你把second_values列重命名为second_value时,这才有效。我想"values"只是一个打字错误吧?
根据@Maël的建议,选择更简单一点:

df %>%
  pivot_longer(cols = -id,
               names_sep = '_',
               names_to = c('timing', '.value'))
zz2j4svz

zz2j4svz2#

可以尝试这样的事情吗?

library(tidyverse)

df <- data.frame(id = c(1,2,3),
              first_value=c("A","B","NA"),
              second_values=c("A","NA","D"), 
              first_date=c("2001",2010,2003),
              second_date=c("2003",2014,"2007"))

bind_rows(
  df %>% 
    select(id, first_value, date = first_date) %>% 
    pivot_longer(cols = "first_value", names_to = "timing"), 
  df %>% 
    select(id, second_values, date = second_date) %>% 
    pivot_longer(cols = "second_values", names_to = "timing")
  ) %>% 
  relocate(id, timing, value, date) %>% 
  arrange(id)

最后两行只是为了获得与您发布的格式/顺序相同的格式/顺序,所以可能会被省略。

相关问题