如何最好地解开它并生成一个长数据字符串
以下是示例数据和所需的结果。是的,我知道有四列未在所需的列中列出。只是为了保持简单。想想如果我能创建前四个,那么接下来的四个也不错。关于如何实现这一目标有什么想法吗?我的第一次尝试是使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果
Result