How to draw specific time intervals of rows from a data frame?











up vote
0
down vote

favorite












I have a data frame with 3523 observation and 92 variables.



Below an example of a data frame with 10 observations and 04:00-05:00



     04:00 04:15 04:30 05:00 05:15 05:30
1: - - - - - -
2: 2 2 2 - 2 2
3: 2 - - 2 2 2
4: - - 2 - 2 2
5: - - - - 2 2
6: 2 - 2 2 - 2
7: - - - - 2 2
8: 2 2 - 2 2 2
9: - - - - 2 2
10: 2 2 - 2 2 2

.


The columns define 24h time from 4:00am till 4:00am (15 minutes interval). The rows define number of observation.



Each row contain values '-' and '2'.



I want to extract the beginning and the ending of the intervals starting with '2'



For example 2: 04:00-04:30; 3: 04:00 ; 05:00 4: 04:30



I also would like to save the output in an excel or txt file. Could you help me please










share|improve this question
























  • That's not clear, how you calculate n. The interval starts with 2 and ends with 2 no mater if there are - inbetween? Row number corresponds to n (e.g. row 2 - n == 2)? If so, why 2: 04:15 - 04:30? Shouldn't it be 2: 04:00 - 04:30?
    – utubun
    Nov 11 at 13:06










  • Dear @utubun yes, you are true - I corrected the mistake - > could you help me please?
    – RforDummies
    Nov 11 at 13:22










  • I'll try to do my best, @Mate
    – utubun
    Nov 11 at 14:16















up vote
0
down vote

favorite












I have a data frame with 3523 observation and 92 variables.



Below an example of a data frame with 10 observations and 04:00-05:00



     04:00 04:15 04:30 05:00 05:15 05:30
1: - - - - - -
2: 2 2 2 - 2 2
3: 2 - - 2 2 2
4: - - 2 - 2 2
5: - - - - 2 2
6: 2 - 2 2 - 2
7: - - - - 2 2
8: 2 2 - 2 2 2
9: - - - - 2 2
10: 2 2 - 2 2 2

.


The columns define 24h time from 4:00am till 4:00am (15 minutes interval). The rows define number of observation.



Each row contain values '-' and '2'.



I want to extract the beginning and the ending of the intervals starting with '2'



For example 2: 04:00-04:30; 3: 04:00 ; 05:00 4: 04:30



I also would like to save the output in an excel or txt file. Could you help me please










share|improve this question
























  • That's not clear, how you calculate n. The interval starts with 2 and ends with 2 no mater if there are - inbetween? Row number corresponds to n (e.g. row 2 - n == 2)? If so, why 2: 04:15 - 04:30? Shouldn't it be 2: 04:00 - 04:30?
    – utubun
    Nov 11 at 13:06










  • Dear @utubun yes, you are true - I corrected the mistake - > could you help me please?
    – RforDummies
    Nov 11 at 13:22










  • I'll try to do my best, @Mate
    – utubun
    Nov 11 at 14:16













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a data frame with 3523 observation and 92 variables.



Below an example of a data frame with 10 observations and 04:00-05:00



     04:00 04:15 04:30 05:00 05:15 05:30
1: - - - - - -
2: 2 2 2 - 2 2
3: 2 - - 2 2 2
4: - - 2 - 2 2
5: - - - - 2 2
6: 2 - 2 2 - 2
7: - - - - 2 2
8: 2 2 - 2 2 2
9: - - - - 2 2
10: 2 2 - 2 2 2

.


The columns define 24h time from 4:00am till 4:00am (15 minutes interval). The rows define number of observation.



Each row contain values '-' and '2'.



I want to extract the beginning and the ending of the intervals starting with '2'



For example 2: 04:00-04:30; 3: 04:00 ; 05:00 4: 04:30



I also would like to save the output in an excel or txt file. Could you help me please










share|improve this question















I have a data frame with 3523 observation and 92 variables.



Below an example of a data frame with 10 observations and 04:00-05:00



     04:00 04:15 04:30 05:00 05:15 05:30
1: - - - - - -
2: 2 2 2 - 2 2
3: 2 - - 2 2 2
4: - - 2 - 2 2
5: - - - - 2 2
6: 2 - 2 2 - 2
7: - - - - 2 2
8: 2 2 - 2 2 2
9: - - - - 2 2
10: 2 2 - 2 2 2

.


The columns define 24h time from 4:00am till 4:00am (15 minutes interval). The rows define number of observation.



Each row contain values '-' and '2'.



I want to extract the beginning and the ending of the intervals starting with '2'



For example 2: 04:00-04:30; 3: 04:00 ; 05:00 4: 04:30



I also would like to save the output in an excel or txt file. Could you help me please







r function matrix return extract






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 18:41

























asked Nov 11 at 12:10









RforDummies

417




417












  • That's not clear, how you calculate n. The interval starts with 2 and ends with 2 no mater if there are - inbetween? Row number corresponds to n (e.g. row 2 - n == 2)? If so, why 2: 04:15 - 04:30? Shouldn't it be 2: 04:00 - 04:30?
    – utubun
    Nov 11 at 13:06










  • Dear @utubun yes, you are true - I corrected the mistake - > could you help me please?
    – RforDummies
    Nov 11 at 13:22










  • I'll try to do my best, @Mate
    – utubun
    Nov 11 at 14:16


















  • That's not clear, how you calculate n. The interval starts with 2 and ends with 2 no mater if there are - inbetween? Row number corresponds to n (e.g. row 2 - n == 2)? If so, why 2: 04:15 - 04:30? Shouldn't it be 2: 04:00 - 04:30?
    – utubun
    Nov 11 at 13:06










  • Dear @utubun yes, you are true - I corrected the mistake - > could you help me please?
    – RforDummies
    Nov 11 at 13:22










  • I'll try to do my best, @Mate
    – utubun
    Nov 11 at 14:16
















That's not clear, how you calculate n. The interval starts with 2 and ends with 2 no mater if there are - inbetween? Row number corresponds to n (e.g. row 2 - n == 2)? If so, why 2: 04:15 - 04:30? Shouldn't it be 2: 04:00 - 04:30?
– utubun
Nov 11 at 13:06




That's not clear, how you calculate n. The interval starts with 2 and ends with 2 no mater if there are - inbetween? Row number corresponds to n (e.g. row 2 - n == 2)? If so, why 2: 04:15 - 04:30? Shouldn't it be 2: 04:00 - 04:30?
– utubun
Nov 11 at 13:06












Dear @utubun yes, you are true - I corrected the mistake - > could you help me please?
– RforDummies
Nov 11 at 13:22




Dear @utubun yes, you are true - I corrected the mistake - > could you help me please?
– RforDummies
Nov 11 at 13:22












I'll try to do my best, @Mate
– utubun
Nov 11 at 14:16




I'll try to do my best, @Mate
– utubun
Nov 11 at 14:16












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










I've edited it after after the discussion with @Mate (see the comments to this answer):



Solution:



library(tidyverse) 

dat %>%
rownames_to_column("n") %>%
mutate(n = as.integer(n)) %>%
gather(key = "time", value = "observation", -n) %>%
group_by(n) %>%
filter(observation == "2") %>%
summarize(
interval = paste(time[seq(1, n(), 2)],
c(time, "...")[seq(2, n() + n() %% 2, 2)],
sep = "-",
collapse = ", ")
) %>%
ungroup() %>%
arrange(n) %>%
write_csv("my_results.csv")


Output



# A tibble: 100 x 2
n interval
<int> <chr>
1 1 04:30-14:00, 19:30-20:15, 22:30-01:15, 03:45-...
2 2 06:15-08:00, 09:00-12:00, 13:45-16:30, 18:45-23:15, 00:30-02:15
3 3 06:00-06:30, 08:00-09:45, 11:15-13:30, 14:15-23:15, 01:00-01:30
4 4 20:00-21:15, 23:30-03:15
5 5 05:00-09:30, 10:00-10:30, 11:45-12:00, 13:15-13:30, 14:00-20:15, 20:30-21:3~
6 6 07:45-08:30, 09:15-13:15, 19:15-19:30, 20:30-20:45, 21:00-21:45, 01:45-...
7 7 09:30-17:45, 21:15-...
8 8 07:00-09:30, 12:45-18:00, 19:00-21:15, 00:15-02:00
9 9 05:45-06:15, 09:00-16:00, 17:15-19:45, 21:15-22:30, 23:00-...
10 10 10:00-10:15, 12:15-13:30, 16:15-16:45, 21:30-23:45, 00:45-01:30


Data



colnms <- paste(
str_pad(rep(c(4:23, 0:3), each = 4), 2, "left", 0),
str_pad(rep(c(0, 15, 30, 45), times = 24), 2, "left", 0),
sep = ":"
)

set.seed(53248604)
dat <- matrix(sample(c("-", 2), 9600, prob = c(0.9, 0.1), replace = T), nrow = 100)

dimnames(dat) <- list(1:100, colnms)

dat <- as.data.frame(dat)





share|improve this answer























  • Dear @utubun you are amazing is working; but I have a big request to you how to mine the interval that starts and ends with 2. As example example 3 it would be 04:00 and 05:00? Thank you
    – RforDummies
    Nov 11 at 14:30










  • Do you mean not to count the intervals where n('2') == 1 as done by @jay.sf? btw look at @jay.sf answer, we both can learn a lot from it.
    – utubun
    Nov 11 at 14:36






  • 1




    Dear @utubun what I mean to count the intervals not just the first and the last '2'; but also the 'spaces' or the intervals that make the case between then. For example let's assume that the table has "2" from 07:15 till 10:30 then there is a "-" at 11:00 there is another interval with "2" till 11:45 than there is "-" and at 12:30 start another "2" intervals till 14:45 This I am failing to understand how to code..Could you help me please?
    – RforDummies
    Nov 11 at 15:15












  • Ok, let me think about it
    – utubun
    Nov 11 at 15:16






  • 1




    Apologize for this late reply; true you need to return all the 2; so 07:15-10:30, 11:00-11:45, 12:30-14:45 and 22:45-thank you and apologize
    – RforDummies
    Nov 11 at 16:13


















up vote
1
down vote













You could work with range() like so:



fun <- function(dat) {
L <- lapply(seq_along(dat), function(x, ...) {
if (length(dat[x, dat[x, ] == 2, ]) >= 2) {
range(names(dat[x, dat[x, ] == 2]))
} else if (length(dat[x, dat[x, ] == 2, ]) == 1) {
c(names(dat)[which(dat[x, ] == 2)], NA)
}})
setNames(data.frame(do.call(rbind, L)), c("t0", "t1"))
}


Yields



> fun(df1)
t0 t1
1 04:00 04:30
2 04:00 04:45
3 04:30 <NA>


Data



df1 <- setNames(data.frame(matrix(c(rep("-", 4), rep(2, 3), "-", 
rep(c(2, rep("-", 2)), 2),2, rep("-", 25)),
ncol=4, byrow=TRUE)),
strftime(as.POSIXct((0:3)*15*60 + 3*60*60, origin=Sys.Date() ),
format="%H:%M"))

> df1
04:00 04:15 04:30 04:45
1 - - - -
2 2 2 2 -
3 2 - - 2
4 - - 2 -
5 - - - -
6 - - - -
7 - - - -
8 - - - -
9 - - - -
10 - - - -





share|improve this answer

















  • 1




    Cool answer, made my mental notes about your approach.
    – utubun
    Nov 11 at 14:44











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248604%2fhow-to-draw-specific-time-intervals-of-rows-from-a-data-frame%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I've edited it after after the discussion with @Mate (see the comments to this answer):



Solution:



library(tidyverse) 

dat %>%
rownames_to_column("n") %>%
mutate(n = as.integer(n)) %>%
gather(key = "time", value = "observation", -n) %>%
group_by(n) %>%
filter(observation == "2") %>%
summarize(
interval = paste(time[seq(1, n(), 2)],
c(time, "...")[seq(2, n() + n() %% 2, 2)],
sep = "-",
collapse = ", ")
) %>%
ungroup() %>%
arrange(n) %>%
write_csv("my_results.csv")


Output



# A tibble: 100 x 2
n interval
<int> <chr>
1 1 04:30-14:00, 19:30-20:15, 22:30-01:15, 03:45-...
2 2 06:15-08:00, 09:00-12:00, 13:45-16:30, 18:45-23:15, 00:30-02:15
3 3 06:00-06:30, 08:00-09:45, 11:15-13:30, 14:15-23:15, 01:00-01:30
4 4 20:00-21:15, 23:30-03:15
5 5 05:00-09:30, 10:00-10:30, 11:45-12:00, 13:15-13:30, 14:00-20:15, 20:30-21:3~
6 6 07:45-08:30, 09:15-13:15, 19:15-19:30, 20:30-20:45, 21:00-21:45, 01:45-...
7 7 09:30-17:45, 21:15-...
8 8 07:00-09:30, 12:45-18:00, 19:00-21:15, 00:15-02:00
9 9 05:45-06:15, 09:00-16:00, 17:15-19:45, 21:15-22:30, 23:00-...
10 10 10:00-10:15, 12:15-13:30, 16:15-16:45, 21:30-23:45, 00:45-01:30


Data



colnms <- paste(
str_pad(rep(c(4:23, 0:3), each = 4), 2, "left", 0),
str_pad(rep(c(0, 15, 30, 45), times = 24), 2, "left", 0),
sep = ":"
)

set.seed(53248604)
dat <- matrix(sample(c("-", 2), 9600, prob = c(0.9, 0.1), replace = T), nrow = 100)

dimnames(dat) <- list(1:100, colnms)

dat <- as.data.frame(dat)





share|improve this answer























  • Dear @utubun you are amazing is working; but I have a big request to you how to mine the interval that starts and ends with 2. As example example 3 it would be 04:00 and 05:00? Thank you
    – RforDummies
    Nov 11 at 14:30










  • Do you mean not to count the intervals where n('2') == 1 as done by @jay.sf? btw look at @jay.sf answer, we both can learn a lot from it.
    – utubun
    Nov 11 at 14:36






  • 1




    Dear @utubun what I mean to count the intervals not just the first and the last '2'; but also the 'spaces' or the intervals that make the case between then. For example let's assume that the table has "2" from 07:15 till 10:30 then there is a "-" at 11:00 there is another interval with "2" till 11:45 than there is "-" and at 12:30 start another "2" intervals till 14:45 This I am failing to understand how to code..Could you help me please?
    – RforDummies
    Nov 11 at 15:15












  • Ok, let me think about it
    – utubun
    Nov 11 at 15:16






  • 1




    Apologize for this late reply; true you need to return all the 2; so 07:15-10:30, 11:00-11:45, 12:30-14:45 and 22:45-thank you and apologize
    – RforDummies
    Nov 11 at 16:13















up vote
0
down vote



accepted










I've edited it after after the discussion with @Mate (see the comments to this answer):



Solution:



library(tidyverse) 

dat %>%
rownames_to_column("n") %>%
mutate(n = as.integer(n)) %>%
gather(key = "time", value = "observation", -n) %>%
group_by(n) %>%
filter(observation == "2") %>%
summarize(
interval = paste(time[seq(1, n(), 2)],
c(time, "...")[seq(2, n() + n() %% 2, 2)],
sep = "-",
collapse = ", ")
) %>%
ungroup() %>%
arrange(n) %>%
write_csv("my_results.csv")


Output



# A tibble: 100 x 2
n interval
<int> <chr>
1 1 04:30-14:00, 19:30-20:15, 22:30-01:15, 03:45-...
2 2 06:15-08:00, 09:00-12:00, 13:45-16:30, 18:45-23:15, 00:30-02:15
3 3 06:00-06:30, 08:00-09:45, 11:15-13:30, 14:15-23:15, 01:00-01:30
4 4 20:00-21:15, 23:30-03:15
5 5 05:00-09:30, 10:00-10:30, 11:45-12:00, 13:15-13:30, 14:00-20:15, 20:30-21:3~
6 6 07:45-08:30, 09:15-13:15, 19:15-19:30, 20:30-20:45, 21:00-21:45, 01:45-...
7 7 09:30-17:45, 21:15-...
8 8 07:00-09:30, 12:45-18:00, 19:00-21:15, 00:15-02:00
9 9 05:45-06:15, 09:00-16:00, 17:15-19:45, 21:15-22:30, 23:00-...
10 10 10:00-10:15, 12:15-13:30, 16:15-16:45, 21:30-23:45, 00:45-01:30


Data



colnms <- paste(
str_pad(rep(c(4:23, 0:3), each = 4), 2, "left", 0),
str_pad(rep(c(0, 15, 30, 45), times = 24), 2, "left", 0),
sep = ":"
)

set.seed(53248604)
dat <- matrix(sample(c("-", 2), 9600, prob = c(0.9, 0.1), replace = T), nrow = 100)

dimnames(dat) <- list(1:100, colnms)

dat <- as.data.frame(dat)





share|improve this answer























  • Dear @utubun you are amazing is working; but I have a big request to you how to mine the interval that starts and ends with 2. As example example 3 it would be 04:00 and 05:00? Thank you
    – RforDummies
    Nov 11 at 14:30










  • Do you mean not to count the intervals where n('2') == 1 as done by @jay.sf? btw look at @jay.sf answer, we both can learn a lot from it.
    – utubun
    Nov 11 at 14:36






  • 1




    Dear @utubun what I mean to count the intervals not just the first and the last '2'; but also the 'spaces' or the intervals that make the case between then. For example let's assume that the table has "2" from 07:15 till 10:30 then there is a "-" at 11:00 there is another interval with "2" till 11:45 than there is "-" and at 12:30 start another "2" intervals till 14:45 This I am failing to understand how to code..Could you help me please?
    – RforDummies
    Nov 11 at 15:15












  • Ok, let me think about it
    – utubun
    Nov 11 at 15:16






  • 1




    Apologize for this late reply; true you need to return all the 2; so 07:15-10:30, 11:00-11:45, 12:30-14:45 and 22:45-thank you and apologize
    – RforDummies
    Nov 11 at 16:13













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I've edited it after after the discussion with @Mate (see the comments to this answer):



Solution:



library(tidyverse) 

dat %>%
rownames_to_column("n") %>%
mutate(n = as.integer(n)) %>%
gather(key = "time", value = "observation", -n) %>%
group_by(n) %>%
filter(observation == "2") %>%
summarize(
interval = paste(time[seq(1, n(), 2)],
c(time, "...")[seq(2, n() + n() %% 2, 2)],
sep = "-",
collapse = ", ")
) %>%
ungroup() %>%
arrange(n) %>%
write_csv("my_results.csv")


Output



# A tibble: 100 x 2
n interval
<int> <chr>
1 1 04:30-14:00, 19:30-20:15, 22:30-01:15, 03:45-...
2 2 06:15-08:00, 09:00-12:00, 13:45-16:30, 18:45-23:15, 00:30-02:15
3 3 06:00-06:30, 08:00-09:45, 11:15-13:30, 14:15-23:15, 01:00-01:30
4 4 20:00-21:15, 23:30-03:15
5 5 05:00-09:30, 10:00-10:30, 11:45-12:00, 13:15-13:30, 14:00-20:15, 20:30-21:3~
6 6 07:45-08:30, 09:15-13:15, 19:15-19:30, 20:30-20:45, 21:00-21:45, 01:45-...
7 7 09:30-17:45, 21:15-...
8 8 07:00-09:30, 12:45-18:00, 19:00-21:15, 00:15-02:00
9 9 05:45-06:15, 09:00-16:00, 17:15-19:45, 21:15-22:30, 23:00-...
10 10 10:00-10:15, 12:15-13:30, 16:15-16:45, 21:30-23:45, 00:45-01:30


Data



colnms <- paste(
str_pad(rep(c(4:23, 0:3), each = 4), 2, "left", 0),
str_pad(rep(c(0, 15, 30, 45), times = 24), 2, "left", 0),
sep = ":"
)

set.seed(53248604)
dat <- matrix(sample(c("-", 2), 9600, prob = c(0.9, 0.1), replace = T), nrow = 100)

dimnames(dat) <- list(1:100, colnms)

dat <- as.data.frame(dat)





share|improve this answer














I've edited it after after the discussion with @Mate (see the comments to this answer):



Solution:



library(tidyverse) 

dat %>%
rownames_to_column("n") %>%
mutate(n = as.integer(n)) %>%
gather(key = "time", value = "observation", -n) %>%
group_by(n) %>%
filter(observation == "2") %>%
summarize(
interval = paste(time[seq(1, n(), 2)],
c(time, "...")[seq(2, n() + n() %% 2, 2)],
sep = "-",
collapse = ", ")
) %>%
ungroup() %>%
arrange(n) %>%
write_csv("my_results.csv")


Output



# A tibble: 100 x 2
n interval
<int> <chr>
1 1 04:30-14:00, 19:30-20:15, 22:30-01:15, 03:45-...
2 2 06:15-08:00, 09:00-12:00, 13:45-16:30, 18:45-23:15, 00:30-02:15
3 3 06:00-06:30, 08:00-09:45, 11:15-13:30, 14:15-23:15, 01:00-01:30
4 4 20:00-21:15, 23:30-03:15
5 5 05:00-09:30, 10:00-10:30, 11:45-12:00, 13:15-13:30, 14:00-20:15, 20:30-21:3~
6 6 07:45-08:30, 09:15-13:15, 19:15-19:30, 20:30-20:45, 21:00-21:45, 01:45-...
7 7 09:30-17:45, 21:15-...
8 8 07:00-09:30, 12:45-18:00, 19:00-21:15, 00:15-02:00
9 9 05:45-06:15, 09:00-16:00, 17:15-19:45, 21:15-22:30, 23:00-...
10 10 10:00-10:15, 12:15-13:30, 16:15-16:45, 21:30-23:45, 00:45-01:30


Data



colnms <- paste(
str_pad(rep(c(4:23, 0:3), each = 4), 2, "left", 0),
str_pad(rep(c(0, 15, 30, 45), times = 24), 2, "left", 0),
sep = ":"
)

set.seed(53248604)
dat <- matrix(sample(c("-", 2), 9600, prob = c(0.9, 0.1), replace = T), nrow = 100)

dimnames(dat) <- list(1:100, colnms)

dat <- as.data.frame(dat)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 18:29

























answered Nov 11 at 14:11









utubun

1,1741711




1,1741711












  • Dear @utubun you are amazing is working; but I have a big request to you how to mine the interval that starts and ends with 2. As example example 3 it would be 04:00 and 05:00? Thank you
    – RforDummies
    Nov 11 at 14:30










  • Do you mean not to count the intervals where n('2') == 1 as done by @jay.sf? btw look at @jay.sf answer, we both can learn a lot from it.
    – utubun
    Nov 11 at 14:36






  • 1




    Dear @utubun what I mean to count the intervals not just the first and the last '2'; but also the 'spaces' or the intervals that make the case between then. For example let's assume that the table has "2" from 07:15 till 10:30 then there is a "-" at 11:00 there is another interval with "2" till 11:45 than there is "-" and at 12:30 start another "2" intervals till 14:45 This I am failing to understand how to code..Could you help me please?
    – RforDummies
    Nov 11 at 15:15












  • Ok, let me think about it
    – utubun
    Nov 11 at 15:16






  • 1




    Apologize for this late reply; true you need to return all the 2; so 07:15-10:30, 11:00-11:45, 12:30-14:45 and 22:45-thank you and apologize
    – RforDummies
    Nov 11 at 16:13


















  • Dear @utubun you are amazing is working; but I have a big request to you how to mine the interval that starts and ends with 2. As example example 3 it would be 04:00 and 05:00? Thank you
    – RforDummies
    Nov 11 at 14:30










  • Do you mean not to count the intervals where n('2') == 1 as done by @jay.sf? btw look at @jay.sf answer, we both can learn a lot from it.
    – utubun
    Nov 11 at 14:36






  • 1




    Dear @utubun what I mean to count the intervals not just the first and the last '2'; but also the 'spaces' or the intervals that make the case between then. For example let's assume that the table has "2" from 07:15 till 10:30 then there is a "-" at 11:00 there is another interval with "2" till 11:45 than there is "-" and at 12:30 start another "2" intervals till 14:45 This I am failing to understand how to code..Could you help me please?
    – RforDummies
    Nov 11 at 15:15












  • Ok, let me think about it
    – utubun
    Nov 11 at 15:16






  • 1




    Apologize for this late reply; true you need to return all the 2; so 07:15-10:30, 11:00-11:45, 12:30-14:45 and 22:45-thank you and apologize
    – RforDummies
    Nov 11 at 16:13
















Dear @utubun you are amazing is working; but I have a big request to you how to mine the interval that starts and ends with 2. As example example 3 it would be 04:00 and 05:00? Thank you
– RforDummies
Nov 11 at 14:30




Dear @utubun you are amazing is working; but I have a big request to you how to mine the interval that starts and ends with 2. As example example 3 it would be 04:00 and 05:00? Thank you
– RforDummies
Nov 11 at 14:30












Do you mean not to count the intervals where n('2') == 1 as done by @jay.sf? btw look at @jay.sf answer, we both can learn a lot from it.
– utubun
Nov 11 at 14:36




Do you mean not to count the intervals where n('2') == 1 as done by @jay.sf? btw look at @jay.sf answer, we both can learn a lot from it.
– utubun
Nov 11 at 14:36




1




1




Dear @utubun what I mean to count the intervals not just the first and the last '2'; but also the 'spaces' or the intervals that make the case between then. For example let's assume that the table has "2" from 07:15 till 10:30 then there is a "-" at 11:00 there is another interval with "2" till 11:45 than there is "-" and at 12:30 start another "2" intervals till 14:45 This I am failing to understand how to code..Could you help me please?
– RforDummies
Nov 11 at 15:15






Dear @utubun what I mean to count the intervals not just the first and the last '2'; but also the 'spaces' or the intervals that make the case between then. For example let's assume that the table has "2" from 07:15 till 10:30 then there is a "-" at 11:00 there is another interval with "2" till 11:45 than there is "-" and at 12:30 start another "2" intervals till 14:45 This I am failing to understand how to code..Could you help me please?
– RforDummies
Nov 11 at 15:15














Ok, let me think about it
– utubun
Nov 11 at 15:16




Ok, let me think about it
– utubun
Nov 11 at 15:16




1




1




Apologize for this late reply; true you need to return all the 2; so 07:15-10:30, 11:00-11:45, 12:30-14:45 and 22:45-thank you and apologize
– RforDummies
Nov 11 at 16:13




Apologize for this late reply; true you need to return all the 2; so 07:15-10:30, 11:00-11:45, 12:30-14:45 and 22:45-thank you and apologize
– RforDummies
Nov 11 at 16:13












up vote
1
down vote













You could work with range() like so:



fun <- function(dat) {
L <- lapply(seq_along(dat), function(x, ...) {
if (length(dat[x, dat[x, ] == 2, ]) >= 2) {
range(names(dat[x, dat[x, ] == 2]))
} else if (length(dat[x, dat[x, ] == 2, ]) == 1) {
c(names(dat)[which(dat[x, ] == 2)], NA)
}})
setNames(data.frame(do.call(rbind, L)), c("t0", "t1"))
}


Yields



> fun(df1)
t0 t1
1 04:00 04:30
2 04:00 04:45
3 04:30 <NA>


Data



df1 <- setNames(data.frame(matrix(c(rep("-", 4), rep(2, 3), "-", 
rep(c(2, rep("-", 2)), 2),2, rep("-", 25)),
ncol=4, byrow=TRUE)),
strftime(as.POSIXct((0:3)*15*60 + 3*60*60, origin=Sys.Date() ),
format="%H:%M"))

> df1
04:00 04:15 04:30 04:45
1 - - - -
2 2 2 2 -
3 2 - - 2
4 - - 2 -
5 - - - -
6 - - - -
7 - - - -
8 - - - -
9 - - - -
10 - - - -





share|improve this answer

















  • 1




    Cool answer, made my mental notes about your approach.
    – utubun
    Nov 11 at 14:44















up vote
1
down vote













You could work with range() like so:



fun <- function(dat) {
L <- lapply(seq_along(dat), function(x, ...) {
if (length(dat[x, dat[x, ] == 2, ]) >= 2) {
range(names(dat[x, dat[x, ] == 2]))
} else if (length(dat[x, dat[x, ] == 2, ]) == 1) {
c(names(dat)[which(dat[x, ] == 2)], NA)
}})
setNames(data.frame(do.call(rbind, L)), c("t0", "t1"))
}


Yields



> fun(df1)
t0 t1
1 04:00 04:30
2 04:00 04:45
3 04:30 <NA>


Data



df1 <- setNames(data.frame(matrix(c(rep("-", 4), rep(2, 3), "-", 
rep(c(2, rep("-", 2)), 2),2, rep("-", 25)),
ncol=4, byrow=TRUE)),
strftime(as.POSIXct((0:3)*15*60 + 3*60*60, origin=Sys.Date() ),
format="%H:%M"))

> df1
04:00 04:15 04:30 04:45
1 - - - -
2 2 2 2 -
3 2 - - 2
4 - - 2 -
5 - - - -
6 - - - -
7 - - - -
8 - - - -
9 - - - -
10 - - - -





share|improve this answer

















  • 1




    Cool answer, made my mental notes about your approach.
    – utubun
    Nov 11 at 14:44













up vote
1
down vote










up vote
1
down vote









You could work with range() like so:



fun <- function(dat) {
L <- lapply(seq_along(dat), function(x, ...) {
if (length(dat[x, dat[x, ] == 2, ]) >= 2) {
range(names(dat[x, dat[x, ] == 2]))
} else if (length(dat[x, dat[x, ] == 2, ]) == 1) {
c(names(dat)[which(dat[x, ] == 2)], NA)
}})
setNames(data.frame(do.call(rbind, L)), c("t0", "t1"))
}


Yields



> fun(df1)
t0 t1
1 04:00 04:30
2 04:00 04:45
3 04:30 <NA>


Data



df1 <- setNames(data.frame(matrix(c(rep("-", 4), rep(2, 3), "-", 
rep(c(2, rep("-", 2)), 2),2, rep("-", 25)),
ncol=4, byrow=TRUE)),
strftime(as.POSIXct((0:3)*15*60 + 3*60*60, origin=Sys.Date() ),
format="%H:%M"))

> df1
04:00 04:15 04:30 04:45
1 - - - -
2 2 2 2 -
3 2 - - 2
4 - - 2 -
5 - - - -
6 - - - -
7 - - - -
8 - - - -
9 - - - -
10 - - - -





share|improve this answer












You could work with range() like so:



fun <- function(dat) {
L <- lapply(seq_along(dat), function(x, ...) {
if (length(dat[x, dat[x, ] == 2, ]) >= 2) {
range(names(dat[x, dat[x, ] == 2]))
} else if (length(dat[x, dat[x, ] == 2, ]) == 1) {
c(names(dat)[which(dat[x, ] == 2)], NA)
}})
setNames(data.frame(do.call(rbind, L)), c("t0", "t1"))
}


Yields



> fun(df1)
t0 t1
1 04:00 04:30
2 04:00 04:45
3 04:30 <NA>


Data



df1 <- setNames(data.frame(matrix(c(rep("-", 4), rep(2, 3), "-", 
rep(c(2, rep("-", 2)), 2),2, rep("-", 25)),
ncol=4, byrow=TRUE)),
strftime(as.POSIXct((0:3)*15*60 + 3*60*60, origin=Sys.Date() ),
format="%H:%M"))

> df1
04:00 04:15 04:30 04:45
1 - - - -
2 2 2 2 -
3 2 - - 2
4 - - 2 -
5 - - - -
6 - - - -
7 - - - -
8 - - - -
9 - - - -
10 - - - -






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 14:24









jay.sf

4,12921435




4,12921435








  • 1




    Cool answer, made my mental notes about your approach.
    – utubun
    Nov 11 at 14:44














  • 1




    Cool answer, made my mental notes about your approach.
    – utubun
    Nov 11 at 14:44








1




1




Cool answer, made my mental notes about your approach.
– utubun
Nov 11 at 14:44




Cool answer, made my mental notes about your approach.
– utubun
Nov 11 at 14:44


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248604%2fhow-to-draw-specific-time-intervals-of-rows-from-a-data-frame%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation