如何最好地解开它并生成一个长数据字符串

发布于 2025-01-14 01:02:02 字数 993 浏览 1 评论 0原文

以下是示例数据和所需的结果。是的,我知道有四列未在所需的列中列出。只是为了保持简单。想想如果我能创建前四个,那么接下来的四个也不错。关于如何实现这一目标有什么想法吗?我的第一次尝试是使用pivot_wider,但很难获取要创建的年份和月份的列名称。

 state <- c(32,32,32,32,32,32,32,32)
 indcode <-c(44,44,44,44,45,45,45,45)
 area <-c("000000","000000","000000","000000","000000","000000","000000","000000")
 areatype <-c("01","01","01","01","01","01","01","01")
 ownership <-c("00","00","00","00","00","00","00","00")
 periodyear  <-c(2018,2019,2020,2021,2018,2019,2020,2021)
 January <- c(44,90,45,91,46,92,48,96)
 February <- c(44,91,46,91,48,92,49,99)


 example <- data.frame(state,indcode,area,areatype,ownership,periodyear,January,February)


 state      indcode      area    areatype     ownership     2018m1     2018m2   2019m1    2019 m2    
 32            44       000000      01            00           44         44       90         91          
 32            45       000000      01            00           46         48       92        92     

Below is the sample data and the desired result. Yes, I know that there are four columns not listed in the desired out. It is just to keep it simple. Figuring if I can get the first four created then the next four are not that bad. Any ideas on how to accomplish this? My first attempts have been to use pivot_wider but struggling to get the column names that have year and month to create.

 state <- c(32,32,32,32,32,32,32,32)
 indcode <-c(44,44,44,44,45,45,45,45)
 area <-c("000000","000000","000000","000000","000000","000000","000000","000000")
 areatype <-c("01","01","01","01","01","01","01","01")
 ownership <-c("00","00","00","00","00","00","00","00")
 periodyear  <-c(2018,2019,2020,2021,2018,2019,2020,2021)
 January <- c(44,90,45,91,46,92,48,96)
 February <- c(44,91,46,91,48,92,49,99)


 example <- data.frame(state,indcode,area,areatype,ownership,periodyear,January,February)


 state      indcode      area    areatype     ownership     2018m1     2018m2   2019m1    2019 m2    
 32            44       000000      01            00           44         44       90         91          
 32            45       000000      01            00           46         48       92        92     

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蹲墙角沉默 2025-01-21 01:02:02
library(tidyverse)
example %>%
  pivot_longer(January:February, names_to = "month") %>%
  mutate(mo_num = match(month, month.name)) %>%
  mutate(col_name = paste(periodyear, mo_num, sep = "m")) %>%
  select(-periodyear, -month, -mo_num) %>%
  pivot_wider(names_from = col_name, values_from = value)

结果

# A tibble: 2 x 13
  state indcode area   areatype ownership `2018m1` `2018m2` `2019m1` `2019m2` `2020m1` `2020m2` `2021m1` `2021m2`
  <dbl>   <dbl> <chr>  <chr>    <chr>        <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
1    32      44 000000 01       00              44       44       90       91       45       46       91       91
2    32      45 000000 01       00              46       48       92       92       48       49       96       99
library(tidyverse)
example %>%
  pivot_longer(January:February, names_to = "month") %>%
  mutate(mo_num = match(month, month.name)) %>%
  mutate(col_name = paste(periodyear, mo_num, sep = "m")) %>%
  select(-periodyear, -month, -mo_num) %>%
  pivot_wider(names_from = col_name, values_from = value)

Result

# A tibble: 2 x 13
  state indcode area   areatype ownership `2018m1` `2018m2` `2019m1` `2019m2` `2020m1` `2020m2` `2021m1` `2021m2`
  <dbl>   <dbl> <chr>  <chr>    <chr>        <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
1    32      44 000000 01       00              44       44       90       91       45       46       91       91
2    32      45 000000 01       00              46       48       92       92       48       49       96       99
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文