作者celestialgod (天)
看板R_Language
标题Re: [问题] 一笔资料转多笔
时间Thu Dec 1 19:37:17 2016
※ 引述《criky (立业成家)》之铭言:
: [问题类型]:
:
: 程式谘询(我想用R 做某件事情,但是我不知道要怎麽用R 写出来)
: [软体熟悉度]:
: 新手(没写过程式,R 是我的第一次)
: [问题叙述]:
: 若我有资料栏位如下:
:
: id start_y start_s end_y end_s
: 1 100 1 102 2
: 2 101 2 103 1
: 3 101 2 101 2
: year:
: 如何转成下面的样子:
: id year s
: 1 100 1
: 1 100 2
: 1 101 1
: 1 101 2
: 1 102 1
: 1 102 2
: 2 101 2
: 2 102 1
: 2 102 2
: 2 103 1
: 3 101 2 (只有一笔)
: 谢谢回答~
: [程式范例]:
:
:
: [环境叙述]:
:
: 请提供 sessionInfo() 的输出结果,
: 里面含有所有你使用的作业系统、R 的版本和套件版本资讯,
: 让版友更容易找出错误
:
: [关键字]:
:
: 选择性,也许未来有用
:
做两次melt就可以达到你要的了,我不确定是否可以一次,看是否有高手写得出来~~
library(data.table)
DT <- fread("id start_y start_s end_y end_s
1 100 1 102 2
2 101 2 103 1
3 101 2 101 2")
DT_melt <- melt.data.table(DT, id = c(1,2,4), value.name = "s")
DT_melt2 <- melt.data.table(DT_melt, id = c(1,5), measure = 2:3,
value.name = "year")
DT_melt2[ , variable := NULL]
setcolorder(DT_melt2, c("id", "year", "s"))
setorderv(DT_melt2, names(DT_melt2))
unique(DT_melt2, by = names(DT_melt2))
# id year s
# 1: 1 100 1
# 2: 1 100 2
# 3: 1 102 1
# 4: 1 102 2
# 5: 2 101 1
# 6: 2 101 2
# 7: 2 103 1
# 8: 2 103 2
# 9: 3 101 2
# with pipeR
library(pipeR)
resDT <- melt.data.table(DT, id = c(1,2,4), value.name = "s") %>>%
melt.data.table(id = c(1,5), measure = 2:3, value.name = "year") %>>%
`[`(j = variable := NULL) %>>% setcolorder(c("id", "year", "s")) %>>%
setorderv(names(.)) %>>% unique(by = names(.))
print(resDT)
# id year s
# 1: 1 100 1
# 2: 1 100 2
# 3: 1 102 1
# 4: 1 102 2
# 5: 2 101 1
# 6: 2 101 2
# 7: 2 103 1
# 8: 2 103 2
# 9: 3 101 2
# tidyr + dplyr解法 (data.table不需要,DT可以是data.frame)
library(dplyr)
library(tidyr)
gather(DT, value, year, -id, -start_s, -end_s) %>>%
gather(ss, s, -id, -value, -year) %>>% select(id, year, s) %>>%
arrange(id, year, s) %>>% distinct(id, year, s)
# id year s
# 1 1 100 1
# 2 1 100 2
# 3 1 102 1
# 4 1 102 2
# 5 2 101 1
# 6 2 101 2
# 7 2 103 1
# 8 2 103 2
# 9 3 101 2
--
R资料整理套件系列文:
magrittr #1LhSWhpH (R_Language) https://goo.gl/72l1m9
data.table #1LhW7Tvj (R_Language) https://goo.gl/PZa6Ue
dplyr(上.下) #1LhpJCfB,#1Lhw8b-s (R_Language) https://goo.gl/I5xX9b
tidyr #1Liqls1R (R_Language) https://goo.gl/i7yzAz
pipeR #1NXESRm5 (R_Language) https://goo.gl/zRUISx
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.232.188.79
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1480592241.A.CFC.html
1F:推 cywhale: #1N_NLOIo data.table::foverlaps可做但程式可能不会较少 12/01 21:57
感觉没有太大差异XD
※ 编辑: celestialgod (36.232.188.79), 12/01/2016 22:04:44
2F:推 cywhale: ya..so.. and maybe melt is faster ^_^ 12/01 22:20
3F:推 criky: 感谢板主用多种方法解答,不过id=1和2都缺了中间几笔资料 12/01 22:42
4F:→ criky: start_y到end_y中间的资料,id=1是101,id=2是102 12/01 22:44
5F:→ criky: pipe R那篇的文章连结变404了 想学一下 >> 的用法 QQ 12/01 22:50
id=1,2缺几笔? 你如果可以回文给我更详细的例子,我会很乐意为你解答
Hi 我点我签名档的文章 进得去耶QQ
这里再给一次网址:
http://chingchuan-chen.github.io/posts/2016/07/10/pipe-operators-in-R
补充:那篇文章的连结已经更新了
我之前部落格翻修不再用旧有的格式了,抱歉
6F:推 criky: id=1少了y=101,s=1,2 这2笔,id=2少了y=102,s=1,2这2笔 12/01 23:10
7F:→ criky: 然後id=2多了y=101,s=1 , y=103,s=2 这2笔 12/01 23:11
8F:→ criky: 文章可以进去了 谢谢! 12/01 23:11
9F:→ criky: y=学年 s=1#上学期 s=2#下学期 12/01 23:12
阿阿 我知道你在说什麽了 我只做了转表而已
我没有注意这一点,抱歉,是我的失误
我新发一篇回答了
※ 编辑: celestialgod (36.232.188.79), 12/02/2016 00:03:26