{"id":7669,"date":"2022-09-09T21:55:34","date_gmt":"2022-09-09T16:25:34","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/"},"modified":"2022-09-09T21:55:34","modified_gmt":"2022-09-09T16:25:34","slug":"solved-r-vectorizing-a-condition","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/","title":{"rendered":"[Solved] R: Vectorizing a condition"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-36146733\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"36146733\" data-parentid=\"36055195\" data-score=\"0\" 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>I finally came to the bottom of this, I figured I could create as many new column I wanted in order to calculate what I had to so that&#8217;s what I did. I think this is the best way to do it but am open to suggestions if there are other ways or even faster. <\/p>\n<p>Here is my code: <\/p>\n<pre><code>DF&lt;- read.csv(file = file,header = FALSE,sep = \"\", col.names = c(\"DateTime\",\"Seq\",\"BP1\",\"BQ1\",\"BO1\",\"AP1\",\"AQ1\",\"AO1\",\"BP2\",\"BQ2\",\"BO2\",\"AP2\",\"AQ2\",\"AO2\",\"BP3\",\"BQ3\",\"BO3\",\"AP3\",\"AQ3\",\"AO3\",\"BP4\",\"BQ4\",\"BO4\",\"AP4\",\"AQ4\",\"AO4\",\"BP5\",\"BQ5\",\"BO5\",\"AP5\",\"AQ5\",\"AO5\",\"BP6\",\"BQ6\",\"BO6\",\"AP6\",\"AQ6\",\"AO6\",\"BP7\",\"BQ7\",\"BO7\",\"AP7\",\"AQ7\",\"AO7\",\"BP8\",\"BQ8\",\"BO8\",\"AP8\",\"AQ8\",\"AO8\",\"BP9\",\"BQ9\",\"BO9\",\"AP9\",\"AQ9\",\"AO9\",\"BP10\",\"BQ10\",\"BO10\",\"AP10\",\"AQ10\",\"AO10\",\"C\",\"Price\",\"Qty\",\"OldPrice\",\"OldQty\"))\nDF&lt;- DF[which(DF$DateTime != 0),]\noptions(digits.secs = 3)\nDF$DateTime= as.POSIXct(DF$DateTime\/(10^9), origin=\"1970-01-01\", tz = \"GMT\")    #timestamp conversion\n\nsource('~\/R\/mywhich.R')\nsource('~\/nwhich.R')\n\n#matching with same line for all \nDF$change &lt;- apply(DF[, 1:62] == DF[,64], 1, mywhich)\n\n#matching \"C\" with previous line \nDF$change[DF[,63] == \"C\"] &lt;- apply(DF[which(DF[,63] == \"C\") - 1, 1:62] == DF[DF[,63] == \"C\",64], 1, mywhich)*(-1)                                                   \n\n#matching old price with previous line in \"M\"                                                \npos2 &lt;- apply(DF[which(DF[,63] == \"M\") - 1, 1:62] == DF[DF[,63] == \"M\",66], 1, mywhich)                                                 \n\n#subracting the two position in \"M\"\nDF$change[DF[,63] == \"M\"] &lt;- DF$change[DF[,63] == \"M\"] - pos2\n\n# arbitrary number to create side\nDF$side &lt;- 1000\n\nDF$side[DF[,63] == \"M\" &amp; DF[,68] == 0] &lt;- apply(DF[which(DF[,63] == \"M\"), 1:62] == DF[DF[,63] == \"M\",64], 1, nwhich)%%2    #check this -- erroneous for modifications that have happend outside level 5 -- might have to add another column for this\n#DF$side[DF[,63] == \"M\" &amp; DF[,68] != 0] &lt;- DF$change[DF[,63] == \"M\" &amp; DF[,68] != 0]  \n#DF$side[DF[,63] == \"N\"] &lt;- DF$change[DF[,63] == \"N\"]\n#DF$side[DF[,63] == \"C\"] &lt;- DF$change[DF[,63] == \"C\"]\nDF$diff &lt;- 0\n#price difference\nDF$diff[DF[,63] == \"M\" &amp; DF[,68] == 0] &lt;-  DF$OldPrice[DF[,63] == \"M\" &amp; DF[,68] == 0] - DF$Price[DF[,63] == \"M\" &amp; DF[,68] == 0]     \n#askside -- price increase\nDF$modify[DF[,69] == 0 &amp; DF[,70] &gt; 0] &lt;- -1\n#askside -- price decrease\nDF$modify[DF[,69] == 0 &amp; DF[,70] &lt; 0] &lt;- 1\n#bidside -- price decrease\nDF$modify[DF[,69] == 1 &amp; DF[,70] &lt; 0] &lt;- -1\n#bidside -- price increase\nDF$modify[DF[,69] == 1 &amp; DF[,70] &gt; 0] &lt;- 1\n#copying change to modify\nDF$modify[DF[,63] == \"N\"] &lt;- DF$change[DF[,63] == \"N\"]\nDF$modify[DF[,63] == \"C\"] &lt;- DF$change[DF[,63] == \"C\"]\nDF$modify[DF[,63] == \"M\" &amp; DF[,68] != 0] &lt;- DF$change[DF[,63] == \"M\" &amp; DF[,68] != 0]\n\ndf = data.frame(Time=DF$DateTime, Modify = DF$modify)\nfinalxts &lt;- as.xts(x = df$Modify, order.by = df$Time)\n#finalxts &lt;- aggregatets(finalxts, FUN = \"sum\", on = \"minutes\", k = 1, dropna = TRUE)\nfinalxts\n<\/code><\/pre>\n<p>Thank you for the help everyone. <\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved R: Vectorizing a condition <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I finally came to the bottom of this, I figured I could create as many new column I wanted in order to calculate what I had to so that&#8217;s what I did. I think this is the best way to do it but am open to suggestions if there are other ways or even &#8230; <a title=\"[Solved] R: Vectorizing a condition\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/\" aria-label=\"More on [Solved] R: Vectorizing a condition\">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":[593,321,2142],"class_list":["post-7669","post","type-post","status-publish","format-standard","hentry","category-solved","tag-for-loop","tag-r","tag-vectorization"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] R: Vectorizing a condition - 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-r-vectorizing-a-condition\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] R: Vectorizing a condition - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I finally came to the bottom of this, I figured I could create as many new column I wanted in order to calculate what I had to so that&#8217;s what I did. I think this is the best way to do it but am open to suggestions if there are other ways or even ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-09T16:25:34+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-r-vectorizing-a-condition\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] R: Vectorizing a condition\",\"datePublished\":\"2022-09-09T16:25:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/\"},\"wordCount\":78,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"for-loop\",\"r\",\"vectorization\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/\",\"name\":\"[Solved] R: Vectorizing a condition - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-09T16:25:34+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] R: Vectorizing a condition\"}]},{\"@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\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] R: Vectorizing a condition - 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-r-vectorizing-a-condition\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] R: Vectorizing a condition - JassWeb","og_description":"[ad_1] I finally came to the bottom of this, I figured I could create as many new column I wanted in order to calculate what I had to so that&#8217;s what I did. I think this is the best way to do it but am open to suggestions if there are other ways or even ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/","og_site_name":"JassWeb","article_published_time":"2022-09-09T16:25:34+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-r-vectorizing-a-condition\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] R: Vectorizing a condition","datePublished":"2022-09-09T16:25:34+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/"},"wordCount":78,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["for-loop","r","vectorization"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/","url":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/","name":"[Solved] R: Vectorizing a condition - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-09T16:25:34+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-r-vectorizing-a-condition\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] R: Vectorizing a condition"}]},{"@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\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1776403586","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\/7669","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=7669"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/7669\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=7669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=7669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=7669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}