[R] Data frame loop
Hosack, Michael
mhosack at state.pa.us
Wed Mar 16 15:58:44 CET 2011
R users,
I am trying to use a numeric vector to extract rows from a data frame. I want to extract two rows
per unique WEEK (27 weeks) based on variable DOW_NUM by using pairs of vector (s3) elements in the order
they appear. For example, vector initial elements 7 and 5 will be used to extract row numbers 2 and 4
from the dataframe, vector elements 4 and 5 will extract rows 8 and 9 from the data frame, and so on. The
loop below will extract two rows per week when a given pair of indices are input for s3, however, I have
been unsuccessful in coding the indices such that they generate the sequence I need. Also, if anyone knows
a way to accomplish this without using loops I would be very interested in learning how to do so.
Thank you,
Mike
DF <- data.frame(DATE=numeric(0),DOW=character(0),DOW_NUM=numeric(0),WEEK=numeric(0))
for (j in 1:max(DF1WD$WEEK)) {
DF <- rbind(DF, DF1WD[DF1WD$DOW_NUM %in% s3[ : ] & DF1WD$WEEK %in% j, ])
}
WK1 <- DF1WD[DF1WD$DOW_NUM %in% s3[1:2] & DF1WD$WEEK %in% 1, ]
WK2 <- DF1WD[DF1WD$DOW_NUM %in% s3[3:4] & DF1WD$WEEK %in% 2, ]
WK3 <- DF1WD[DF1WD$DOW_NUM %in% s3[5:6] & DF1WD$WEEK %in% 3, ]
.
.
.
WK27<- DF1WD[DF1WD$DOW_NUM %in% s3[53:54] & DF1WD$WEEK %in% 27, ]
SCHEDULE <- rbind(WK1,WK2,WK3,...,WK27)
DF1WD <-
structure(list(DATE = structure(c(15461, 15462, 15463, 15464,
15467, 15468, 15469, 15470, 15471, 15474, 15475, 15476, 15477,
15478, 15481, 15482, 15483, 15484, 15485, 15488, 15489, 15490,
15491, 15492, 15495, 15496, 15497, 15498, 15499, 15502, 15503,
15504, 15505, 15506, 15509, 15510, 15511, 15512, 15513, 15516,
15517, 15518, 15519, 15520, 15523, 15524, 15525, 15526, 15527,
15530, 15531, 15532, 15533, 15534, 15537, 15538, 15539, 15540,
15541, 15544, 15545, 15546, 15547, 15548, 15551, 15552, 15553,
15554, 15555, 15558, 15559, 15560, 15561, 15562, 15565, 15566,
15567, 15568, 15569, 15572, 15573, 15574, 15575, 15576, 15579,
15580, 15581, 15582, 15583, 15586, 15587, 15588, 15589, 15590,
15593, 15594, 15595, 15596, 15597, 15600, 15601, 15602, 15603,
15604, 15607, 15608, 15609, 15610, 15611, 15614, 15615, 15616,
15617, 15618, 15621, 15622, 15623, 15624, 15625, 15628, 15629,
15630, 15631, 15632, 15635, 15636, 15637, 15638, 15639, 15642,
15643, 15644), class = "Date"), DOW = c("Tue", "Wed", "Thu",
"Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon", "Tue", "Wed",
"Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon", "Tue",
"Wed", "Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon",
"Tue", "Wed", "Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri",
"Mon", "Tue", "Wed", "Thu", "Fri", "Mon", "Tue", "Wed", "Thu",
"Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon", "Tue", "Wed",
"Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon", "Tue",
"Wed", "Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon",
"Tue", "Wed", "Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri",
"Mon", "Tue", "Wed", "Thu", "Fri", "Mon", "Tue", "Wed", "Thu",
"Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon", "Tue", "Wed",
"Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon", "Tue",
"Wed", "Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri", "Mon",
"Tue", "Wed", "Thu", "Fri", "Mon", "Tue", "Wed", "Thu", "Fri",
"Mon", "Tue", "Wed"), DOW_NUM = c(4, 5, 6, 7, 3, 4, 5, 6, 7,
3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3,
4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4,
5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5,
6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6,
7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7,
3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5, 6, 7, 3, 4, 5), WEEK = c(1,
1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5,
5, 5, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9,
9, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12,
13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 16,
16, 16, 16, 16, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19,
19, 19, 19, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 22, 22, 22,
22, 22, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 25, 25, 25, 25,
25, 26, 26, 26, 26, 26, 27, 27, 27)), .Names = c("DATE", "DOW",
"DOW_NUM", "WEEK"), row.names = c(1L, 2L, 3L, 4L, 7L, 8L, 9L,
10L, 11L, 14L, 15L, 16L, 17L, 18L, 21L, 22L, 23L, 24L, 25L, 28L,
29L, 30L, 31L, 32L, 35L, 36L, 37L, 38L, 39L, 42L, 43L, 44L, 45L,
46L, 49L, 50L, 51L, 52L, 53L, 56L, 57L, 58L, 59L, 60L, 63L, 64L,
65L, 66L, 67L, 70L, 71L, 72L, 73L, 74L, 77L, 78L, 79L, 80L, 81L,
84L, 85L, 86L, 87L, 88L, 91L, 92L, 93L, 94L, 95L, 98L, 99L, 100L,
101L, 102L, 105L, 106L, 107L, 108L, 109L, 112L, 113L, 114L, 115L,
116L, 119L, 120L, 121L, 122L, 123L, 126L, 127L, 128L, 129L, 130L,
133L, 134L, 135L, 136L, 137L, 140L, 141L, 142L, 143L, 144L, 147L,
148L, 149L, 150L, 151L, 154L, 155L, 156L, 157L, 158L, 161L, 162L,
163L, 164L, 165L, 168L, 169L, 170L, 171L, 172L, 175L, 176L, 177L,
178L, 179L, 182L, 183L, 184L), class = "data.frame")
s3 <-
c(7, 5, 4, 5, 7, 6, 3, 5, 3, 6, 7, 4, 6, 5, 7, 3, 4, 5, 3, 7,
4, 6, 5, 6, 3, 7, 4, 7, 4, 6, 3, 5, 7, 3, 6, 4, 5, 7, 3, 4, 5,
6, 4, 6, 3, 5, 7, 5, 4, 7, 6, 3, 5, 4)
More information about the R-help
mailing list