{"id":9162,"date":"2022-09-17T12:25:46","date_gmt":"2022-09-17T06:55:46","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/"},"modified":"2022-09-17T12:25:46","modified_gmt":"2022-09-17T06:55:46","slug":"solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/","title":{"rendered":"[Solved] How to add a variable in a data frame using another variable for indices?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-37261952\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"37261952\" data-parentid=\"37261025\" data-score=\"2\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" 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>You need joins, and something like <code>tidyr::fill<\/code>:<\/p>\n<pre><code>library(dplyr)\nlibrary(tidyr)\n\nx %&gt;% mutate(slopezz = slopezz[1:n()]) %&gt;% \n    right_join(veh, by = c('psi' = 'Tim')) %&gt;% \n    fill(slopezz, .direction = 'up')\n#     psi slopezz\n# 1 169.7 -2.1920\n# 2 169.8 -2.1920\n# 3 169.9 -2.1920\n# 4 170.0 -2.1920\n# 5 170.1 -2.1920\n# 6 170.2 -2.1920\n# .   ...     ...\n<\/code><\/pre>\n<p>Note that this will leave the last four values as <code>NA<\/code> as you&#8217;re <code>fill<\/code>ing up. If you want to then <code>fill<\/code> down, just add on <code>%&gt;% fill(slopezz)<\/code>.<\/p>\n<hr>\n<h3>Data<\/h3>\n<pre><code>x &lt;- structure(list(psi = c(171.4, 171.8, 175.1, 175.7, 176.3, 177.8, \n               178.7, 180.1, 181.5, 182.4, 183.8, 184.8)), .Names = \"psi\", class = \"data.frame\", row.names = c(NA, -12L))\n\nslopezz &lt;- c(-2.192, 0.7034, 0.6113, -1.254, 0.7513, 2.325, 0.0791, -0.9713, \n             1.101, 1.949, -1.429, 2.25, 0.8775)\n\nveh &lt;- structure(list(Tim = c(169.7, 169.8, 169.9, 170, 170.1, 170.2, \n                 170.3, 170.4, 170.5, 170.6, 170.7, 170.8, 170.9, 171, 171.1, \n                 171.2, 171.3, 171.4, 171.5, 171.6, 171.7, 171.8, 171.9, 172, \n                 172.1, 172.2, 172.3, 172.4, 172.5, 172.6, 172.7, 172.8, 172.9,  \n                 173, 173.1, 173.2, 173.3, 173.4, 173.5, 173.6, 173.7, 173.8, \n                 173.9, 174, 174.1, 174.2, 174.3, 174.4, 174.5, 174.6, 174.7, \n                 174.8, 174.9, 175, 175.1, 175.2, 175.3, 175.4, 175.5, 175.6, \n                 175.7, 175.8, 175.9, 176, 176.1, 176.2, 176.3, 176.4, 176.5, \n                 176.6, 176.7, 176.8, 176.9, 177, 177.1, 177.2, 177.3, 177.4, \n                 177.5, 177.6, 177.7, 177.8, 177.9, 178, 178.1, 178.2, 178.3, \n                 178.4, 178.5, 178.6, 178.7, 178.8, 178.9, 179, 179.1, 179.2, \n                 179.3, 179.4, 179.5, 179.6, 179.7, 179.8, 179.9, 180, 180.1, \n                 180.2, 180.3, 180.4, 180.5, 180.6, 180.7, 180.8, 180.9, 181, \n                 181.1, 181.2, 181.3, 181.4, 181.5, 181.6, 181.7, 181.8, 181.9,  \n                 182, 182.1, 182.2, 182.3, 182.4, 182.5, 182.6, 182.7, 182.8, \n                 182.9, 183, 183.1, 183.2, 183.3, 183.4, 183.5, 183.6, 183.7, \n                 183.8, 183.9, 184, 184.1, 184.2, 184.3, 184.4, 184.5, 184.6, \n                 184.7, 184.8, 184.9, 185, 185.1, 185.2)), .Names = \"Tim\", row.names = c(NA, \n                 -156L), class = \"data.frame\")\n<\/code><\/pre>\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 How to add a variable in a data frame using another variable for indices? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You need joins, and something like tidyr::fill: library(dplyr) library(tidyr) x %&gt;% mutate(slopezz = slopezz[1:n()]) %&gt;% right_join(veh, by = c(&#8216;psi&#8217; = &#8216;Tim&#8217;)) %&gt;% fill(slopezz, .direction = &#8216;up&#8217;) # psi slopezz # 1 169.7 -2.1920 # 2 169.8 -2.1920 # 3 169.9 -2.1920 # 4 170.0 -2.1920 # 5 170.1 -2.1920 # 6 170.2 -2.1920 # &#8230; <a title=\"[Solved] How to add a variable in a data frame using another variable for indices?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/\" aria-label=\"More on [Solved] How to add a variable in a data frame using another variable for indices?\">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-9162","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 to add a variable in a data frame using another variable for indices? - 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-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to add a variable in a data frame using another variable for indices? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You need joins, and something like tidyr::fill: library(dplyr) library(tidyr) x %&gt;% mutate(slopezz = slopezz[1:n()]) %&gt;% right_join(veh, by = c(&#039;psi&#039; = &#039;Tim&#039;)) %&gt;% fill(slopezz, .direction = &#039;up&#039;) # psi slopezz # 1 169.7 -2.1920 # 2 169.8 -2.1920 # 3 169.9 -2.1920 # 4 170.0 -2.1920 # 5 170.1 -2.1920 # 6 170.2 -2.1920 # ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-17T06:55:46+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to add a variable in a data frame using another variable for indices?\",\"datePublished\":\"2022-09-17T06:55:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\\\/\"},\"wordCount\":63,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"r\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\\\/\",\"name\":\"[Solved] How to add a variable in a data frame using another variable for indices? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-09-17T06:55:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to add a variable in a data frame using another variable for indices?\"}]},{\"@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 to add a variable in a data frame using another variable for indices? - 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-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to add a variable in a data frame using another variable for indices? - JassWeb","og_description":"[ad_1] You need joins, and something like tidyr::fill: library(dplyr) library(tidyr) x %&gt;% mutate(slopezz = slopezz[1:n()]) %&gt;% right_join(veh, by = c('psi' = 'Tim')) %&gt;% fill(slopezz, .direction = 'up') # psi slopezz # 1 169.7 -2.1920 # 2 169.8 -2.1920 # 3 169.9 -2.1920 # 4 170.0 -2.1920 # 5 170.1 -2.1920 # 6 170.2 -2.1920 # ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/","og_site_name":"JassWeb","article_published_time":"2022-09-17T06:55:46+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to add a variable in a data frame using another variable for indices?","datePublished":"2022-09-17T06:55:46+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/"},"wordCount":63,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["r"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/","name":"[Solved] How to add a variable in a data frame using another variable for indices? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-17T06:55:46+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-add-a-variable-in-a-data-frame-using-another-variable-for-indices\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to add a variable in a data frame using another variable for indices?"}]},{"@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\/9162","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=9162"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/9162\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=9162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=9162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=9162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}