嵌套循环输出到 data.frame
Nested loop output to a data.frame
我在 R 中有两个数据集(下面的这些 table 只是较小的版本),我想将它们合并到一个新的数据框中。
> 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.
期望的结果是 table 就像上面直接的那样,但是每个值都在单独的列中的数据框(每个日期和时间都设置在一列中)。
我查看了列表,lapply,foreach,但我就是无法理解解决方案。
任何帮助将不胜感激,我是初学者所以我很想学习。
干杯
*输入
>dput(meetingtime2)
结构(列表(ST =结构(1:5,.Label = c(“22/12/2014 7:00”,
"22/12/2014 7:30", "22/12/2014 8:00", "22/12/2014 8:30", "22/12/2014 9:00 “
), class = "factor"), ET = 结构(1:5, .Label = c("22/12/2014 7:30",
"22/12/2014 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"), class = "factor"), 容量 = c(16L, 8L, 12L), Video.Conference = c(1L,
0L, 1L)), .Names = c("Room", "Capacity", "Video.Conference"), row.names = c(NA,
-3L), class = "data.frame")
使用您的数据:
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
方便地 returns 笛卡尔积,因为 none 的列匹配。
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
这很丑陋,但应该可以完成工作。给定以下数据:
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)
}
粉碎(房间,时间)returns:
> 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
我在 R 中有两个数据集(下面的这些 table 只是较小的版本),我想将它们合并到一个新的数据框中。
> 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.
期望的结果是 table 就像上面直接的那样,但是每个值都在单独的列中的数据框(每个日期和时间都设置在一列中)。
我查看了列表,lapply,foreach,但我就是无法理解解决方案。 任何帮助将不胜感激,我是初学者所以我很想学习。
干杯 *输入
>dput(meetingtime2)
结构(列表(ST =结构(1:5,.Label = c(“22/12/2014 7:00”, "22/12/2014 7:30", "22/12/2014 8:00", "22/12/2014 8:30", "22/12/2014 9:00 “ ), class = "factor"), ET = 结构(1:5, .Label = c("22/12/2014 7:30", "22/12/2014 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"), class = "factor"), 容量 = c(16L, 8L, 12L), Video.Conference = c(1L, 0L, 1L)), .Names = c("Room", "Capacity", "Video.Conference"), row.names = c(NA, -3L), class = "data.frame")
使用您的数据:
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
方便地 returns 笛卡尔积,因为 none 的列匹配。
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
这很丑陋,但应该可以完成工作。给定以下数据:
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)
}
粉碎(房间,时间)returns:
> 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