다음과 같은 data.frame이 있습니다.날짜 조합 날짜 열
toolid startdate enddate stage
abc 1-Jan-13 5-Jan-13 production
abc 6-Jan-13 10-Jan-13 down
xyz 3-Jan-13 8-Jan-13 production
xyz 9-Jan-13 15-Jan-13 down
아래 형식으로 data.frame을 변환하려고합니다. 나는 'startdate'
과 'enddate'
위의 data.frame 열을 아래의 'date'
이라는 단일 열에 결합하려고합니다. 내가 가지고있는 원본 데이터는 많은 수의 행을 통해 많은 toolids
및 여러 단계에 걸쳐 있습니다. 이미 SQL을 사용하여이 작업을 수행하는 방법을 찾았지만 R 솔루션을 선호합니다. 나는 아래의 코드와 같이 데이터를 녹여서 시작했다.
toolid date stage
abc 1-Jan-13 production
abc 2-Jan-13 production
abc 3-Jan-13 production
abc 4-Jan-13 production
abc 5-Jan-13 production
abc 6-Jan-13 down
abc 7-Jan-13 down
abc 8-Jan-13 down
abc 9-Jan-13 down
abc 10-Jan-13 down
xyz 3-Jan-13 production
xyz 4-Jan-13 production
xyz 5-Jan-13 production
xyz 6-Jan-13 production
xyz 7-Jan-13 production
xyz 8-Jan-13 production
xyz 9-Jan-13 down
xyz 10-Jan-13 down
xyz 11-Jan-13 down
xyz 12-Jan-13 down
xyz 13-Jan-13 down
xyz 14-Jan-13 down
xyz 15-Jan-13 down
R 코드
startdate=c('1-Jan-13','6-Jan-13','3-Jan-13','9-Jan-13')
enddate=c('5-Jan-13', '10-Jan-13', '8-Jan-13', '15-Jan-13')
toolid=c('abc', 'abc', 'xyz', 'xyz')
stage=c('production', 'down', 'production', 'down')
data=data.frame(toolid,startdate,enddate,stage)
require(reshape2)
newdata=melt(data,id.vars=c('toolid','stage'))
업데이트 : 아래 Ananda Mahto 대답 @에서 코드를 대처 내가 확신
## Convert "startdate" and "enddate" to date objects
data$startdate <- as.Date(data$startdate, format="%d-%b-%y")
data$enddate <- as.Date(data$enddate, format="%d-%b-%y")
## Use `seq` to create the date sequence, and manually recreate
## your dataframe. `do.call(rbind, ...) to put it back together
ddd=do.call(rbind, lapply(sequence(nrow(data)), function(x) {
data.frame(toolid = data$toolid[x],
date = seq(data$startdate[x], data$enddate[x], by = 1),
stage = data$stage[x])
}))
ddd
toolid date stage
1 abc 2013-01-01 production
2 abc 2013-01-02 production
3 abc 2013-01-03 production
4 abc 2013-01-04 production
5 abc 2013-01-05 production
6 abc 2013-01-06 down
7 abc 2013-01-07 down
8 abc 2013-01-08 down
9 abc 2013-01-09 down
10 abc 2013-01-10 down
11 xyz 2013-01-03 production
12 xyz 2013-01-04 production
13 xyz 2013-01-05 production
14 xyz 2013-01-06 production
15 xyz 2013-01-07 production
16 xyz 2013-01-08 production
17 xyz 2013-01-09 down
18 xyz 2013-01-10 down
19 xyz 2013-01-11 down
20 xyz 2013-01-12 down
21 xyz 2013-01-13 down
22 xyz 2013-01-14 down
23 xyz 2013-01-15 down
ddd1=dcast(ddd,date~stage)
ddd1
date down production
1 2013-01-01 0 1
2 2013-01-02 0 1
3 2013-01-03 0 2
4 2013-01-04 0 2
5 2013-01-05 0 2
6 2013-01-06 1 1
7 2013-01-07 1 1
8 2013-01-08 1 1
9 2013-01-09 2 0
10 2013-01-10 2 0
11 2013-01-11 1 0
12 2013-01-12 1 0
13 2013-01-13 1 0
14 2013-01-14 1 0
15 2013-01-15 1 0
일을 - 날 웃게 만드는 : *이 * 빨리 내 마음에 온 것입니다. 네, 저도 그렇게 생각했습니다. 신난다. –
@ SimonO101, 나는'zoo' /'xts' 해결책이있을 것이라고 확신하지만, 나는 그것을 찾기 위해 Google에해야 할 수도 있습니다 .... – A5C1D2H2I1M1N2O1R2T1
@AnandaMahto 코드를 사용하여 원래 질문에 붙여 넣었습니다. 내가 찾고 있던 최종 결과물을 얻기 위해 몇 줄의 코드를 추가했다. 그 유사한 출력을 얻을 수있는 더 좋은 방법이 있다면 알려주십시오. "dcast 명령을 입력하면 메시지 아래에"값 열로 스테이지 사용 : value.var을 사용하여 재정의합니다. 집계 기능이 누락되었습니다 : 길이를 기본값으로 설정 " 하지만 여전히 내가 찾던 결과가 – user2543622