如何重塑R中的数据,将顶部日期行应用为列,并按id分组
嗨,我有这样的数据如何重塑R中的数据,将顶部日期行应用为列,并按id分组,r,lapply,reshape,reshape2,melt,R,Lapply,Reshape,Reshape2,Melt,嗨,我有这样的数据 date 2020/06/10 2020/06/10 2020/06/10 2020/06/11 2020/06/11 2020/06/11 id x y z x y z 10432 0 0 0 0 0 0
date 2020/06/10 2020/06/10 2020/06/10 2020/06/11 2020/06/11 2020/06/11
id x y z x y z
10432 0 0 0 0 0 0
10668 0 0 0 0 0 0
11088 0 0 0 0 0 0
id date x y z
10432 2020/06/10 0 0 0
10432 2020/06/11 0 0 0
10668 2020/06/10 0 0 0
10668 2020/06/11 0 0 0
11088 2020/06/10 0 0 0
11088 2020/06/11 0 0 0
我希望我的输出像这样
date 2020/06/10 2020/06/10 2020/06/10 2020/06/11 2020/06/11 2020/06/11
id x y z x y z
10432 0 0 0 0 0 0
10668 0 0 0 0 0 0
11088 0 0 0 0 0 0
id date x y z
10432 2020/06/10 0 0 0
10432 2020/06/11 0 0 0
10668 2020/06/10 0 0 0
10668 2020/06/11 0 0 0
11088 2020/06/10 0 0 0
11088 2020/06/11 0 0 0
我想得到R中的输出这个数据非常混乱,但是通过一些工作我已经设法得到了你想要的输出 这就是我使用readr包中的read_table2导入您提供的示例数据后的样子: 我们现在不再需要第一行,因为它的内容在列名中重复
df <- df[-1,]
这回答了你的问题吗?你能补充一下已经失败的吗?谢谢伙计!!我可以一直跑到放下第一行,我被困在这里df%>%pivot\u longer-id,names\u to=c.value,date,names\u pattern=。\u。。我有10个ID,我有xy,xy,ad,xyz,而不是x,y,z。一定要帮我解决这个问题。我不太擅长正则表达式,但如果它有效,请尝试命名\u pattern=[a-z]{1,6}\u。。。请使用dputheaddf提供一些示例数据,以便我和其他人可以看到您在RSome有用的正则表达式信息中的列名是如何精确的:非常感谢,正则表达式工作了,我对日期有疑问导入文件时,日期被转换为字符,当我最终尝试使用as.Dateas.numericdf$date,origin=1970-01-01转换为日期格式时,我得到了相同日期的所有值,类似于2090-06-12及其重复的整行,你知道它重复显示错误的日期吗?你应该把它作为一个新问题发布,但我相信已经有答案了。考虑使用LuBrutt软件包
df <- df[-1,]
> df
# A tibble: 3 x 7
id `x_2020/06/10` `y_2020/06/10` `z_2020/06/10` `x_2020/06/11` `y_2020/06/11` `z_2020/06/11`
<chr> <chr> <chr> <chr> <chr> <chr> <chr>
1 10432 0 0 0 0 0 0
2 10668 0 0 0 0 0 0
3 11088 0 0 0 0 0 0
library(tidyr) # 1.1.0
df %>% pivot_longer(-id, names_to = c(".value", "date"),
names_pattern = "(.)_(..../../..)")
# A tibble: 6 x 5
id date x y z
<chr> <chr> <chr> <chr> <chr>
1 10432 2020/06/10 0 0 0
2 10432 2020/06/11 0 0 0
3 10668 2020/06/10 0 0 0
4 10668 2020/06/11 0 0 0
5 11088 2020/06/10 0 0 0
6 11088 2020/06/11 0 0 0
structure(list(id = c("10432", "10668", "11088"), `x_2020/06/10` = c("0",
"0", "0"), `y_2020/06/10` = c("0", "0", "0"), `z_2020/06/10` = c("0",
"0", "0"), `x_2020/06/11` = c("0", "0", "0"), `y_2020/06/11` = c("0",
"0", "0"), `z_2020/06/11` = c("0", "0", "0")), row.names = c(NA,
-3L), class = c("tbl_df", "tbl", "data.frame"))