2015-01-13 61 views
1

我有兩個數據集在R(以下這些表只是更小的版本),我想合併成一個新的數據框。嵌套循環輸出到data.frame

> meetingtime2  
#two columns of datetime that class=factor 

       ST     ET 
1 2014-12-22 07:00:00 2014-12-22 07:30:00 
2 2014-12-22 07:30:00 2014-12-22 08:00:00 
3 2014-12-22 08:00:00 2014-12-22 08:30:00 
4 2014-12-22 08:30:00 2014-12-22 09:00:00 
5 2014-12-22 09:00:00 2014-12-22 09:30:00 

> roomdata2 
#three columns; Room=factor, Capacity=integer, Video Conference=numeric 

    Room Capacity Video.Conference 
1 0M02A  16    1 
2 0M03A  8    0 
3 0M03B  12    1 

期望的輸出將是15行5列矩陣。簡而言之,輸出是每個房間的每個時間段。

#the following is a MANUALLY created output of what the first few rows should look like 

    Room Capacity Video.Conference  ST    ET 
1 0M02A 16   1  2014-12-22 07:00:00 2014-12-22 07:30:00 
2 0M02A 16   1  2014-12-22 07:30:00 2014-12-22 08:00:00 
3 0M02A 16   1  2014-12-22 08:00:00 2014-12-22 08:30:00 
4 0M02A 16   1  2014-12-22 08:30:00 2014-12-22 09:00:00 
5 0M02A 16   1  2014-12-22 09:00:00 2014-12-22 09:30:00 
6 0M03A 16   1  2014-12-22 07:00:00 2014-12-22 07:30:00 
7 0M03A 16   1  2014-12-22 07:30:00 2014-12-22 08:00:00 
#and so forth to 15 rows. 

我使用嵌套循環

#note, the code is written so I can apply to a bigger (1000's of rows) dataset 

>mylist<-list() 
>for(i in 1:(nrow(roomdata2))) 
    +{ for(j in 1:(nrow(meetingtime2))) 
+mylist[[j]]<-  data.frame(roomdata2[i,1],roomdata2[i,2],roomdata2[i,3], 
+meetingtime2[j,1],meetingtime2[j,2]) 
    } 
    >df<-do.call("rbind",mylist) 
>df 

輸出我得到嘗試。我讓所有的時隙的最後一個房間,只是沒有前面的房間

roomdata2.i..1. roomdata2.i..2. roomdata2.i..3. meetingtime2.j..1. meetingtime2.j..2. 
1 0M03B   12    1   2014-12-22 07:00:00 2014-12-22 07:30:00 
2 0M03B   12    1   2014-12-22 07:30:00 2014-12-22 08:00:00 
3 0M03B   12    1   2014-12-22 08:00:00 2014-12-22 08:30:00 
4 0M03B   12    1   2014-12-22 08:30:00 2014-12-22 09:00:00 
5 0M03B   12    1   2014-12-22 09:00:00 2014-12-22 09:30:00 

我知道我的代碼是遠離正確的,是給我循環的最後一次迭代。

另一種方法我搜索這是針對每次迭代

>for(i in 1:(nrow(roomdata2))) 
>for(j in 1:(nrow(meetingtime2))) 
>print(paste(roomdata2[i,1],roomdata2[i,2],roomdata2[i,3], 
+meetingtime2[j,1],meetingtime2[j,2])) 

輸出

[1] "0M02A 16 1 2014-12-22 07:00:00 2014-12-22 07:30:00" 
[1] "0M02A 16 1 2014-12-22 07:30:00 2014-12-22 08:00:00" 
[1] "0M02A 16 1 2014-12-22 08:00:00 2014-12-22 08:30:00" 
[1] "0M02A 16 1 2014-12-22 08:30:00 2014-12-22 09:00:00" 
[1] "0M02A 16 1 2014-12-22 09:00:00 2014-12-22 09:30:00" 
[1] "0M03A 8 0 2014-12-22 07:00:00 2014-12-22 07:30:00" 
[1] "0M03A 8 0 2014-12-22 07:30:00 2014-12-22 08:00:00" 
[1] "0M03A 8 0 2014-12-22 08:00:00 2014-12-22 08:30:00" 
[1] "0M03A 8 0 2014-12-22 08:30:00 2014-12-22 09:00:00" 
[1] "0M03A 8 0 2014-12-22 09:00:00 2014-12-22 09:30:00" 
[1] "0M03B 12 1 2014-12-22 07:00:00 2014-12-22 07:30:00" 
[1] "0M03B 12 1 2014-12-22 07:30:00 2014-12-22 08:00:00" 
[1] "0M03B 12 1 2014-12-22 08:00:00 2014-12-22 08:30:00" 
[1] "0M03B 12 1 2014-12-22 08:30:00 2014-12-22 09:00:00" 
[1] "0M03B 12 1 2014-12-22 09:00:00 2014-12-22 09:30:00" 

#however the values are not separated, they are just in one set of string for each row. 

的期望結果的連續打印功能是一個表等的正上方,而是一個與數據幀每個值都在一個單獨的列中(每個日期在一列中設置爲&時間)。

我已經看過列表,lapply,foreach,但我無法將頭圍繞解決方案。 任何幫助將不勝感激,我是一個初學者,所以我渴望學習。

乾杯 *的dputs

>dput(meetingtime2) 

結構(列表(ST =結構(1:5,.Label = C( 「22/12/2014 7:00」, 「22日/ 12/2014年7月30日「,」2014年12月22日8:00「,」2014年12月22日8:30「,」2014年12月22日9:00「 ),class =」factor「結構(1:5,.Label = c(「22/12/2014 7:30」, 「2014年12月22日8:00」,「22/12/2014 8:30」,「22/12/2014)9:00「,」22/12/2014 9:30「 ),class =」factor「)),.Names = c(」ST「,」ET「),row.names = c(NA, -5L),class =「data.frame」)

>dput(roomdata2) 

結構(列表(房間=結構(1:3,.Label = C( 「0M02A」, 「0M03A」, 「0M03B」),類= 「因子」),容量= C(16L,8L ,12L),Video.Conference = c(1L, 0L,1L)),.Names = c(「Room」,「Capacity」,「Video.Conference」),row.names = c(NA, -3L )的class = 「data.frame」)

+0

你能不能把輸出dput的' (meetingtime2)'和'dput(roomdata2)'在你的問題中? –

回答

3

使用您的數據:

meetingtime2 <- read.csv(text = "ST,ET 
2014-12-22 07:00:00,2014-12-22 07:30:00 
2014-12-22 07:30:00,2014-12-22 08:00:00 
2014-12-22 08:00:00,2014-12-22 08:30:00 
2014-12-22 08:30:00,2014-12-22 09:00:00 
2014-12-22 09:00:00,2014-12-22 09:30:00") 

roomdata2 <- read.csv(text = "Room,Capacity,Video_Conference 
0M02A,16,1 
0M03A,8,0 
0M03B,12,1") 

然後merge輕而易舉地返回笛卡爾乘積,因爲沒有任何列匹配。

merge(meetingtime2, roomdata2)[, c(3:5, 1:2)] 

##  Room Capacity Video_Conference     ST     ET 
## 1 0M02A  16    1 2014-12-22 07:00:00 2014-12-22 07:30:00 
## 2 0M02A  16    1 2014-12-22 07:30:00 2014-12-22 08:00:00 
## 3 0M02A  16    1 2014-12-22 08:00:00 2014-12-22 08:30:00 
## 4 0M02A  16    1 2014-12-22 08:30:00 2014-12-22 09:00:00 
## 5 0M02A  16    1 2014-12-22 09:00:00 2014-12-22 09:30:00 
+0

但是,結果可能無法按照OP的預期方式進行排序。 – Avraham

+0

這非常簡單,我永遠不會猜到這一點。完美的工作 – Nic

0

這是醜陋的,但應該完成工作。鑑於以下數據:

ST <- c('2014-12-22 07:00:00', '2014-12-22 07:30:00', '2014-12-22 08:00:00', '2014-12-22 08:30:00', '2014-12-22 09:00:00') 
ET <- c('2014-12-22 07:30:00', '2014-12-22 08:00:00', '2014-12-22 08:30:00', '2014-12-22 09:00:00', '2014-12-22 09:30:00') 

RoomName <- c('0M02A', '0M03A', '0M03B') 
Capacity <- c(16, 8, 12) 
VideoCap <- c(1, 0, 1) 

Times <- data.frame(ST, ET, stringsAsFactors = FALSE) 
Rooms <- data.frame(RoomName, Capacity, VideoCap,stringsAsFactors = FALSE) 

下面的函數應該做你想要什麼:

Smash <- function(DF1, DF2){ 
    nm <- dim(DF1) 
    pq <- dim(DF2)  
    maxrow <- nm[[1]] * pq[[1]] 
    maxcol <- nm[[2]] + pq[[2]] 
    MAT <- matrix('A', nrow = maxrow, ncol = maxcol) 
    currow <- 1 
    for (i1 in seq_len(nm[[1]])) { 
     for (i2 in seq_len(pq[[1]])) { 
     curcol <- 1 
     for (j in seq_len(nm[[2]])) { 
      MAT[currow, curcol] <- DF1[i1, j] 
      curcol <- curcol + 1 
     } 
     for (j in seq_len(pq[[2]])) { 
      MAT[currow, curcol] <- DF2[i2, j] 
      curcol <- curcol + 1 
     } 
     currow <- currow + 1 
     } 
    } 
    DF <- data.frame(MAT) 
    names(DF) <- c(names(DF1), names(DF2)) 
    return(DF) 
} 

粉碎(會議室時報)返回:

> Smash(Rooms, Times) 
    RoomName Capacity VideoCap     ST     ET 
1  0M02A  16  1 2014-12-22 07:00:00 2014-12-22 07:30:00 
2  0M02A  16  1 2014-12-22 07:30:00 2014-12-22 08:00:00 
3  0M02A  16  1 2014-12-22 08:00:00 2014-12-22 08:30:00 
4  0M02A  16  1 2014-12-22 08:30:00 2014-12-22 09:00:00 
5  0M02A  16  1 2014-12-22 09:00:00 2014-12-22 09:30:00 
6  0M03A  8  0 2014-12-22 07:00:00 2014-12-22 07:30:00 
7  0M03A  8  0 2014-12-22 07:30:00 2014-12-22 08:00:00 
8  0M03A  8  0 2014-12-22 08:00:00 2014-12-22 08:30:00 
9  0M03A  8  0 2014-12-22 08:30:00 2014-12-22 09:00:00 
10 0M03A  8  0 2014-12-22 09:00:00 2014-12-22 09:30:00 
11 0M03B  12  1 2014-12-22 07:00:00 2014-12-22 07:30:00 
12 0M03B  12  1 2014-12-22 07:30:00 2014-12-22 08:00:00 
13 0M03B  12  1 2014-12-22 08:00:00 2014-12-22 08:30:00 
14 0M03B  12  1 2014-12-22 08:30:00 2014-12-22 09:00:00 
15 0M03B  12  1 2014-12-22 09:00:00 2014-12-22 09:30:00 
+0

我會試用這個,可能比笛卡爾產品更強大 – Nic