简体   繁体   English

pivot_longer 命令的问题

[英]Issue with pivot_longer command

I am trying to convert a long dataset to a wide set set using pivot longer, the column headers are "Program ID" and 'Participant_Count_22', 'Participant_Count_21', 'Participant_Count_20', 'Participant_Count_19' for four years 2019-2022.我正在尝试使用更长的 pivot 将长数据集转换为宽集,列标题为“Program ID”和“Participant_Count_22”、“Participant_Count_21”、“Participant_Count_20”、“Participant_Count_19”,为期四年 2019-2022。

   program_tot %>% pivot_longer(cols=c(‘Participant_Count_22’, ‘Participant_Count_21’, ‘Participant_Count_20’, ‘Participant_Count_19’), 
               names_to='year',
               values_to='Participant_Count')

I am getting an unexpected input error message.我收到意外的输入错误消息。 Any tips?有小费吗?

You seem to have fancy single quotes around your column names.您的列名周围似乎有花哨的单引号。 Note carefully the difference between quotes in 'Participant_Count_22' , which R doesn't recognise as a string, and 'Participant_Count_22' , which it does recognise as a string.仔细注意'Participant_Count_22'中引号之间的区别,R将其识别为字符串,而'Participant_Count_22'将其识别为字符串。

In any case, you will save yourself some time and complexity if you use the selection helpers to select columns.在任何情况下,如果您使用 select 列的选择助手,您将节省一些时间和复杂性。 In your case you could use contains("Participant_Count")在您的情况下,您可以使用contains("Participant_Count")

library(tidyverse)

program_tot %>% 
  pivot_longer(cols = contains("Participant_Count"),
               names_to = 'year',
               values_to = 'Participant_Count') %>%
  mutate(year = as.numeric(paste0("20", sub("Participant_Count_", "", year))))
#> # A tibble: 20 x 3
#>    Program.ID  year Participant_Count
#>    <fct>      <dbl>             <dbl>
#>  1 1           2022                 5
#>  2 1           2021                 5
#>  3 1           2020                 6
#>  4 1           2019                 2
#>  5 2           2022                 7
#>  6 2           2021                 2
#>  7 2           2020                 8
#>  8 2           2019                 5
#>  9 3           2022                 4
#> 10 3           2021                 8
#> 11 3           2020                 1
#> 12 3           2019                 7
#> 13 4           2022                 3
#> 14 4           2021                 1
#> 15 4           2020                 3
#> 16 4           2019                 3
#> 17 5           2022                 5
#> 18 5           2021                 3
#> 19 5           2020                 2
#> 20 5           2019                 1

Data used使用的数据

program_tot <- data.frame('Program ID' = factor(1:5),
                           Participant_Count_22 = c(5, 7, 4, 3, 5), 
                           Participant_Count_21 = c(5, 2, 8, 1, 3), 
                           Participant_Count_20 = c(6, 8, 1, 3, 2), 
                           Participant_Count_19 = c(2, 5, 7, 3, 1))

Created on 2022-09-28 with reprex v2.0.2创建于 2022-09-28,使用reprex v2.0.2

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

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