{"id":217893,"date":"2025-01-29T10:02:31","date_gmt":"2025-01-29T15:02:31","guid":{"rendered":"https:\/\/ibkrcampus.com\/campus\/?p=217893"},"modified":"2025-01-29T10:02:57","modified_gmt":"2025-01-29T15:02:57","slug":"r-code-snippet-transform-from-long-format-to-wide-format","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/","title":{"rendered":"R Code Snippet: Transform from Long Format to Wide Format"},"content":{"rendered":"\n<p><em>The article &#8220;R Code Snippet: Transform from Long Format to Wide Format&#8221; was originally posted on <a href=\"https:\/\/shleeai.blogspot.com\/2022\/07\/r-code-snippet-transform-from-long.html\">SHLee AI Financial Model<\/a>.<\/em><\/p>\n\n\n\n<p>This post introduces a simple R code snippet for\u00a0<strong>transforming the long format data to the wide format<\/strong>. We occasionally encounter the long format data such as yield curve data since it has two dimensions : maturity and time. For this end, we can use\u00a0<strong>reshape()<\/strong>\u00a0R built-in function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-long-and-wide-formats\">Long and Wide formats<\/h2>\n\n\n\n<p>What is the\u00a0<strong>long\u00a0<\/strong>and\u00a0<strong>wide format<\/strong>? A picture paints a thousand words.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"653\" height=\"875\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/long_to_wide_to_long-shlee.png\" alt=\"\" class=\"wp-image-217895 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/long_to_wide_to_long-shlee.png 653w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/long_to_wide_to_long-shlee-300x402.png 300w\" data-sizes=\"(max-width: 653px) 100vw, 653px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 653px; aspect-ratio: 653\/875;\" \/><\/figure>\n\n\n\n<p>We want to transform the long format data to the wide format data for a panel of time series analysis such as a term structure of interest rates. As financial data is usually extracted from database system, we occasionally encounter the long format data. For example, the data in the above figure is a sample of&nbsp;<strong>Euro area yield curve<\/strong>&nbsp;which has the long format. To facilitate an empirical analysis, the wide format is appropriate.<\/p>\n\n\n\n<p>Transforming between the long and wide format can be carried out by using&nbsp;<strong>reshape()<\/strong>&nbsp;R function. No further explanation is needed. Let&#8217;s see the R code below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">R code<\/h3>\n\n\n\n<p>The following R code reads sample data and transform the long format to the wide format and vice versa. When using the&nbsp;<strong>reshape()<\/strong>&nbsp;function, we need to set the&nbsp;<strong>direction argument<\/strong>&nbsp;as&nbsp;<strong>&#8220;long&#8221;<\/strong>&nbsp;or&nbsp;<strong>&#8220;wide&#8221;<\/strong>. In particular, we&nbsp;<strong>need to add new column name with some delimitator (., _, etc) to the wide format data when we transform it to the long format<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#========================================================#\n# Quantitative Financial Econometrics &amp; Derivatives \n# ML\/DL using R, Python, Tensorflow by Sang-Heon Lee \n#\n# https:\/\/shleeai.blogspot.com\n#--------------------------------------------------------#\n# Long to wide format and vice versa for yield data\n#========================================================#\n \ngraphics.off(); rm(list = ls())\n \n# sample data : ECB zero yields\nstr_data &lt;- \"term    date    rate\n            3M    2021-01-29    -0.625\n            3M    2021-02-26    -0.612\n            3M    2021-03-31    -0.636\n            3M    2021-04-30    -0.628\n            3M    2021-05-31    -0.632\n            3M    2021-06-30    -0.650\n            3M    2021-07-30    -0.663\n            3M    2021-08-31    -0.676\n            3M    2021-09-30    -0.712\n            3M    2021-10-29    -0.736\n            3M    2021-11-30    -0.895\n            3M    2021-12-31    -0.731\n            3Y    2021-01-29    -0.771\n            3Y    2021-02-26    -0.648\n            3Y    2021-03-31    -0.711\n            3Y    2021-04-30    -0.684\n            3Y    2021-05-31    -0.666\n            3Y    2021-06-30    -0.672\n            3Y    2021-07-30    -0.813\n            3Y    2021-08-31    -0.760\n            3Y    2021-09-30    -0.677\n            3Y    2021-10-29    -0.537\n            3Y    2021-11-30    -0.766\n            3Y    2021-12-31    -0.620\n            10Y    2021-01-29    -0.512\n            10Y    2021-02-26    -0.246\n            10Y    2021-03-31    -0.279\n            10Y    2021-04-30    -0.180\n            10Y    2021-05-31    -0.146\n            10Y    2021-06-30    -0.203\n            10Y    2021-07-30    -0.440\n            10Y    2021-08-31    -0.393\n            10Y    2021-09-30    -0.170\n            10Y    2021-10-29    -0.069\n            10Y    2021-11-30    -0.350\n            10Y    2021-12-31    -0.188\n            20Y    2021-01-29    -0.176\n            20Y    2021-02-26    0.103\n            20Y    2021-03-31    0.142\n            20Y    2021-04-30    0.252\n            20Y    2021-05-31    0.287\n            20Y    2021-06-30    0.201\n            20Y    2021-07-30    -0.059\n            20Y    2021-08-31    -0.033\n            20Y    2021-09-30    0.195\n            20Y    2021-10-29    0.103\n            20Y    2021-11-30    -0.115\n            20Y    2021-12-31    0.056\"\n \n#==========================================\n# Read a sample of ECB zero coupon yields\n#==========================================\ndf_long &lt;- read.table(text = str_data, header = TRUE)\n \n#==========================================\n# Transform LONG to WIDE format\n#==========================================\n \n# using \"wide\" option\ndf_wide &lt;- reshape(df_long, direction = \"wide\", \n                   idvar = \"date\", \n                   timevar = \"term\")\ndf_wide\n \n# initialize row names\nrownames(df_long) &lt;- NULL\n \n# delete a unnecessary prefix in column names\ncolnames(df_wide) &lt;- gsub(\"rate.\",\"\", colnames(df_wide))\ndf_wide\n \n#==========================================\n# Transform WIDE to LONG format\n#==========================================\ndf_wide2 &lt;- df_wide\n \n# need to add new column name as a prefix\ncolnames(df_wide2)[-1] &lt;- \n    paste0(\"term.\", colnames(df_wide)[-1])\n \n# using \"long\" option\ndf_long2 &lt;- reshape(df_wide2, direction = \"long\", \n                    idvar=\"date\", \n                    varying = colnames(df_wide2)[-1], \n                    sep = \".\")\n \n# initialize row names\nrownames(df_long2) &lt;- NULL\ndf_long2<\/pre>\n\n\n\n<p>Running the above R code produces the following\u00a0<strong>wide format of the yield curve data<\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"590\" height=\"722\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/long_to_wide-r-shlee.png\" alt=\"\" class=\"wp-image-217897 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/long_to_wide-r-shlee.png 590w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/long_to_wide-r-shlee-300x367.png 300w\" data-sizes=\"(max-width: 590px) 100vw, 590px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 590px; aspect-ratio: 590\/722;\" \/><\/figure>\n\n\n\n<p>We can also transform the wide format data to\u00a0<strong>the long format<\/strong>\u00a0conversely.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"510\" height=\"946\" data-src=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/wide_to_long-r-shlee.png\" alt=\"\" class=\"wp-image-217898 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/wide_to_long-r-shlee.png 510w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2025\/01\/wide_to_long-r-shlee-300x556.png 300w\" data-sizes=\"(max-width: 510px) 100vw, 510px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 510px; aspect-ratio: 510\/946;\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>This post introduces a simple R code snippet for transforming the long format data to the wide format.<\/p>\n","protected":false},"author":662,"featured_media":190524,"comment_status":"open","ping_status":"closed","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[339,343,338,341,342],"tags":[806,487],"contributors-categories":[13728],"class_list":{"0":"post-217893","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-ibkr-quant-news","10":"category-quant-development","11":"category-r-development","12":"tag-data-science","13":"tag-r","14":"contributors-categories-sh-fintech-modeling"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>R Code Snippet: Transform from Long Format to Wide Format<\/title>\n<meta name=\"description\" content=\"This post introduces a simple R code snippet for transforming the long format data to the wide format.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/217893\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"R Code Snippet: Transform from Long Format to Wide Format\" \/>\n<meta property=\"og:description\" content=\"This post introduces a simple R code snippet for transforming the long format data to the wide format.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-29T15:02:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-29T15:02:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-computer-teal-background.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"563\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sang-Heon Lee\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sang-Heon Lee\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Sang-Heon Lee\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/0a959ff9de7f0465a07baa1fe1ae0200\"\n\t            },\n\t            \"headline\": \"R Code Snippet: Transform from Long Format to Wide Format\",\n\t            \"datePublished\": \"2025-01-29T15:02:31+00:00\",\n\t            \"dateModified\": \"2025-01-29T15:02:57+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/\"\n\t            },\n\t            \"wordCount\": 301,\n\t            \"commentCount\": 0,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/05\\\/r-programming-computer-teal-background.jpg\",\n\t            \"keywords\": [\n\t                \"Data Science\",\n\t                \"R\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Quant\",\n\t                \"Quant Development\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"CommentAction\",\n\t                    \"name\": \"Comment\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/#respond\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/\",\n\t            \"name\": \"R Code Snippet: Transform from Long Format to Wide Format | IBKR Campus US\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/05\\\/r-programming-computer-teal-background.jpg\",\n\t            \"datePublished\": \"2025-01-29T15:02:31+00:00\",\n\t            \"dateModified\": \"2025-01-29T15:02:57+00:00\",\n\t            \"description\": \"This post introduces a simple R code snippet for transforming the long format data to the wide format.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/ibkr-quant-news\\\/r-code-snippet-transform-from-long-format-to-wide-format\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/05\\\/r-programming-computer-teal-background.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2023\\\/05\\\/r-programming-computer-teal-background.jpg\",\n\t            \"width\": 1000,\n\t            \"height\": 563,\n\t            \"caption\": \"R Programming\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/0a959ff9de7f0465a07baa1fe1ae0200\",\n\t            \"name\": \"Sang-Heon Lee\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/sang-heonlee\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"R Code Snippet: Transform from Long Format to Wide Format","description":"This post introduces a simple R code snippet for transforming the long format data to the wide format.","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:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/217893\/","og_locale":"en_US","og_type":"article","og_title":"R Code Snippet: Transform from Long Format to Wide Format","og_description":"This post introduces a simple R code snippet for transforming the long format data to the wide format.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/","og_site_name":"IBKR Campus US","article_published_time":"2025-01-29T15:02:31+00:00","article_modified_time":"2025-01-29T15:02:57+00:00","og_image":[{"width":1000,"height":563,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-computer-teal-background.jpg","type":"image\/jpeg"}],"author":"Sang-Heon Lee","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Sang-Heon Lee","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/#article","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/"},"author":{"name":"Sang-Heon Lee","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/0a959ff9de7f0465a07baa1fe1ae0200"},"headline":"R Code Snippet: Transform from Long Format to Wide Format","datePublished":"2025-01-29T15:02:31+00:00","dateModified":"2025-01-29T15:02:57+00:00","mainEntityOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/"},"wordCount":301,"commentCount":0,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-computer-teal-background.jpg","keywords":["Data Science","R"],"articleSection":["Data Science","Programming Languages","Quant","Quant Development","R Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/","url":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/","name":"R Code Snippet: Transform from Long Format to Wide Format | IBKR Campus US","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/#primaryimage"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-computer-teal-background.jpg","datePublished":"2025-01-29T15:02:31+00:00","dateModified":"2025-01-29T15:02:57+00:00","description":"This post introduces a simple R code snippet for transforming the long format data to the wide format.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/ibkr-quant-news\/r-code-snippet-transform-from-long-format-to-wide-format\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-computer-teal-background.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-computer-teal-background.jpg","width":1000,"height":563,"caption":"R Programming"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/0a959ff9de7f0465a07baa1fe1ae0200","name":"Sang-Heon Lee","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/sang-heonlee\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2023\/05\/r-programming-computer-teal-background.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/217893","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/662"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=217893"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/217893\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/190524"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=217893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=217893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=217893"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=217893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}