【发布时间】:2020-11-10 01:50:31
【问题描述】:
我在尝试从下面的dat 制作长格式数据方面已经完成了一半。
两个小问题:在我下面的pivot_longer() 代码中,
(1)如何为times 的数量添加id 变量,如我的预期输出所示?
(2)如何将列time1, . . .,time8 转为0, . . ., 7?
dat <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/wi.csv')
# Top 3 rows of current data:
time1 time2 time3 time4 time5 time6 time7 time8 ses
1 1.203999 2.278898 3.716495 3.550721 3.375575 4.029231 5.292819 4.117426 -0.428465
2 0.291965 1.882300 0.958540 0.793806 0.021239 1.709134 3.127197 1.713560 -0.831093
3 -0.634382 0.847460 -0.801319 -0.126182 -0.496423 -1.009533 1.067997 0.131556 0.936131
# Top 3 rows of EXPECTED output:
id ses Reading time
1 1 -0.428465 1.203999 0
2 1 -0.428465 2.278898 1
3 1 -0.428465 3.716495 2
# What I tried:-----------------------------------------------------------------
pivot_longer(dat, time1:time8, names_to = "time", values_to = "Reading")
ses time Reading
1 -0.428465 time1 1.203999
2 -0.428465 time2 2.278898
3 -0.428465 time3 3.716495
【问题讨论】:
标签: r dataframe dplyr tidyverse