{"id":14993,"date":"2022-10-10T00:36:51","date_gmt":"2022-10-09T19:06:51","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/"},"modified":"2022-10-10T00:36:51","modified_gmt":"2022-10-09T19:06:51","slug":"solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/","title":{"rendered":"[Solved] How I can find out 1st and last observation with in group in R for every by group"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-28007037\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"28007037\" data-parentid=\"28006813\" data-score=\"3\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Here is an option with <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/Rdatatable\/data.table\/\"><code>data.table_1.9.5<\/code><\/a>.  Create the &#8220;data.table&#8221; from &#8220;data.frame&#8221; using <code>setDT<\/code>, remove the <code>NA<\/code> values in &#8220;dialled&#8221; column (<code>!is.na(dialled)<\/code>), generate grouping variable by using <code>rleid<\/code> on &#8220;Dialled_nbr&#8221;, get the row index of the first and last rows for the levels of grouping variable (<code>.I(c(1L, .N)]<\/code>), finally subset the &#8220;dt1&#8221; based on the row index.<\/p>\n<pre><code>library(data.table)\ndt1 &lt;- setDT(df)[!is.na(dialled)]\ndt1[dt1[,.I[c(1L, .N)],rleid(dialled)]$V1]\n#    dialled Ringing    state duration\n#1:     123      NA       NA        0\n#2:     123      NA       NA       60\n#3:     222      NA inactive        0\n#4:     222      NA inactive       37\n#5:     123      NA inactive        0\n#6:     123      NA   active       60\n<\/code><\/pre>\n<p>Or using <code>base R<\/code><\/p>\n<pre><code>df1 &lt;- df[!is.na(df$dialled),]\ngrp&lt;-  inverse.rle(within.list(rle(df1$dialled), \n                    values &lt;- seq_along(values)))\n\ndf1[!duplicated(grp)|!duplicated(grp,fromLast=TRUE),]\n#    dialled Ringing    state duration\n#5      123      NA     &lt;NA&gt;        0\n#8      123      NA     &lt;NA&gt;       60\n#19     222      NA inactive        0\n#21     222      NA inactive       37\n#25     123      NA inactive        0\n#27     123      NA   active       60\n<\/code><\/pre>\n<h3>Update<\/h3>\n<p>Based on the new dataset, <\/p>\n<pre><code>grp &lt;- cumsum(c(TRUE,df$duration[-nrow(df)]!=0))\ndf[!duplicated(grp)|!duplicated(grp,fromLast=TRUE),]\n#   dialled Ringing    state duration\n#1      123      NA     &lt;NA&gt;        0\n#3      123      NA     &lt;NA&gt;       60\n#4      123      NA     &lt;NA&gt;        0\n#6      123      NA     &lt;NA&gt;       70\n#7      222      NA inactive        0\n#9      222      NA inactive       37\n#10     123      NA inactive        0\n#12     123      NA   active       60\n<\/code><\/pre>\n<h3>data<\/h3>\n<pre><code> df &lt;- structure(list(dialled = c(NA, NA, NA, NA, 123L, 123L, 123L, \n 123L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 222L, 222L, 222L, \n NA, NA, NA, 123L, 123L, 123L, NA), Ringing = c(NA, NA, NA, NA, \n NA, NA, NA, NA, NA, NA, NA, NA, 145L, 145L, 145L, NA, NA, NA, \n NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), state = c(NA, NA, NA, \n NA, NA, NA, NA, NA, \"active\", \"active\", \"inactive\", \"inactive\", \n \"inactive\", \"inactive\", \"inactive\", \"active\", \"active\", \"inactive\", \n \"inactive\", \"inactive\", \"inactive\", \"active\", \"active\", \"inactive\", \n \"inactive\", \"inactive\", \"active\", \"active\"), duration = c(0L, \n 0L, 0L, 0L, 0L, 0L, 0L, 60L, 0L, 0L, 0L, 0L, 0L, 0L, 56L, 0L, \n 0L, 0L, 0L, 0L, 37L, 0L, 0L, 0L, 0L, 0L, 60L, 0L)), .Names = \n c(\"dialled\", \"Ringing\", \"state\", \"duration\"), class = \"data.frame\", \n row.names = c(NA, -28L))\n<\/code><\/pre>\n<h3>newdata<\/h3>\n<pre><code> df &lt;- structure(list(dialled = c(123L, 123L, 123L, 123L, 123L, 123L, \n 222L, 222L, 222L, 123L, 123L, 123L), Ringing = c(NA, NA, NA, \n NA, NA, NA, NA, NA, NA, NA, NA, NA), state = c(NA, NA, NA, NA, \n NA, NA, \"inactive\", \"inactive\", \"inactive\", \"inactive\", \"inactive\", \n \"active\"), duration = c(0L, 0L, 60L, 0L, 0L, 70L, 0L, 0L, 37L, \n 0L, 0L, 60L)), .Names = c(\"dialled\", \"Ringing\", \"state\", \"duration\"\n ), class = \"data.frame\", row.names = c(NA, -12L))\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How I can find out 1st and last observation with in group in R for every by group <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Here is an option with data.table_1.9.5. Create the &#8220;data.table&#8221; from &#8220;data.frame&#8221; using setDT, remove the NA values in &#8220;dialled&#8221; column (!is.na(dialled)), generate grouping variable by using rleid on &#8220;Dialled_nbr&#8221;, get the row index of the first and last rows for the levels of grouping variable (.I(c(1L, .N)]), finally subset the &#8220;dt1&#8221; based on the &#8230; <a title=\"[Solved] How I can find out 1st and last observation with in group in R for every by group\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/\" aria-label=\"More on [Solved] How I can find out 1st and last observation with in group in R for every by group\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[321],"class_list":["post-14993","post","type-post","status-publish","format-standard","hentry","category-solved","tag-r"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] How I can find out 1st and last observation with in group in R for every by group - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How I can find out 1st and last observation with in group in R for every by group - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Here is an option with data.table_1.9.5. Create the &#8220;data.table&#8221; from &#8220;data.frame&#8221; using setDT, remove the NA values in &#8220;dialled&#8221; column (!is.na(dialled)), generate grouping variable by using rleid on &#8220;Dialled_nbr&#8221;, get the row index of the first and last rows for the levels of grouping variable (.I(c(1L, .N)]), finally subset the &#8220;dt1&#8221; based on the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-09T19:06:51+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How I can find out 1st and last observation with in group in R for every by group\",\"datePublished\":\"2022-10-09T19:06:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\\\/\"},\"wordCount\":102,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"r\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\\\/\",\"name\":\"[Solved] How I can find out 1st and last observation with in group in R for every by group - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-10-09T19:06:51+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How I can find out 1st and last observation with in group in R for every by group\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How I can find out 1st and last observation with in group in R for every by group - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How I can find out 1st and last observation with in group in R for every by group - JassWeb","og_description":"[ad_1] Here is an option with data.table_1.9.5. Create the &#8220;data.table&#8221; from &#8220;data.frame&#8221; using setDT, remove the NA values in &#8220;dialled&#8221; column (!is.na(dialled)), generate grouping variable by using rleid on &#8220;Dialled_nbr&#8221;, get the row index of the first and last rows for the levels of grouping variable (.I(c(1L, .N)]), finally subset the &#8220;dt1&#8221; based on the ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/","og_site_name":"JassWeb","article_published_time":"2022-10-09T19:06:51+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How I can find out 1st and last observation with in group in R for every by group","datePublished":"2022-10-09T19:06:51+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/"},"wordCount":102,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["r"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/","url":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/","name":"[Solved] How I can find out 1st and last observation with in group in R for every by group - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-09T19:06:51+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-i-can-find-out-1st-and-last-observation-with-in-group-in-r-for-every-by-group\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How I can find out 1st and last observation with in group in R for every by group"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=14993"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14993\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=14993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=14993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=14993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}