如何在R中多次执行PIVOT_WIDER?
以下是示例数据和所需结果。手头的任务是多次旋转(或者至少我认为是多次),以创建每个行的完整历史系列。目前,每个QTR(四分之一)是一排。命名惯例并不那么重要。我知道如何做一个pivot_wider,但是每月列的名称的创建使我感到困惑。
area <- c("000000","000000","000000","000000","000003","000003","000003","000003")
indcode <- c("432100","432100","432100","432100","432100","432100","432100","432100")
Year <- c("2019","2019","2019","2019","2019","2019","2019","2019")
qtr <- c("01","02","03","04","01","02","03","04")
month1_emplvl <-c(100,101,102,103,44,44,46,52)
month2_emplvl <-c(100,101,103,104,48,44,52,41)
month3_emplvl <-c(101,100,102,99,44,45,46,47)
testdata <- data.frame(area,indcode,Year,qtr,month1_emplvl,month2_emplvl,month3_emplvl)
期望的结果
area indcode 2019-01 2019-02 2019-03 2019-04 2019-05 2019-06 and so on...
000000 432100 100 100 101 101 101 100
000003 432100 44 48 44 44 44 45
Below is the sample data and the desired result. The task at hand is to pivot multiple times(or at least I think it is multiple times) in order to create a complete historical series per row. At the moment, each qtr (quarter) is a row. The naming convention is not all that important. I know how to do a pivot_wider but the creating of the monthly column names has me stumped.
area <- c("000000","000000","000000","000000","000003","000003","000003","000003")
indcode <- c("432100","432100","432100","432100","432100","432100","432100","432100")
Year <- c("2019","2019","2019","2019","2019","2019","2019","2019")
qtr <- c("01","02","03","04","01","02","03","04")
month1_emplvl <-c(100,101,102,103,44,44,46,52)
month2_emplvl <-c(100,101,103,104,48,44,52,41)
month3_emplvl <-c(101,100,102,99,44,45,46,47)
testdata <- data.frame(area,indcode,Year,qtr,month1_emplvl,month2_emplvl,month3_emplvl)
Desired result
area indcode 2019-01 2019-02 2019-03 2019-04 2019-05 2019-06 and so on...
000000 432100 100 100 101 101 101 100
000003 432100 44 48 44 44 44 45
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您要寻找的解决方案(将所有几个月传递给
values_from
参数)吗?Is this the solution you're looking for (pass all the months to the
values_from
argument)?我不知道您要多次使用枢轴,但我认为您可以使用
此回报
I don't know how you want to apply pivoting multiple times, but I think you could use
This returns