{"id":76291,"date":"2020-04-01T13:42:28","date_gmt":"2020-04-01T11:42:28","guid":{"rendered":"https:\/\/prohoster.info\/blog\/administrirovanie\/r-paket-tidyr-i-ego-novye-funkczii-pivot_longer-i-pivot_wider"},"modified":"2020-04-01T13:42:28","modified_gmt":"2020-04-01T11:42:28","slug":"r-paket-tidyr-i-ego-novye-funkczii-pivot_longer-i-pivot_wider","status":"publish","type":"post","link":"https:\/\/prohoster.info\/sq\/blog\/administrirovanie\/r-paket-tidyr-i-ego-novye-funkczii-pivot_longer-i-pivot_wider","title":{"rendered":"Paketa R tidyr dhe funksionet e saj t\u00eb reja pivot_longer dhe pivot_wider","gt_translate_keys":[{"key":"rendered","format":"text"}]},"content":{"rendered":"<p>Paketa <strong>tidyr<\/strong> \u00ebsht\u00eb pjes\u00eb e baz\u00ebs s\u00eb nj\u00eb prej bibliotekeve m\u00eb t\u00eb njohura n\u00eb gjuh\u00ebn R \u2014 <strong>tidyverse<\/strong>.<br \/>\nQ\u00ebllimi kryesor i paket\u00ebs \u00ebsht\u00eb t\u00eb sjell\u00eb t\u00eb dh\u00ebnat n\u00eb nj\u00eb format t\u00eb rregullt.<\/p>\n<p><\/p>\n<p>N\u00eb Habr\u00eb tashm\u00eb ekziston <noindex><a rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/248741\/\">shkencor<\/a><\/noindex> nj\u00eb artikull i kushtuar k\u00ebsaj pakete, por i datuar n\u00eb vitin 2015. Un\u00eb dua t\u00eb flas p\u00ebr ndryshimet m\u00eb aktuale, t\u00eb cilat disa dit\u00eb m\u00eb par\u00eb i shpalli autori i saj, Hadley Wickham.<\/p>\n<p>\n<img decoding=\"async\" alt=\"Paketa R tidyr dhe funksionet e saj t\u00eb reja pivot_longer dhe pivot_wider\" src=\"\/wp-content\/uploads\/2020\/04\/a290263b63aaea3db70e83a1ec411415.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<blockquote><p><b>SJK<\/b>: A do t\u00eb konsiderohen funksionet gather() dhe spread() t\u00eb vjetra?<\/p>\n<p><b>Hadley Wickham<\/b>: N\u00eb nj\u00eb far\u00eb mase. Ne do t\u00eb ndalojm\u00eb rekomandimin e p\u00ebrdorimit t\u00eb k\u00ebtyre funksioneve dhe do t\u00eb ndalojm\u00eb rregullimet e tyre, por ato do t\u00eb mbeten n\u00eb paket\u00eb n\u00eb gjendjen aktuale.<\/p><\/blockquote>\n<p><noindex><a rel=\"nofollow\" name=\"habracut\"><\/a><\/noindex><\/p>\n<h2 id=\"soderzhanie\">P\u00ebrmbajtja<\/h2>\n<p><\/p>\n<p><em>N\u00ebse jeni t\u00eb interesuar p\u00ebr analiz\u00ebn e t\u00eb dh\u00ebnave, ndoshta do t'ju interesojn\u00eb kanalet e mia n\u00eb <noindex><a rel=\"nofollow\" href=\"https:\/\/t.me\/R4marketing\">telegram<\/a><\/noindex> dhe <noindex><a rel=\"nofollow\" href=\"https:\/\/www.youtube.com\/R4marketing\/?sub_confirmation=1\">youtube<\/a><\/noindex> Gati t\u00eb gjitha p\u00ebrmbajtjet e tyre i kushtohen gjuh\u00ebs R.<\/em><\/p>\n<p><\/p>\n<ul>\n<li><noindex><a rel=\"nofollow\" href=\"#koncepciya-tidydata\">Koncepsioni TidyData<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#osnovnye-funkcii-vhodyaschie-v-paket-tydir\">Funksionet kryesore q\u00eb p\u00ebrfshihen n\u00eb paket\u00ebn tidyr<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#novaya-koncepciya-preobrazovaniya-dannyh-iz-shirokogo-formata-v-dlinnyy-i-naoborot\">Nj\u00eb koncept i ri p\u00ebr transformimin e t\u00eb dh\u00ebnave nga formati i gjer\u00eb n\u00eb t\u00eb gjat\u00eb dhe anasjelltas<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"http:\/\/ustnovka-naibolee-aktualnoy-versii-tidyr-0839000\">Instalimi i versionit m\u00eb t\u00eb fundit t\u00eb tidyr 0.8.3.9000<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#perehod-na-novye-funkcii\">Kalimi n\u00eb funksione t\u00eb reja<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#prostoy-primer-preobrazovaniya-dannyh-iz-shirokogo-formata-v-dlinnyy\">Nj\u00eb shembull i thjesht\u00eb i transformimit t\u00eb t\u00eb dh\u00ebnave nga formati i gjer\u00eb n\u00eb t\u00eb gjat\u00eb<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#specifikacii\">Specifikimet<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#specifikaciya-s-ispolzovaniem-neskolkih-znacheniyvalue\">Specifikimi me p\u00ebrdorimin e disa vlerave (.value)<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#preobrazovanie-data-freymov-iz-dlinnogo-formata-k-shirokomu\">Transformimi i frame-ve t\u00eb t\u00eb dh\u00ebnave nga formati i gjat\u00eb n\u00eb t\u00eb gjer\u00eb<\/a><\/noindex>\n<ul>\n<li><noindex><a rel=\"nofollow\" href=\"#prosteyshiy-primer-privedeniya-tablicy-k-shirokomu-formatu\">Nj\u00eb shembull shum\u00eb i thjesht\u00eb i sjelljes s\u00eb tabel\u00ebs n\u00eb nj\u00eb format t\u00eb gjer\u00eb<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#generaciya-imeni-stolbca-iz-neskolkih-ishodnyh-peremennyh\">Generimi i emrit t\u00eb kolon\u00ebs nga disa variabla burimor\u00eb<\/a><\/noindex><\/li>\n<\/ul>\n<\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#neskolko-prodvinutyh-primerov-raboty-s-novoy-koncepciey-tidyr\">Disa shembuj m\u00eb t\u00eb avancuar t\u00eb pun\u00ebs me konceptin e ri tidyr<\/a><\/noindex>\n<ul>\n<li><noindex><a rel=\"nofollow\" href=\"#privedenie-dannyh-k-akkuratnomu-vidu-na-primere-nabora-dannyh-o-perepisi-dohoda-i-arendnoy-platy-v-ssha\">Kozhime t\u00eb dh\u00ebnash n\u00eb nj\u00eb format t\u00eb rregullt me shembullin e nj\u00eb seti t\u00eb dh\u00ebnash mbi t\u00eb ardhurat dhe qirat\u00eb n\u00eb SHBA<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#vsemirnyy-bank\">Banka Bot\u00ebrore<\/a><\/noindex><\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#spisok-kontaktov\">Lista e kontakteve<\/a><\/noindex><\/li>\n<\/ul>\n<\/li>\n<li><noindex><a rel=\"nofollow\" href=\"#zaklyuchenie\">P\u00ebrfundim<\/a><\/noindex><\/li>\n<\/ul>\n<p><\/p>\n<h2 id=\"koncepciya-tidydata\">Koncepsioni TidyData<\/h2>\n<p><\/p>\n<p>Q\u00ebllimi <strong>tidyr<\/strong> \u2014 p\u00ebr t'ju ndihmuar t\u00eb sillni t\u00eb dh\u00ebnat n\u00eb at\u00eb q\u00eb quhet format i rregullt. T\u00eb dh\u00ebnat e rregullta jan\u00eb t\u00eb dh\u00ebna ku: <\/p>\n<p><\/p>\n<ul>\n<li>\u00c7do variabl\u00eb \u00ebsht\u00eb n\u00eb nj\u00eb kolon\u00eb. <\/li>\n<li>\u00c7do v\u00ebzhgim \u00ebsht\u00eb nj\u00eb rresht. <\/li>\n<li>\u00c7do vler\u00eb \u00ebsht\u00eb nj\u00eb qeliza.<\/li>\n<\/ul>\n<p><\/p>\n<p>Me t\u00eb dh\u00ebnat q\u00eb jan\u00eb t\u00eb rregullta \u00ebsht\u00eb shum\u00eb m\u00eb e leht\u00eb dhe m\u00eb e p\u00ebrshtatshme t\u00eb punoni gjat\u00eb analiz\u00ebs.<\/p>\n<p><\/p>\n<h2 id=\"osnovnye-funkcii-vhodyaschie-v-paket-tidyr\">Funksionet kryesore q\u00eb p\u00ebrfshihen n\u00eb paket\u00ebn tidyr<\/h2>\n<p><\/p>\n<p>tidyr p\u00ebrmban nj\u00eb grup funksionesh t\u00eb dedikuara p\u00ebr transformimin e tabelave:<\/p>\n<p><\/p>\n<ul>\n<li><code>fill()<\/code> \u2014 mbushja e vlerave t\u00eb humbura n\u00eb nj\u00eb kolon\u00eb me vlerat e m\u00ebparshme;<\/li>\n<li><code>separate()<\/code> \u2014 ndan nj\u00eb fush\u00eb n\u00eb disa p\u00ebrmes nj\u00eb ndar\u00ebsi;<\/li>\n<li><code>unite()<\/code> \u2014 realizon operacionin e bashkimit t\u00eb disa fushave n\u00eb nj\u00eb, veprimi i kund\u00ebrt i funksionit <code>separate()<\/code>;<\/li>\n<li><code>pivot_longer()<\/code> \u2014 funksioni q\u00eb transformon t\u00eb dh\u00ebnat nga formati i gjer\u00eb n\u00eb t\u00eb gjat\u00eb;<\/li>\n<li><code>pivot_wider()<\/code> \u2014 funksioni q\u00eb transformon t\u00eb dh\u00ebnat nga formati i gjat\u00eb n\u00eb t\u00eb gjer\u00eb. Operacioni i kund\u00ebrt i asaj q\u00eb realizon funksioni <code>pivot_longer()<\/code>.<\/li>\n<li><code>gather()<\/code><strong>i vjet\u00ebr<\/strong> \u2014 funksioni q\u00eb transformon t\u00eb dh\u00ebnat nga formati i gjer\u00eb n\u00eb t\u00eb gjat\u00eb;<\/li>\n<li><code>spread()<\/code><strong>i vjet\u00ebr<\/strong> \u2014 funksioni q\u00eb transformon t\u00eb dh\u00ebnat nga formati i gjat\u00eb n\u00eb t\u00eb gjer\u00eb. Operacioni i kund\u00ebrt i asaj q\u00eb realizon funksioni <code>gather()<\/code>.<\/li>\n<\/ul>\n<p><\/p>\n<h2 id=\"novaya-koncepciya-preobrazovaniya-dannyh-iz-shirokogo-formata-v-dlinnyy-i-naoborot\">Nj\u00eb koncept i ri p\u00ebr transformimin e t\u00eb dh\u00ebnave nga formati i gjer\u00eb n\u00eb t\u00eb gjat\u00eb dhe anasjelltas<\/h2>\n<p><\/p>\n<p>M\u00eb par\u00eb, p\u00ebr k\u00ebt\u00eb lloj transformimi ishin p\u00ebrdorur funksionet <code>gather()<\/code> dhe <code>spread()<\/code>Gjat\u00eb viteve t\u00eb ekzistenc\u00ebs s\u00eb k\u00ebtyre funksioneve, ka b\u00ebr\u00eb t\u00eb qart\u00eb se p\u00ebr shumic\u00ebn e p\u00ebrdoruesve, p\u00ebrfshir\u00eb autorin e paket\u00ebs, emrat e k\u00ebtyre funksioneve dhe argumentet e tyre kan\u00eb qen\u00eb mjaft t\u00eb paqarta dhe kan\u00eb shkaktuar v\u00ebshtir\u00ebsi n\u00eb gjetjen dhe kuptimin e asaj se cili nga k\u00ebto funksione konverton frame t\u00eb dh\u00ebnash nga formati i gjer\u00eb n\u00eb formatin e gjat\u00eb dhe anasjelltas.<\/p>\n<p><\/p>\n<p>P\u00ebr k\u00ebt\u00eb arsye, <strong>tidyr<\/strong> jan\u00eb shtuar dy funksione t\u00eb reja dhe t\u00eb r\u00ebnd\u00ebsishme t\u00eb cilat jan\u00eb t\u00eb destinuara p\u00ebr transformimin e frame-ve t\u00eb dh\u00ebnash.<\/p>\n<p><\/p>\n<p>Funksionet e reja <code>pivot_longer()<\/code> dhe <code>pivot_wider()<\/code> jan\u00eb krijuar n\u00ebn ndikimin e disa funksioneve nga paketa <strong>cdata<\/strong>, krijuar nga John Mount dhe Nina Zumel.<\/p>\n<p><\/p>\n<h3 id=\"ustanovka-naibolee-aktualnoy-versii-tidyr-0839000\">Instalimi i versionit m\u00eb t\u00eb fundit t\u00eb tidyr 0.8.3.9000<\/h3>\n<p><\/p>\n<p>P\u00ebr t\u00eb instaluar versionin m\u00eb t\u00eb ri t\u00eb paket\u00ebs <strong>tidyr<\/strong> <em>0.8.3.9000<\/em>, n\u00eb t\u00eb cil\u00ebn jan\u00eb t\u00eb disponueshme funksionet e reja, p\u00ebrdorni kodin n\u00eb vazhdim.<\/p>\n<p><\/p>\n<p><code>devtools::install_github(\"tidyverse\/tidyr\")<\/code><\/p>\n<p><\/p>\n<p>N\u00eb momentin e shk writinges s\u00eb k\u00ebtij artikulli, k\u00ebto funksione jan\u00eb t\u00eb disponueshme vet\u00ebm n\u00eb versionin dev t\u00eb paket\u00ebs n\u00eb GitHub.<\/p>\n<p><\/p>\n<h3 id=\"perehod-na-novye-funkcii\">Kalimi n\u00eb funksione t\u00eb reja<\/h3>\n<p><\/p>\n<p>N\u00eb t\u00eb v\u00ebrtet\u00eb, p\u00ebr t\u00eb p\u00ebrkthyer skenar\u00ebt e vjet\u00ebr p\u00ebr t'i b\u00ebr\u00eb ata t\u00eb punojn\u00eb me funksionet e reja nuk \u00ebsht\u00eb e v\u00ebshtir\u00eb; p\u00ebr nj\u00eb kuptim t\u00eb m\u00ebtejsh\u00ebm, do t\u00eb marr nj\u00eb shembull nga dokumentacioni i funksioneve t\u00eb vjetra dhe do t\u00eb tregoj si k\u00ebto operacione kryhen me ndihm\u00ebn e funksioneve t\u00eb reja <code>pivot_*()<\/code> funksioneve.<\/p>\n<p><\/p>\n<p>Transformimi i formatit t\u00eb gjer\u00eb n\u00eb t\u00eb gjat\u00eb.<\/p>\n<p>\n<b class=\"spoiler_title\">Shembulli i kodit nga dokumentacioni i funksionit gather<\/b><\/p>\n<pre><code class=\"plaintext\"># example\nlibrary(dplyr)\nstocks &lt;- data.frame(\n  time = as.Date('2009-01-01') + 0:9,\n  X = rnorm(10, 0, 1),\n  Y = rnorm(10, 0, 2),\n  Z = rnorm(10, 0, 4)\n)\n\n# old\nstocks_gather &lt;- stocks %&gt;% gather(key   = stock, \n                                   value = price, \n                                   -time)\n\n# new\nstocks_long   &lt;- stocks %&gt;% pivot_longer(cols      = -time, \n                                       names_to  = &quot;stock&quot;, \n                                       values_to = &quot;price&quot;)\n<\/code><\/pre>\n<p><\/p>\n<p>Transformimi i formatit t\u00eb gjat\u00eb n\u00eb t\u00eb gjer\u00eb.<\/p>\n<p>\n<b class=\"spoiler_title\">Shembulli i kodit nga dokumentacioni i funksionit spread<\/b><\/p>\n<pre><code class=\"plaintext\"># old\nstocks_spread &lt;- stocks_gather %&gt;% spread(key = stock, \n                                          value = price) \n\n# new \nstock_wide    &lt;- stocks_long %&gt;% pivot_wider(names_from  = &quot;stock&quot;,\n                                            values_from = &quot;price&quot;)\n<\/code><\/pre>\n<p><\/p>\n<p>Duke qen\u00eb se n\u00eb shembujt e sip\u00ebrp\u00ebrmendur t\u00eb pun\u00ebs me <code>pivot_longer()<\/code> dhe <code>pivot_wider()<\/code>, n\u00eb tabel\u00ebn fillestare <em>stocks<\/em> nuk ka kolona t\u00eb listuara n\u00eb argumentet <em>names_to<\/em> dhe <em>values_to<\/em> emrat e tyre duhet t\u00eb jepen n\u00eb cita.<\/p>\n<p><\/p>\n<p>Tabela n\u00ebp\u00ebrmjet s\u00eb cil\u00ebs do t'ju jet\u00eb m\u00eb e thjesht\u00eb t\u00eb kuptoni se si t\u00eb kaloni n\u00eb pun\u00ebn me konceptin e ri <strong>tidyr<\/strong>.<\/p>\n<p><\/p>\n<p><img decoding=\"async\" alt=\"Paketa R tidyr dhe funksionet e saj t\u00eb reja pivot_longer dhe pivot_wider\" src=\"\/wp-content\/uploads\/2020\/04\/fe598b5cd152088a2f1b23b846d212e9.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<p><\/p>\n<h2 id=\"primechanie-ot-avtora\">Sh\u00ebnim nga autori<\/h2>\n<p><\/p>\n<blockquote><p>I gjith\u00eb teksti i dh\u00ebn\u00eb m\u00eb posht\u00eb \u00ebsht\u00eb nj\u00eb p\u00ebrkthim adaptiv, madje do t\u00eb thosha nj\u00eb p\u00ebrkthim t\u00eb lir\u00eb <noindex><a rel=\"nofollow\" href=\"https:\/\/tidyr.tidyverse.org\/dev\/articles\/pivot.html\">vignettes<\/a><\/noindex> nga faqja zyrtare e bibliotek\u00ebs tidyverse.<\/p><\/blockquote>\n<p><\/p>\n<h2 id=\"prostoy-primer-preobrazovaniya-dannyh-iz-shirokogo-formata-v-dlinnyy\">Nj\u00eb shembull i thjesht\u00eb i transformimit t\u00eb t\u00eb dh\u00ebnave nga formati i gjer\u00eb n\u00eb t\u00eb gjat\u00eb<\/h2>\n<p><\/p>\n<p><code>pivot_longer ()<\/code> b\u00ebn grumbujt e t\u00eb dh\u00ebnave t\u00eb gjat\u00eb, duke reduktuar numrin e kolonave dhe duke rritur numrin e rreshtave.<\/p>\n<p><\/p>\n<p><img decoding=\"async\" alt=\"Paketa R tidyr dhe funksionet e saj t\u00eb reja pivot_longer dhe pivot_wider\" src=\"\/wp-content\/uploads\/2020\/04\/fc88b99f2fecdcdaa6179d2e7d29e59d.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<p><\/p>\n<p>P\u00ebr t\u00eb realizuar shembujt e paraqitur n\u00eb artikull, fillimisht \u00ebsht\u00eb e nevojshme t\u00eb ngarkoni paketat e nevojshme:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">library(tidyr)\nlibrary(dplyr)\nlibrary(readr)<\/code><\/pre>\n<p><\/p>\n<p>Supozoni se kemi nj\u00eb tabel\u00eb me rezultatet e nj\u00eb ankete, ku (mes t\u00eb tjerash) u pyet\u00ebn njer\u00ebzit mbi fen\u00eb e tyre dhe t\u00eb ardhurat vjetore:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 18 x 11\n#&gt;    religion `&lt;$10k` `$10-20k` `$20-30k` `$30-40k` `$40-50k` `$50-75k`\n#&gt;    &lt;chr&gt;      &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;\n#&gt;  1 Agnostic      27        34        60        81        76       137\n#&gt;  2 Atheist       12        27        37        52        35        70\n#&gt;  3 Buddhist      27        21        30        34        33        58\n#&gt;  4 Catholic     418       617       732       670       638      1116\n#&gt;  5 Don\u2019t k\u2026      15        14        15        11        10        35\n#&gt;  6 Evangel\u2026     575       869      1064       982       881      1486\n#&gt;  7 Hindu          1         9         7         9        11        34\n#&gt;  8 Histori\u2026     228       244       236       238       197       223\n#&gt;  9 Jehovah\u2026      20        27        24        24        21        30\n#&gt; 10 Jewish        19        19        25        25        30        95\n#&gt; # \u2026 with 8 more rows, and 4 more variables: `$75-100k` &lt;dbl&gt;,\n#&gt; #   `$100-150k` &lt;dbl&gt;, `&gt;150k` &lt;dbl&gt;, `Don't know\/refused` &lt;dbl&gt;<\/code><\/pre>\n<p><\/p>\n<p>Ky tabela p\u00ebrmban t\u00eb dh\u00ebna rreth religjionit t\u00eb respondent\u00ebve n\u00eb rreshta, nd\u00ebrsa niveli i t\u00eb ardhurave \u00ebsht\u00eb shp\u00ebrndar\u00eb sipas emrave t\u00eb kolonave. Numri i respondent\u00ebve nga \u00e7do kategori ruhet n\u00eb vlerat e qelizave n\u00eb nd\u00ebrprerjen e religjionit dhe nivelit t\u00eb t\u00eb ardhurave. P\u00ebr t\u00eb sjell\u00eb tabel\u00ebn n\u00eb nj\u00eb format t\u00eb rregullt dhe t\u00eb sakt\u00eb, mjafton t\u00eb p\u00ebrdorni <code>pivot_longer()<\/code>:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">pew %&gt;% \n  pivot_longer(cols = -religion, names_to = \"income\", values_to = \"count\")<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">pew %&gt;% \n  pivot_longer(cols = -religion, names_to = \"income\", values_to = \"count\")\n#&gt; # A tibble: 180 x 3\n#&gt;    religion income             count\n#&gt;                      \n#&gt;  1 Agnostic   2 Agnostic $10-20k               34\n#&gt;  3 Agnostic $20-30k               60\n#&gt;  4 Agnostic $30-40k               81\n#&gt;  5 Agnostic $40-50k               76\n#&gt;  6 Agnostic $50-75k              137\n#&gt;  7 Agnostic $75-100k             122\n#&gt;  8 Agnostic $100-150k            109\n#&gt;  9 Agnostic &gt;150k                 84\n#&gt; 10 Agnostic Don't know\/refused    96\n#&gt; # \u2026 with 170 more rows<\/code><\/pre>\n<p><\/p>\n<p>Argumentet e funksionit <code>pivot_longer()<\/code><\/p>\n<p><\/p>\n<ul>\n<li>Argumenti i par\u00eb <em>cols<\/em>, p\u00ebrshkruan se cilat kolona duhet t\u00eb bashkohen. N\u00eb k\u00ebt\u00eb rast, t\u00eb gjitha kolonat, p\u00ebrve\u00e7 <em>time<\/em>.<\/li>\n<li>Argumenti <em>names_to<\/em> shkruan emrin e variablit q\u00eb do t\u00eb krijohet nga emrat e kolonave q\u00eb kemi bashkuar.<\/li>\n<li><em>values_to<\/em> shkruan emrin e variablit q\u00eb do t\u00eb krijohet nga t\u00eb dh\u00ebnat q\u00eb ruhen n\u00eb vlerat e qelizave t\u00eb kolonave t\u00eb bashkuara.<\/li>\n<\/ul>\n<p><\/p>\n<h2 id=\"specifikacii\">Specifikimet<\/h2>\n<p><\/p>\n<p>Ky \u00ebsht\u00eb nj\u00eb funksionalitet i ri i paket\u00ebs <strong>tidyr<\/strong>, i cili m\u00eb par\u00eb gjat\u00eb pun\u00ebs me funksionet e vjetra ishte i pap\u00ebrshtatsh\u00ebm.<\/p>\n<p><\/p>\n<p>Specifikimi \u00ebsht\u00eb nj\u00eb korniz\u00eb t\u00eb dh\u00ebnash, \u00e7do rresht i s\u00eb cil\u00ebs i p\u00ebrgjigjet nj\u00eb kolone n\u00eb dataframin e ri t\u00eb daljes, dhe dy kolona speciale, t\u00eb cilat fillojn\u00eb me: <\/p>\n<p><\/p>\n<ul>\n<li><em>.name<\/em> p\u00ebrmban emrin origjinal t\u00eb kolon\u00ebs. <\/li>\n<li><em>.value<\/em> p\u00ebrmban emrin e kolon\u00ebs, n\u00eb t\u00eb cil\u00ebn do t\u00eb hyjn\u00eb vlerat e qelizave. <\/li>\n<\/ul>\n<p><\/p>\n<p>Kolonat e tjera t\u00eb specifikimit pasqyrojn\u00eb se si n\u00eb kolon\u00ebn e re do t\u00eb paraqiten emrat e kolonave t\u00eb shkurtra nga <em>.name<\/em>.<\/p>\n<p><\/p>\n<p>Specifikimi p\u00ebrshkruan metadat\u00ebt, t\u00eb cilat ruhen n\u00eb emrin e kolon\u00ebs, me nj\u00eb rresht p\u00ebr \u00e7do kolone dhe nj\u00eb kolon\u00eb p\u00ebr \u00e7do variab\u00ebl t\u00eb bashkuar me emrin e kolon\u00ebs. Ndoshta tani kjo p\u00ebrkufizim duket e ngat\u00ebrruar, por pas shqyrtimit t\u00eb disa shembujve gjith\u00e7ka do t\u00eb b\u00ebhet shum\u00eb m\u00eb e qart\u00eb.<\/p>\n<p><\/p>\n<p>Kuptimi i specifikimit \u00ebsht\u00eb q\u00eb ju mund t\u00eb nxirrni, modifikoni dhe caktoni metadat\u00eb t\u00eb reja n\u00eb dataframin q\u00eb po transformoni.<\/p>\n<p><\/p>\n<p>P\u00ebr t\u00eb punuar me specifikimet gjat\u00eb transformimit t\u00eb tabel\u00ebs nga formati i gjer\u00eb n\u00eb t\u00eb gjat\u00eb sh\u00ebrben funksioni <code>pivot_longer_spec()<\/code>.<\/p>\n<p><\/p>\n<p>Si funksionon ky funksion, ai merr \u00e7do datafram dhe formon metadat\u00ebt e tij n\u00eb m\u00ebnyr\u00ebn e p\u00ebrshkruar m\u00eb sip\u00ebr. <\/p>\n<p><\/p>\n<p>P\u00ebr shembull, le t\u00eb marrim setin e t\u00eb dh\u00ebnave who, i cili ofrohet s\u00eb bashku me paket\u00ebn <strong>tidyr<\/strong>. Ky set t\u00eb dh\u00ebnash p\u00ebrmban informacionin e ofruar nga organizata nd\u00ebrkomb\u00ebtare e sh\u00ebndet\u00ebsis\u00eb p\u00ebr s\u00ebmundshm\u00ebrin\u00eb nga tuberkulozi.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">who\n# &gt; # A tibble: 7,240 x 60\n# &gt;    country iso2  iso3   year new_sp_m014 new_sp_m1524 new_sp_m2534\n# &gt;        &amp;  &amp;                        \n# &gt;  1 Afghan\u2026 AF    AFG    1980          NA           NA           NA\n# &gt;  2 Afghan\u2026 AF    AFG    1981          NA           NA           NA\n# &gt;  3 Afghan\u2026 AF    AFG    1982          NA           NA           NA\n# &gt;  4 Afghan\u2026 AF    AFG    1983          NA           NA           NA\n# &gt;  5 Afghan\u2026 AF    AFG    1984          NA           NA           NA\n# &gt;  6 Afghan\u2026 AF    AFG    1985          NA           NA           NA\n# &gt;  7 Afghan\u2026 AF    AFG    1986          NA           NA           NA\n# &gt;  8 Afghan\u2026 AF    AFG    1987          NA           NA           NA\n# &gt;  9 Afghan\u2026 AF    AFG    1988          NA           NA           NA\n# &gt; 10 Afghan\u2026 AF    AFG    1989          NA           NA           NA\n# &gt; # \u2026 with 7,230 more rows, and 53 more variables<\/code><\/pre>\n<p><\/p>\n<p>Do ta ndjekim specifikimin e tij.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">spec %\n  pivot_longer_spec(new_sp_m014:newrel_f65, values_to = \"count\")<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 56 x 3\n#&gt;    .name        .value name        \n#&gt;    &lt;chr&gt;        &lt;chr&gt;  &lt;chr&gt;       \n#&gt;  1 new_sp_m014  count  new_sp_m014 \n#&gt;  2 new_sp_m1524 count  new_sp_m1524\n#&gt;  3 new_sp_m2534 count  new_sp_m2534\n#&gt;  4 new_sp_m3544 count  new_sp_m3544\n#&gt;  5 new_sp_m4554 count  new_sp_m4554\n#&gt;  6 new_sp_m5564 count  new_sp_m5564\n#&gt;  7 new_sp_m65   count  new_sp_m65  \n#&gt;  8 new_sp_f014  count  new_sp_f014 \n#&gt;  9 new_sp_f1524 count  new_sp_f1524\n#&gt; 10 new_sp_f2534 count  new_sp_f2534\n#&gt; # \u2026 with 46 more rows<\/code><\/pre>\n<p><\/p>\n<p>Fushat <em>country<\/em>, <em>iso2<\/em>, <em>iso3<\/em> tani jan\u00eb variabla. Detyra jon\u00eb \u00ebsht\u00eb t\u00eb p\u00ebrmbysim kolonat me <em>new_sp_m014<\/em> sip\u00ebr <em>newrel_f65<\/em>.<\/p>\n<p><\/p>\n<p>N\u00eb emrat e k\u00ebtyre kolonave ndodhet informacioni si vijon:<\/p>\n<p><\/p>\n<ul>\n<li>Prefiksi <code>new_<\/code> tregon se kolona p\u00ebrmban t\u00eb dh\u00ebna mbi rastet e reja t\u00eb s\u00ebmundjes nga tuberkulozi, ku data kuad\u00ebr aktual p\u00ebrmban informacion vet\u00ebm p\u00ebr s\u00ebmundjet e reja, prandaj ky prefiks n\u00eb k\u00ebt\u00eb kontekst nuk ka ngarkes\u00eb semantike.<\/li>\n<li><code>sp<\/code>\/<code>rel<\/code>\/<code>sp<\/code>\/<code>ep<\/code> shpjegon metod\u00ebn e diagnostikimit t\u00eb s\u00ebmundjes.<\/li>\n<li><code>m<\/code>\/<code>f<\/code> gjinia e pacientit.<\/li>\n<li><code>014<\/code>\/<code>1524<\/code>\/<code>2535<\/code>\/<code>3544<\/code>\/<code>4554<\/code>\/<code>65<\/code> intervali i mosh\u00ebs s\u00eb pacientit.<\/li>\n<\/ul>\n<p><\/p>\n<p>Mund t\u00eb ndajm\u00eb k\u00ebto kolona duke p\u00ebrdorur funksionin <code>extract()<\/code>, duke p\u00ebrdorur nj\u00eb shprehje t\u00eb rregullt.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">spec %\n        extract(name, c(\"diagnosis\", \"gender\", \"age\"), \"new_?(.*)_(.)(.*)\")<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 56 x 5\n#&gt;    .name        .value diagnosis gender age  \n#&gt;    &lt;chr&gt;        &lt;chr&gt;  &lt;chr&gt;     &lt;chr&gt;  &lt;chr&gt;\n#&gt;  1 new_sp_m014  count  sp        m      014  \n#&gt;  2 new_sp_m1524 count  sp        m      1524 \n#&gt;  3 new_sp_m2534 count  sp        m      2534 \n#&gt;  4 new_sp_m3544 count  sp        m      3544 \n#&gt;  5 new_sp_m4554 count  sp        m      4554 \n#&gt;  6 new_sp_m5564 count  sp        m      5564 \n#&gt;  7 new_sp_m65   count  sp        m      65   \n#&gt;  8 new_sp_f014  count  sp        f      014  \n#&gt;  9 new_sp_f1524 count  sp        f      1524 \n#&gt; 10 new_sp_f2534 count  sp        f      2534 \n#&gt; # \u2026 with 46 more rows<\/code><\/pre>\n<p><\/p>\n<p>Vini re, kolona <em>.name<\/em> duhet t\u00eb mbetet e pandryshuar, pasi \u00ebsht\u00eb indeksi yn\u00eb n\u00eb emrat e kolonave t\u00eb setit t\u00eb t\u00eb dh\u00ebnave origjinal. <\/p>\n<p><\/p>\n<p>Gjinia dhe mosha (kolonat <em>gender<\/em> dhe <em>age<\/em>) kan\u00eb vlera fikse dhe t\u00eb njohura, prandaj rekomandohet q\u00eb k\u00ebto kolona t\u00eb konvertohen n\u00eb faktor\u00eb:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">spec %\n            mutate(\n              gender = factor(gender, levels = c(\"f\", \"m\")),\n              age = factor(age, levels = unique(age), ordered = TRUE)\n            ) <\/code><\/pre>\n<p><\/p>\n<p>S\u00eb fundi, p\u00ebr t\u00eb aplikuar specifikimin q\u00eb krijuam mbi kuadrin e t\u00eb dh\u00ebnave origjinal <em>who<\/em> na nevojitet t\u00eb p\u00ebrdorim argumentin <em>spec<\/em> n\u00eb funksionin <code>pivot_longer()<\/code>.<\/p>\n<p><\/p>\n<p><code>who %&gt;% pivot_longer(spec = spec)<\/code><\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 405,440 x 8\n#&gt;    country     iso2  iso3   year diagnosis gender age   count\n#&gt;    &lt;chr&gt;       &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;chr&gt;     &lt;fct&gt;  &lt;ord&gt; &lt;int&gt;\n#&gt;  1 Afghanistan AF    AFG    1980 sp        m      014      NA\n#&gt;  2 Afghanistan AF    AFG    1980 sp        m      1524     NA\n#&gt;  3 Afghanistan AF    AFG    1980 sp        m      2534     NA\n#&gt;  4 Afghanistan AF    AFG    1980 sp        m      3544     NA\n#&gt;  5 Afghanistan AF    AFG    1980 sp        m      4554     NA\n#&gt;  6 Afghanistan AF    AFG    1980 sp        m      5564     NA\n#&gt;  7 Afghanistan AF    AFG    1980 sp        m      65       NA\n#&gt;  8 Afghanistan AF    AFG    1980 sp        f      014      NA\n#&gt;  9 Afghanistan AF    AFG    1980 sp        f      1524     NA\n#&gt; 10 Afghanistan AF    AFG    1980 sp        f      2534     NA\n#&gt; # \u2026 with 405,430 more rows<\/code><\/pre>\n<p><\/p>\n<p>\u00c7do gj\u00eb q\u00eb sapo b\u00ebm\u00eb mund t\u00eb p\u00ebrfaq\u00ebsohet n\u00eb m\u00ebnyr\u00eb schematike si n\u00eb vijim:<\/p>\n<p><\/p>\n<p><img decoding=\"async\" alt=\"Paketa R tidyr dhe funksionet e saj t\u00eb reja pivot_longer dhe pivot_wider\" src=\"\/wp-content\/uploads\/2020\/04\/77c8c266c205e1b087738d31de8b8523.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<p><\/p>\n<h2 id=\"specifikaciya-s-ispolzovaniem-neskolkih-znacheniyvalue\">Specifikimi me p\u00ebrdorimin e disa vlerave (.value)<\/h2>\n<p><\/p>\n<p>N\u00eb shembullin e m\u00ebsip\u00ebrm, kolona e specifikimit <em>.value<\/em> p\u00ebrmbante vet\u00ebm nj\u00eb vler\u00eb, n\u00eb shumic\u00ebn e rasteve k\u00ebshtu ndodh. <\/p>\n<p><\/p>\n<p>Por ndonj\u00ebher\u00eb mund t\u00eb ndodhi nj\u00eb situat\u00eb kur ju nevojitet t\u00eb mbledhni n\u00eb vlera t\u00eb dh\u00ebna nga kolona me lloje t\u00eb ndryshme t\u00eb t\u00eb dh\u00ebnave. Duke p\u00ebrdorur funksionin e vjetruar <code>spread()<\/code> do t\u00eb ishte mjaft e v\u00ebshtir\u00eb ta realizonit k\u00ebt\u00eb.<\/p>\n<p><\/p>\n<p>Shembulli m\u00eb posht\u00eb \u00ebsht\u00eb marr\u00eb nga <noindex><a rel=\"nofollow\" href=\"https:\/\/cran.r-project.org\/web\/packages\/data.table\/vignettes\/datatable-reshape.html\">vignettes<\/a><\/noindex> paketa <strong>data.table<\/strong>.<\/p>\n<p><\/p>\n<p>Le ta krijojm\u00eb nj\u00eb dataframe trajnimi.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">family &lt;- tibble::tribble(\n  ~family,  ~dob_child1,  ~dob_child2, ~gender_child1, ~gender_child2,\n       1L, &quot;1998-11-26&quot;, &quot;2000-01-29&quot;,             1L,             2L,\n       2L, &quot;1996-06-22&quot;,           NA,             2L,             NA,\n       3L, &quot;2002-07-11&quot;, &quot;2004-04-05&quot;,             2L,             2L,\n       4L, &quot;2004-10-10&quot;, &quot;2009-08-27&quot;,             1L,             1L,\n       5L, &quot;2000-12-05&quot;, &quot;2005-02-28&quot;,             2L,             1L,\n)\nfamily % mutate_at(vars(starts_with(\"dob\")), parse_date)<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 5 x 5\n#&gt;   family dob_child1 dob_child2 gender_child1 gender_child2\n#&gt;    &lt;int&gt; &lt;date&gt;     &lt;date&gt;             &lt;int&gt;         &lt;int&gt;\n#&gt; 1      1 1998-11-26 2000-01-29             1             2\n#&gt; 2      2 1996-06-22 NA                     2            NA\n#&gt; 3      3 2002-07-11 2004-04-05             2             2\n#&gt; 4      4 2004-10-10 2009-08-27             1             1\n#&gt; 5      5 2000-12-05 2005-02-28             2             1<\/code><\/pre>\n<p><\/p>\n<p>Dataframe i krijuar n\u00eb \u00e7do rresht p\u00ebrmban t\u00eb dh\u00ebna p\u00ebr f\u00ebmij\u00ebt e nj\u00eb familje. N\u00eb familje mund t\u00eb ket\u00eb nj\u00eb ose dy f\u00ebmij\u00eb. P\u00ebr secilin f\u00ebmij\u00eb sigurohen t\u00eb dh\u00ebna mbi dat\u00ebn e lindjes dhe gjinin\u00eb, nd\u00ebrsa t\u00eb dh\u00ebnat p\u00ebr secilin f\u00ebmij\u00eb jan\u00eb n\u00eb kolona t\u00eb ve\u00e7anta, detyra jon\u00eb \u00ebsht\u00eb t\u00eb sjellim k\u00ebto t\u00eb dh\u00ebna n\u00eb formatin e duhur p\u00ebr analiz\u00eb.<\/p>\n<p><\/p>\n<p>Vini re se kemi dy variabla me informacion mbi secilin f\u00ebmij\u00eb: gjinia dhe data e lindjes (kolonat me prefiksin <em>dop<\/em> p\u00ebrmbajn\u00eb dat\u00ebn e lindjes, kolonat me prefiksin <em>gender<\/em> p\u00ebrmbajn\u00eb gjinin\u00eb e f\u00ebmij\u00ebs). N\u00eb rezultatin e pritur ato duhet t\u00eb jen\u00eb n\u00eb kolona t\u00eb ve\u00e7anta. Ne mund ta b\u00ebjm\u00eb k\u00ebt\u00eb duke gjeneruar nj\u00eb specifikim, n\u00eb t\u00eb cilin kolona <code>.value<\/code> do t\u00eb ket\u00eb dy vlera t\u00eb ndryshme.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">spec %\n  pivot_longer_spec(-family) %&gt;%\n  separate(col = name, into = c(\".value\", \"child\"))%&gt;%\n  mutate(child = parse_number(child))\n<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 4 x 3\n#&gt;   .name         .value child\n#&gt;   &lt;chr&gt;         &lt;chr&gt;  &lt;dbl&gt;\n#&gt; 1 dob_child1    dob        1\n#&gt; 2 dob_child2    dob        2\n#&gt; 3 gender_child1 gender     1\n#&gt; 4 gender_child2 gender     2<\/code><\/pre>\n<p><\/p>\n<p>K\u00ebshtu, le t\u00eb analizojm\u00eb hapat q\u00eb kryhen nga kodi i m\u00ebsip\u00ebrm.<\/p>\n<p><\/p>\n<ul>\n<li><code>pivot_longer_spec(-familja)<\/code> \u2014 krijojm\u00eb nj\u00eb specifikim q\u00eb comprimion t\u00eb gjitha kolonat ekzistuese, p\u00ebrve\u00e7 kolon\u00ebs familja.<\/li>\n<li><code>separate(col = name, into = c(\".value\", \"child\"))<\/code> \u2014 ndajm\u00eb kolon\u00ebn <em>.name<\/em>, e cila p\u00ebrmban emrat e fushave origjinale, sipas posht\u00ebs dhe regjistron vlerat e marra n\u00eb kolonat <em>.value<\/em> dhe <em>f\u00ebmija<\/em>.<\/li>\n<li><code>mutate(f\u00ebmija = parse_number(f\u00ebmija))<\/code> \u2014 konvertojm\u00eb vlerat e fush\u00ebs <em>f\u00ebmija<\/em> nga lloji tekstual n\u00eb llojin num\u00ebror.<\/li>\n<\/ul>\n<p><\/p>\n<p>Tani ne mund t\u00eb aplikojm\u00eb specifikimin e fituar n\u00eb dataframe origjinal dhe ta formatojm\u00eb tavolin\u00ebn n\u00eb m\u00ebnyr\u00ebn e d\u00ebshiruar.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">familja %&gt;% \n    pivot_longer(spec = spec, na.rm = T)<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 9 x 4\n#&gt;   family child dob        gender\n#&gt;    &lt;int&gt; &lt;dbl&gt; &lt;date&gt;      &lt;int&gt;\n#&gt; 1      1     1 1998-11-26      1\n#&gt; 2      1     2 2000-01-29      2\n#&gt; 3      2     1 1996-06-22      2\n#&gt; 4      3     1 2002-07-11      2\n#&gt; 5      3     2 2004-04-05      2\n#&gt; 6      4     1 2004-10-10      1\n#&gt; 7      4     2 2009-08-27      1\n#&gt; 8      5     1 2000-12-05      2\n#&gt; 9      5     2 2005-02-28      1<\/code><\/pre>\n<p><\/p>\n<p>Ne p\u00ebrdorim argumentin <code>na.rm = TRUE<\/code>, sepse forma aktuale e t\u00eb dh\u00ebnave detyron krijimin e rreshtave t\u00eb tep\u00ebrt p\u00ebr observimet q\u00eb nuk ekzistojn\u00eb. Pasi n\u00eb familjen 2 ka vet\u00ebm nj\u00eb f\u00ebmij\u00eb, <code>na.rm = TRUE<\/code> siguron se q\u00eb familja 2 do t\u00eb ket\u00eb nj\u00eb rresht n\u00eb dalje.<\/p>\n<p><\/p>\n<h2 id=\"preobrazovanie-data-freymov-iz-dlinnogo-formata-k-shirokomu\">Transformimi i frame-ve t\u00eb t\u00eb dh\u00ebnave nga formati i gjat\u00eb n\u00eb t\u00eb gjer\u00eb<\/h2>\n<p><\/p>\n<p><code>pivot_wider()<\/code> \u2014 \u00ebsht\u00eb nj\u00eb transformim i kund\u00ebrt, dhe p\u00ebrkundrazi rrit numrin e kolonave t\u00eb kuadrit t\u00eb t\u00eb dh\u00ebnave duke ulur numrin e rreshtave.<\/p>\n<p><\/p>\n<p><img decoding=\"async\" alt=\"Paketa R tidyr dhe funksionet e saj t\u00eb reja pivot_longer dhe pivot_wider\" src=\"\/wp-content\/uploads\/2020\/04\/b470664e06f362d6f0ea6df9c1ed426d.jpg\" style=\"display:block;margin: 0 auto;\" \/><\/p>\n<p><\/p>\n<p>Kjo lloj transformimi p\u00ebrdoret shum\u00eb rrall\u00eb p\u00ebr t\u00eb sjell\u00eb t\u00eb dh\u00ebnat n\u00eb nj\u00eb pamje t\u00eb rregullt, megjithat\u00eb ky metod\u00eb mund t\u00eb jet\u00eb e dobishme p\u00ebr krijimin e tabelave p\u00ebrmbledh\u00ebse q\u00eb p\u00ebrdoren n\u00eb prezantime, ose p\u00ebr integrimin me ndonj\u00eb veg\u00ebl tjet\u00ebr.<\/p>\n<p><\/p>\n<p>N\u00eb fakt funksionet <code>pivot_longer()<\/code> dhe <code>pivot_wider()<\/code> jan\u00eb simetrike, dhe kryejn\u00eb veprime t\u00eb kund\u00ebrta, dmth: <code>df %&gt;% pivot_longer(spec = spec) %&gt;% pivot_wider(spec = spec)<\/code> dhe <code>df %&gt;% pivot_wider(spec = spec) %&gt;% pivot_longer(spec = spec)<\/code> do t\u00eb rikthej\u00eb df origjinal.<\/p>\n<p><\/p>\n<h3 id=\"prosteyshiy-primer-privedeniya-tablicy-k-shirokomu-formatu\">Nj\u00eb shembull shum\u00eb i thjesht\u00eb i sjelljes s\u00eb tabel\u00ebs n\u00eb nj\u00eb format t\u00eb gjer\u00eb<\/h3>\n<p><\/p>\n<p>P\u00ebr t\u00eb demonstruar funksionin <code>pivot_wider()<\/code> ne do t\u00eb p\u00ebrdorim nj\u00eb grup t\u00eb dh\u00ebnash <em>fish_encounters<\/em>, i cili ruan informacione rreth m\u00ebnyrave si stacionet e ndryshme regjistrojn\u00eb l\u00ebvizjen e peshqve n\u00eb lum\u00eb.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 114 x 3\n#&gt;    fish  station  seen\n#&gt;    &lt;fct&gt; &lt;fct&gt;   &lt;int&gt;\n#&gt;  1 4842  Release     1\n#&gt;  2 4842  I80_1       1\n#&gt;  3 4842  Lisbon      1\n#&gt;  4 4842  Rstr        1\n#&gt;  5 4842  Base_TD     1\n#&gt;  6 4842  BCE         1\n#&gt;  7 4842  BCW         1\n#&gt;  8 4842  BCE2        1\n#&gt;  9 4842  BCW2        1\n#&gt; 10 4842  MAE         1\n#&gt; # \u2026 with 104 more rows<\/code><\/pre>\n<p><\/p>\n<p>N\u00eb shumic\u00ebn e rasteve, kjo tabel\u00eb do t\u00eb jet\u00eb m\u00eb informuese dhe e leht\u00eb p\u00ebr t'u p\u00ebrdorur n\u00ebse paraqesim informacionin p\u00ebr \u00e7do stacion n\u00eb nj\u00eb kolon\u00eb t\u00eb ve\u00e7ant\u00eb.<\/p>\n<p><\/p>\n<p><code>fish_encounters %&gt;% pivot_wider(names_from = station, values_from = seen)<\/code><\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">fish_encounters %&gt;% pivot_wider(names_from = station, values_from = seen)\n#&gt; # Nj\u00eb tibble: 19 x 12\n#&gt;    fish  Release I80_1 Lisbon  Rstr Base_TD   BCE   BCW  BCE2  BCW2   MAE\n#&gt;        &amp;;                 \n#&gt;  1 4842        1     1      1     1       1     1     1     1     1     1\n#&gt;  2 4843        1     1      1     1       1     1     1     1     1     1\n#&gt;  3 4844        1     1      1     1       1     1     1     1     1     1\n#&gt;  4 4845        1     1      1     1       1    NA    NA    NA    NA    NA\n#&gt;  5 4847        1     1      1    NA      NA    NA    NA    NA    NA    NA\n#&gt;  6 4848        1     1      1     1      NA    NA    NA    NA    NA    NA\n#&gt;  7 4849        1     1     NA    NA      NA    NA    NA    NA    NA    NA\n#&gt;  8 4850        1     1     NA     1       1     1     1    NA    NA    NA\n#&gt;  9 4851        1     1     NA    NA      NA    NA    NA    NA    NA    NA\n#&gt; 10 4854        1     1     NA    NA      NA    NA    NA    NA    NA    NA\n#&gt; # \u2026 me 9 rreshta t\u00eb tjera dhe 1 variab\u00ebl m\u00eb shum\u00eb: MAW<\/code><\/pre>\n<p><\/p>\n<p>Ky grup t\u00eb dh\u00ebnash regjistron informacione vet\u00ebm n\u00eb rastet kur peshku \u00ebsht\u00eb zbuluar nga stacioni, dmth. n\u00ebse ndonj\u00eb peshk nuk \u00ebsht\u00eb regjistruar nga ndonj\u00eb stacion, at\u00ebher\u00eb ato t\u00eb dh\u00ebna nuk do t\u00eb jen\u00eb n\u00eb tabel\u00eb. Kjo do t\u00eb thot\u00eb se daljet do t\u00eb jen\u00eb t\u00eb mbushura me NA. <\/p>\n<p><\/p>\n<p>Megjithat\u00eb n\u00eb k\u00ebt\u00eb rast ne e dim\u00eb se mungesa e nj\u00eb regjistrimi do t\u00eb thot\u00eb se peshku nuk \u00ebsht\u00eb v\u00ebn\u00eb re, prandaj mund t\u00eb p\u00ebrdorim argumentin <em>values_fill<\/em> n\u00eb funksionin <code>pivot_wider()<\/code> dhe t\u00eb mbushim k\u00ebto vlera t\u00eb humbura me zero:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">fish_encounters %&gt;% pivot_wider(\n  names_from = station, \n  values_from = seen,\n  values_fill = list(seen = 0)\n)<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 19 x 12\n#&gt;    fish  Release I80_1 Lisbon  Rstr Base_TD   BCE   BCW  BCE2  BCW2   MAE\n#&gt;    &lt;fct&gt;   &lt;int&gt; &lt;int&gt;  &lt;int&gt; &lt;int&gt;   &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt; &lt;int&gt;\n#&gt;  1 4842        1     1      1     1       1     1     1     1     1     1\n#&gt;  2 4843        1     1      1     1       1     1     1     1     1     1\n#&gt;  3 4844        1     1      1     1       1     1     1     1     1     1\n#&gt;  4 4845        1     1      1     1       1     0     0     0     0     0\n#&gt;  5 4847        1     1      1     0       0     0     0     0     0     0\n#&gt;  6 4848        1     1      1     1       0     0     0     0     0     0\n#&gt;  7 4849        1     1      0     0       0     0     0     0     0     0\n#&gt;  8 4850        1     1      0     1       1     1     1     0     0     0\n#&gt;  9 4851        1     1      0     0       0     0     0     0     0     0\n#&gt; 10 4854        1     1      0     0       0     0     0     0     0     0\n#&gt; # \u2026 with 9 more rows, and 1 more variable: MAW &lt;int&gt;<\/code><\/pre>\n<p><\/p>\n<h3 id=\"generaciya-imeni-stolbca-iz-neskolkih-ishodnyh-peremennyh\">Generimi i emrit t\u00eb kolon\u00ebs nga disa variabla burimor\u00eb<\/h3>\n<p><\/p>\n<p>Imagjinoni se kemi nj\u00eb tabel\u00eb q\u00eb p\u00ebrmban nj\u00eb kombinim t\u00eb produktit, vendit dhe vitit. P\u00ebr t\u00eb gjeneruar nj\u00eb kuad\u00ebr t\u00eb dh\u00ebnash testues, mund t\u00eb ekzekutoni kodin e m\u00ebposht\u00ebm:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">df %\n  filter((product == \"A\" &amp; country == \"AI\") | product == \"B\") %&gt;% \n  mutate(value = rnorm(nrow(.)))<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 45 x 4\n#&gt;    product country  year    value\n#&gt;    &lt;chr&gt;   &lt;chr&gt;   &lt;int&gt;    &lt;dbl&gt;\n#&gt;  1 A       AI       2000 -2.05   \n#&gt;  2 A       AI       2001 -0.676  \n#&gt;  3 A       AI       2002  1.60   \n#&gt;  4 A       AI       2003 -0.353  \n#&gt;  5 A       AI       2004 -0.00530\n#&gt;  6 A       AI       2005  0.442  \n#&gt;  7 A       AI       2006 -0.610  \n#&gt;  8 A       AI       2007 -2.77   \n#&gt;  9 A       AI       2008  0.899  \n#&gt; 10 A       AI       2009 -0.106  \n#&gt; # \u2026 with 35 more rows<\/code><\/pre>\n<p><\/p>\n<p>Detyra jon\u00eb \u00ebsht\u00eb t\u00eb zgjasim kuadrin e t\u00eb dh\u00ebnave n\u00eb m\u00ebnyr\u00eb q\u00eb nj\u00eb kolon\u00eb t\u00eb p\u00ebrmbaj\u00eb t\u00eb dh\u00ebna p\u00ebr \u00e7do kombinim produkti dhe vendi. P\u00ebr k\u00ebt\u00eb, mjafton t\u00eb kaloni n\u00eb argumentin <em>names_from<\/em> nj\u00eb vektor q\u00eb p\u00ebrmban emrat e fushave t\u00eb bashkuara.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">df %&gt;% pivot_wider(names_from = c(product, country),\n                 values_from = \"value\")<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 15 x 4\n#&gt;     year     A_AI    B_AI    B_EI\n#&gt;    &lt;int&gt;    &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;\n#&gt;  1  2000 -2.05     0.607   1.20  \n#&gt;  2  2001 -0.676    1.65   -0.114 \n#&gt;  3  2002  1.60    -0.0245  0.501 \n#&gt;  4  2003 -0.353    1.30   -0.459 \n#&gt;  5  2004 -0.00530  0.921  -0.0589\n#&gt;  6  2005  0.442   -1.55    0.594 \n#&gt;  7  2006 -0.610    0.380  -1.28  \n#&gt;  8  2007 -2.77     0.830   0.637 \n#&gt;  9  2008  0.899    0.0175 -1.30  \n#&gt; 10  2009 -0.106   -0.195   1.03  \n#&gt; # \u2026 with 5 more rows<\/code><\/pre>\n<p><\/p>\n<p>Po ashtu, mund t\u00eb aplikoni specifikime n\u00eb funksionin <code>pivot_wider()<\/code>. Por kur kaloni n\u00eb <code>pivot_wider()<\/code> specifikimi kryen nj\u00eb transformim, t\u00eb kund\u00ebrt <code>pivot_longer()<\/code>: krijohen kolonat q\u00eb p\u00ebrmenden n\u00eb <em>.name<\/em>, duke p\u00ebrdorur vlerat nga <em>.value<\/em> dhe kolonat e tjera.<\/p>\n<p><\/p>\n<p>P\u00ebr k\u00ebt\u00eb set t\u00eb dh\u00ebnash, mund t\u00eb gjeneroni nj\u00eb specifikim personalizues, n\u00ebse d\u00ebshironi q\u00eb \u00e7do kombinim t\u00eb mundsh\u00ebm t\u00eb vendit dhe produktit t\u00eb ket\u00eb kolon\u00ebn e vet, e jo vet\u00ebm ato q\u00eb jan\u00eb t\u00eb pranishme n\u00eb t\u00eb dh\u00ebna:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">spec % \n  expand(product, country, .value = \"value\") %&gt;% \n  unite(\".name\", product, country, remove = FALSE)<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 4 x 4\n#&gt;   .name product country .value\n#&gt;   &lt;chr&gt; &lt;chr&gt;   &lt;chr&gt;   &lt;chr&gt; \n#&gt; 1 A_AI  A       AI      value \n#&gt; 2 A_EI  A       EI      value \n#&gt; 3 B_AI  B       AI      value \n#&gt; 4 B_EI  B       EI      value<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">df %&gt;% pivot_wider(spec = spec) %&gt;% head()<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 6 x 5\n#&gt;    year     A_AI  A_EI    B_AI    B_EI\n#&gt;   &lt;int&gt;    &lt;dbl&gt; &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;\n#&gt; 1  2000 -2.05       NA  0.607   1.20  \n#&gt; 2  2001 -0.676      NA  1.65   -0.114 \n#&gt; 3  2002  1.60       NA -0.0245  0.501 \n#&gt; 4  2003 -0.353      NA  1.30   -0.459 \n#&gt; 5  2004 -0.00530    NA  0.921  -0.0589\n#&gt; 6  2005  0.442      NA -1.55    0.594<\/code><\/pre>\n<p><\/p>\n<h2 id=\"neskolko-prodvinutyh-primerov-raboty-s-novoy-koncepciey-tidyr\">Disa shembuj m\u00eb t\u00eb avancuar t\u00eb pun\u00ebs me konceptin e ri tidyr<\/h2>\n<p><\/p>\n<h3 id=\"privedenie-dannyh-k-akkuratnomu-vidu-na-primere-nabora-dannyh-o-perepisi-dohoda-i-arendnoy-platy-v-ssha\">Kozhime t\u00eb dh\u00ebnash n\u00eb nj\u00eb format t\u00eb rregullt me shembullin e nj\u00eb seti t\u00eb dh\u00ebnash mbi t\u00eb ardhurat dhe qirat\u00eb n\u00eb SHBA<\/h3>\n<p><\/p>\n<p>Seti i t\u00eb dh\u00ebnave <em>us_rent_income<\/em> p\u00ebrmban informacion mbi t\u00eb ardhurat mesatare dhe qiran\u00eb p\u00ebr \u00e7do shtet n\u00eb SHBA p\u00ebr vitin 2017 (seti i t\u00eb dh\u00ebnave \u00ebsht\u00eb i disponuesh\u00ebm n\u00eb paket\u00ebn <strong>tidycensus<\/strong>).<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">us_rent_income\n#&gt; # A tibble: 104 x 5\n#&gt;    GEOID NAME       variable estimate   moe\n#&gt;                   \n#&gt;  1 01    Alabama    income      24476   136\n#&gt;  2 01    Alabama    rent          747     3\n#&gt;  3 02    Alaska     income      32940   508\n#&gt;  4 02    Alaska     rent         1200    13\n#&gt;  5 04    Arizona    income      27517   148\n#&gt;  6 04    Arizona    rent          972     4\n#&gt;  7 05    Arkansas   income      23789   165\n#&gt;  8 05    Arkansas   rent          709     5\n#&gt;  9 06    California income      29454   109\n#&gt; 10 06    California rent         1358     3\n#&gt; # \u2026 with 94 more rows<\/code><\/pre>\n<p><\/p>\n<p>N\u00eb form\u00ebn n\u00eb t\u00eb cil\u00ebn ruhen t\u00eb dh\u00ebnat n\u00eb dataset, <em>us_rent_income<\/em> punimi me to \u00ebsht\u00eb jasht\u00ebzakonisht i v\u00ebshtir\u00eb, prandaj do t\u00eb d\u00ebshironim t\u00eb krijonim nj\u00eb set t\u00eb dh\u00ebnash me kolonat: <em>rent<\/em>, <em>rent_moe<\/em>, <em>come<\/em>, <em>income_moe<\/em>. Ka shum\u00eb m\u00ebnyra p\u00ebr t\u00eb krijuar k\u00ebt\u00eb specifikim, por e r\u00ebnd\u00ebsishme \u00ebsht\u00eb se na nevojitet t\u00eb gjenerojm\u00eb \u00e7do kombinim t\u00eb vlerave t\u00eb variabl\u00ebs dhe <em>estimate\/moe<\/em>, dhe pastaj t\u00eb gjenerojm\u00eb emrin e kolon\u00ebs.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">  spec %\n    expand(variable, .value = c(\"estimate\", \"moe\")) %&gt;% \n    mutate(\n      .name = paste0(variable, ifelse(.value == \"moe\", \"_moe\", \"\"))\n    )<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 4 x 3\n#&gt;   variable .value   .name     \n#&gt;   &lt;chr&gt;    &lt;chr&gt;    &lt;chr&gt;     \n#&gt; 1 income   estimate income    \n#&gt; 2 income   moe      income_moe\n#&gt; 3 rent     estimate rent      \n#&gt; 4 rent     moe      rent_moe<\/code><\/pre>\n<p><\/p>\n<p>Ofrimi i k\u00ebsaj specifikimi <code>pivot_wider()<\/code> na jep rezultatin q\u00eb k\u00ebrkojm\u00eb:<\/p>\n<p><\/p>\n<p><code>us_rent_income %&gt;% pivot_wider(spec = spec)<\/code><\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 52 x 6\n#&gt;    GEOID NAME                 income income_moe  rent rent_moe\n#&gt;    &lt;chr&gt; &lt;chr&gt;                 &lt;dbl&gt;      &lt;dbl&gt; &lt;dbl&gt;    &lt;dbl&gt;\n#&gt;  1 01    Alabama               24476        136   747        3\n#&gt;  2 02    Alaska                32940        508  1200       13\n#&gt;  3 04    Arizona               27517        148   972        4\n#&gt;  4 05    Arkansas              23789        165   709        5\n#&gt;  5 06    California            29454        109  1358        3\n#&gt;  6 08    Colorado              32401        109  1125        5\n#&gt;  7 09    Connecticut           35326        195  1123        5\n#&gt;  8 10    Delaware              31560        247  1076       10\n#&gt;  9 11    District of Columbia  43198        681  1424       17\n#&gt; 10 12    Florida               25952         70  1077        3\n#&gt; # \u2026 with 42 more rows<\/code><\/pre>\n<p><\/p>\n<h3 id=\"vsemirnyy-bank\">Banka Bot\u00ebrore<\/h3>\n<p><\/p>\n<p>Ndonj\u00ebher\u00eb, shnd\u00ebrrimi i nj\u00eb seti t\u00eb dh\u00ebnash n\u00eb form\u00ebn e duhur k\u00ebrkon disa hapa.<br \/>\nDataset <em>world_bank_pop<\/em> p\u00ebrmban t\u00eb dh\u00ebna nga banka bot\u00ebrore p\u00ebr popullsin\u00eb e \u00e7do vendi n\u00eb periudh\u00ebn nga viti 2000 deri n\u00eb vitin 2018.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 1,056 x 20\n#&gt;    country indicator `2000` `2001` `2002` `2003`  `2004`  `2005`   `2006`\n#&gt;    &lt;chr&gt;   &lt;chr&gt;      &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;   &lt;dbl&gt;   &lt;dbl&gt;    &lt;dbl&gt;\n#&gt;  1 ABW     SP.URB.T\u2026 4.24e4 4.30e4 4.37e4 4.42e4 4.47e+4 4.49e+4  4.49e+4\n#&gt;  2 ABW     SP.URB.G\u2026 1.18e0 1.41e0 1.43e0 1.31e0 9.51e-1 4.91e-1 -1.78e-2\n#&gt;  3 ABW     SP.POP.T\u2026 9.09e4 9.29e4 9.50e4 9.70e4 9.87e+4 1.00e+5  1.01e+5\n#&gt;  4 ABW     SP.POP.G\u2026 2.06e0 2.23e0 2.23e0 2.11e0 1.76e+0 1.30e+0  7.98e-1\n#&gt;  5 AFG     SP.URB.T\u2026 4.44e6 4.65e6 4.89e6 5.16e6 5.43e+6 5.69e+6  5.93e+6\n#&gt;  6 AFG     SP.URB.G\u2026 3.91e0 4.66e0 5.13e0 5.23e0 5.12e+0 4.77e+0  4.12e+0\n#&gt;  7 AFG     SP.POP.T\u2026 2.01e7 2.10e7 2.20e7 2.31e7 2.41e+7 2.51e+7  2.59e+7\n#&gt;  8 AFG     SP.POP.G\u2026 3.49e0 4.25e0 4.72e0 4.82e0 4.47e+0 3.87e+0  3.23e+0\n#&gt;  9 AGO     SP.URB.T\u2026 8.23e6 8.71e6 9.22e6 9.77e6 1.03e+7 1.09e+7  1.15e+7\n#&gt; 10 AGO     SP.URB.G\u2026 5.44e0 5.59e0 5.70e0 5.76e0 5.75e+0 5.69e+0  4.92e+0\n#&gt; # \u2026 with 1,046 more rows, and 11 more variables: `2007` &lt;dbl&gt;,\n#&gt; #   `2008` &lt;dbl&gt;, `2009` &lt;dbl&gt;, `2010` &lt;dbl&gt;, `2011` &lt;dbl&gt;, `2012` &lt;dbl&gt;,\n#&gt; #   `2013` &lt;dbl&gt;, `2014` &lt;dbl&gt;, `2015` &lt;dbl&gt;, `2016` &lt;dbl&gt;, `2017` &lt;dbl&gt;<\/code><\/pre>\n<p><\/p>\n<p>Q\u00ebllimi yn\u00eb \u00ebsht\u00eb t\u00eb krijojm\u00eb nj\u00eb set t\u00eb dh\u00ebnash t\u00eb organizuar, ku \u00e7do variab\u00ebl ndodhet n\u00eb nj\u00eb kolon\u00eb t\u00eb ve\u00e7ant\u00eb. Deri m\u00eb tani, nuk \u00ebsht\u00eb e qart\u00eb se cilat hapa t\u00eb nevojiten sakt\u00ebsisht, por ne do t\u00eb fillojm\u00eb me problemin m\u00eb t\u00eb duksh\u00ebm: viti \u00ebsht\u00eb shp\u00ebrndar\u00eb n\u00eb disa kolona.<\/p>\n<p><\/p>\n<p>P\u00ebr ta riparuar k\u00ebt\u00eb, \u00ebsht\u00eb e nevojshme t\u00eb p\u00ebrdoret funksioni <code>pivot_longer()<\/code>.<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">pop2 % \n  pivot_longer(`2000`:`2017`, names_to = \"year\")<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 19,008 x 4\n#&gt;    country indicator   year  value\n#&gt;    &lt;chr&gt;   &lt;chr&gt;       &lt;chr&gt; &lt;dbl&gt;\n#&gt;  1 ABW     SP.URB.TOTL 2000  42444\n#&gt;  2 ABW     SP.URB.TOTL 2001  43048\n#&gt;  3 ABW     SP.URB.TOTL 2002  43670\n#&gt;  4 ABW     SP.URB.TOTL 2003  44246\n#&gt;  5 ABW     SP.URB.TOTL 2004  44669\n#&gt;  6 ABW     SP.URB.TOTL 2005  44889\n#&gt;  7 ABW     SP.URB.TOTL 2006  44881\n#&gt;  8 ABW     SP.URB.TOTL 2007  44686\n#&gt;  9 ABW     SP.URB.TOTL 2008  44375\n#&gt; 10 ABW     SP.URB.TOTL 2009  44052\n#&gt; # \u2026 with 18,998 more rows<\/code><\/pre>\n<p><\/p>\n<p>Hapi tjet\u00ebr \u00ebsht\u00eb t\u00eb shqyrtojm\u00eb variabl\u00ebn indicator.<br \/>\n<code>pop2 %&gt;% count(indicator)<\/code><\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 4 x 2\n#&gt;   indicator       n\n#&gt;   &lt;chr&gt;       &lt;int&gt;\n#&gt; 1 SP.POP.GROW  4752\n#&gt; 2 SP.POP.TOTL  4752\n#&gt; 3 SP.URB.GROW  4752\n#&gt; 4 SP.URB.TOTL  4752<\/code><\/pre>\n<p><\/p>\n<p>Ku SP.POP.GROW \u00ebsht\u00eb rritja e popullsis\u00eb, SP.POP.TOTL \u00ebsht\u00eb numri total i popullsis\u00eb, dhe SP.URB. * \u00ebsht\u00eb e nj\u00ebjta, por vet\u00ebm p\u00ebr zonat urbane. Le t\u00eb ndajm\u00eb k\u00ebto vlera n\u00eb dy variabla: area \u2014 zona (total ose urban) dhe nj\u00eb variab\u00ebl q\u00eb p\u00ebrmban t\u00eb dh\u00ebnat e v\u00ebrteta (population ose growth):<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">pop3 % \n  separate(indicator, c(NA, \"area\", \"variable\") )<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 19,008 x 5\n#&gt;    country area  variable year  value\n#&gt;    &lt;chr&gt;   &lt;chr&gt; &lt;chr&gt;    &lt;chr&gt; &lt;dbl&gt;\n#&gt;  1 ABW     URB   TOTL     2000  42444\n#&gt;  2 ABW     URB   TOTL     2001  43048\n#&gt;  3 ABW     URB   TOTL     2002  43670\n#&gt;  4 ABW     URB   TOTL     2003  44246\n#&gt;  5 ABW     URB   TOTL     2004  44669\n#&gt;  6 ABW     URB   TOTL     2005  44889\n#&gt;  7 ABW     URB   TOTL     2006  44881\n#&gt;  8 ABW     URB   TOTL     2007  44686\n#&gt;  9 ABW     URB   TOTL     2008  44375\n#&gt; 10 ABW     URB   TOTL     2009  44052\n#&gt; # \u2026 with 18,998 more rows<\/code><\/pre>\n<p><\/p>\n<p>Tani na mbetet vet\u00ebm t\u00eb ndajm\u00eb variabl\u00ebn variable n\u00eb dy kolona:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">pop3 %&gt;% \n  pivot_wider(names_from = variable, values_from = value)<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 9,504 x 5\n#&gt;    country area  year   TOTL    GROW\n#&gt;    &lt;chr&gt;   &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;   &lt;dbl&gt;\n#&gt;  1 ABW     URB   2000  42444  1.18  \n#&gt;  2 ABW     URB   2001  43048  1.41  \n#&gt;  3 ABW     URB   2002  43670  1.43  \n#&gt;  4 ABW     URB   2003  44246  1.31  \n#&gt;  5 ABW     URB   2004  44669  0.951 \n#&gt;  6 ABW     URB   2005  44889  0.491 \n#&gt;  7 ABW     URB   2006  44881 -0.0178\n#&gt;  8 ABW     URB   2007  44686 -0.435 \n#&gt;  9 ABW     URB   2008  44375 -0.698 \n#&gt; 10 ABW     URB   2009  44052 -0.731 \n#&gt; # \u2026 with 9,494 more rows<\/code><\/pre>\n<p><\/p>\n<h3 id=\"spisok-kontaktov\">Lista e kontakteve<\/h3>\n<p><\/p>\n<p>Shembulli i fundit, imagjinoni se keni nj\u00eb list\u00eb kontakti q\u00eb e keni kopjuar dhe ngjitur nga nj\u00eb faqe interneti:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">contacts &lt;- tribble(\n  ~field, ~value,\n  &quot;name&quot;, &quot;Jiena McLellan&quot;,\n  &quot;company&quot;, &quot;Toyota&quot;, \n  &quot;name&quot;, &quot;John Smith&quot;, \n  &quot;company&quot;, &quot;google&quot;, \n  &quot;email&quot;, &quot;john@google.com&quot;,\n  &quot;name&quot;, &quot;Huxley Ratcliffe&quot;\n)<\/code><\/pre>\n<p><\/p>\n<p>T\u00eb shnd\u00ebrroni k\u00ebt\u00eb list\u00eb n\u00eb nj\u00eb form\u00eb tabelore \u00ebsht\u00eb mjaft e v\u00ebshtir\u00eb, sepse nuk ka nj\u00eb besueshm\u00ebri q\u00eb identifikon cilat t\u00eb dh\u00ebna i p\u00ebrkasin cilit kontakt. Mund ta rregullojm\u00eb k\u00ebt\u00eb duke v\u00ebn\u00eb re se t\u00eb dh\u00ebnat p\u00ebr \u00e7do kontakt t\u00eb ri fillojn\u00eb me emrin (\"name\"), prandaj mund t\u00eb krijojm\u00eb nj\u00eb identifikues unik, dhe ta rrisim me nj\u00eb \u00e7do her\u00eb q\u00eb n\u00eb kolon\u00ebn field shfaqet vlera \"name\":<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">contacts %\n  mutate(\n    person_id = cumsum(field == \"name\")\n  )\ncontacts<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 6 x 3\n#&gt;   field   value            person_id\n#&gt;   &lt;chr&gt;   &lt;chr&gt;                &lt;int&gt;\n#&gt; 1 name    Jiena McLellan           1\n#&gt; 2 company Toyota                   1\n#&gt; 3 name    John Smith               2\n#&gt; 4 company google                   2\n#&gt; 5 email   john@google.com          2\n#&gt; 6 name    Huxley Ratcliffe         3<\/code><\/pre>\n<p><\/p>\n<p>Tani q\u00eb kemi nj\u00eb identifikues unik p\u00ebr \u00e7do kontakt, ne mund t\u00eb kthejm\u00eb fush\u00ebn dhe vler\u00ebn n\u00eb kolona:<\/p>\n<p><\/p>\n<pre><code class=\"plaintext\">contacts %&gt;% \n  pivot_wider(names_from = field, values_from = value)<\/code><\/pre>\n<p><\/p>\n<pre><code class=\"plaintext\">#&gt; # A tibble: 3 x 4\n#&gt;   person_id name             company email          \n#&gt;       &lt;int&gt; &lt;chr&gt;            &lt;chr&gt;   &lt;chr&gt;          \n#&gt; 1         1 Jiena McLellan   Toyota  &lt;NA&gt;           \n#&gt; 2         2 John Smith       google  john@google.com\n#&gt; 3         3 Huxley Ratcliffe &lt;NA&gt;    &lt;NA&gt;<\/code><\/pre>\n<p><\/p>\n<h2 id=\"zaklyuchenie\">P\u00ebrfundim<\/h2>\n<p><\/p>\n<p>Sipas mendimit tim, koncepti i ri <strong>tidyr<\/strong> \u00ebsht\u00eb me t\u00eb v\u00ebrtet\u00eb m\u00eb intuitiv dhe tejkalon ndjesh\u00ebm funksionalitetin e funksioneve t\u00eb vjetra. <code>spread()<\/code> dhe <code>gather()<\/code>Shpresoj se ky artikull ju ndihmoi t\u00eb kuptoni <code>pivot_longer()<\/code> dhe <code>pivot_wider()<\/code>.<\/p>\n<p>Burimi: <a content=\"nofollow\" rel=\"nofollow\" href=\"https:\/\/habr.com\/ru\/post\/444622\/\">habr.com<\/a> <\/p>","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"excerpt":{"rendered":"<p>\u041f\u0430\u043a\u0435\u0442 tidyr \u0432\u0445\u043e\u0434\u0438\u0442 \u0432 \u044f\u0434\u0440\u043e \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 R \u2014 tidyverse. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u043a\u0435\u0442\u0430 \u2014 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443. \u041d\u0430 \u0425\u0430\u0431\u0440\u0435 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u044f \u043f\u043e\u0441\u0432\u044f\u0449\u0451\u043d\u043d\u0430\u044f \u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u043f\u0430\u043a\u0435\u0442\u0443, \u043d\u043e \u0434\u0430\u0442\u0438\u0440\u0443\u044e\u0435\u0442\u0441\u044f \u043e\u043d\u0430 2015 \u0433\u043e\u0434\u043e\u043c. \u0410 \u044f \u0445\u043e\u0447\u0443 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u0445, \u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u043d\u0435\u0439 \u043d\u0430\u0437\u0430\u0434 \u0441\u043e\u043e\u0431\u0449\u0438\u043b \u0435\u0433\u043e \u0430\u0432\u0442\u043e\u0440 \u0425\u0435\u0434\u043b\u0438 \u0412\u0438\u043a\u0445\u0435\u043c. [&hellip;]<\/p>\n","protected":false,"gt_translate_keys":[{"key":"rendered","format":"html"}]},"author":1,"featured_media":76292,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[688],"tags":[],"class_list":["post-76291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-administrirovanie"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.2 - aioseo.com -->\n\t<meta name=\"description\" content=\"\u041f\u0430\u043a\u0435\u0442 tidyr \u0432\u0445\u043e\u0434\u0438\u0442 \u0432 \u044f\u0434\u0440\u043e \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 R \u2014 tidyverse. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u043a\u0435\u0442\u0430 \u2014 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443. \u041d\u0430 \u0425\u0430\u0431\u0440\u0435.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Yuri Gagarin\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/prohoster.info\/sq\/blog\/administrirovanie\/r-paket-tidyr-i-ego-novye-funkczii-pivot_longer-i-pivot_wider\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.2\" \/>\n\t\t<meta property=\"og:locale\" content=\"sq_AL\" \/>\n\t\t<meta property=\"og:site_name\" content=\"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"\ud83e\udd47R \u043f\u0430\u043a\u0435\u0442 tidyr \u0438 \u0435\u0433\u043e \u043d\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 pivot_longer \u0438 pivot_wider | ProHoster\" \/>\n\t\t<meta property=\"og:description\" content=\"\u041f\u0430\u043a\u0435\u0442 tidyr \u0432\u0445\u043e\u0434\u0438\u0442 \u0432 \u044f\u0434\u0440\u043e \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 R \u2014 tidyverse. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u043a\u0435\u0442\u0430 \u2014 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443. \u041d\u0430 \u0425\u0430\u0431\u0440\u0435.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/prohoster.info\/sq\/blog\/administrirovanie\/r-paket-tidyr-i-ego-novye-funkczii-pivot_longer-i-pivot_wider\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg\" \/>\n\t\t<meta property=\"og:image:width\" content=\"350\" \/>\n\t\t<meta property=\"og:image:height\" content=\"350\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2020-04-01T11:42:28+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2020-04-01T11:42:28+00:00\" \/>\n\t\t<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/prohoster\" \/>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"\ud83e\udd47Pako R tidyr dhe funksionet e tij t\u00eb reja pivot_longer dhe pivot_wider | ProHoster","description":"Paketa tidyr \u00ebsht\u00eb pjes\u00eb e nj\u00eb nga bibliotekave m\u00eb t\u00eb njohura n\u00eb gjuh\u00ebn R \u2014 tidyverse. Q\u00ebllimi kryesor i paket\u00ebs \u00ebsht\u00eb shnd\u00ebrrimi i t\u00eb dh\u00ebnave n\u00eb nj\u00eb format t\u00eb organizuar. N\u00eb Habr.","canonical_url":"https:\/\/prohoster.info\/sq\/blog\/administrirovanie\/r-paket-tidyr-i-ego-novye-funkczii-pivot_longer-i-pivot_wider","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":null,"og:locale":"sq_AL","og:site_name":"ProHoster | \u041a\u0443\u043f\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u0445\u043e\u0441\u0442\u0438\u043d\u0433 \u0434\u043b\u044f \u0441\u0430\u0439\u0442\u043e\u0432 \u0441 \u0437\u0430\u0449\u0438\u0442\u043e\u0439 \u043e\u0442 DDoS, VPS VDS \u0441\u0435\u0440\u0432\u0435\u0440\u044b","og:type":"article","og:title":"\ud83e\udd47R \u043f\u0430\u043a\u0435\u0442 tidyr \u0438 \u0435\u0433\u043e \u043d\u043e\u0432\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 pivot_longer \u0438 pivot_wider | ProHoster","og:description":"\u041f\u0430\u043a\u0435\u0442 tidyr \u0432\u0445\u043e\u0434\u0438\u0442 \u0432 \u044f\u0434\u0440\u043e \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 R \u2014 tidyverse. \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0430\u043a\u0435\u0442\u0430 \u2014 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u0432\u0438\u0434\u0443. \u041d\u0430 \u0425\u0430\u0431\u0440\u0435.","og:url":"https:\/\/prohoster.info\/sq\/blog\/administrirovanie\/r-paket-tidyr-i-ego-novye-funkczii-pivot_longer-i-pivot_wider","og:image":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:secure_url":"https:\/\/prohoster.info\/wp-content\/uploads\/2021\/11\/logo-350.jpg","og:image:width":350,"og:image:height":350,"article:published_time":"2020-04-01T11:42:28+00:00","article:modified_time":"2020-04-01T11:42:28+00:00","article:publisher":"https:\/\/www.facebook.com\/prohoster","article:author":"https:\/\/www.facebook.com\/prohoster"},"aioseo_meta_data":{"post_id":"76291","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"seo_analyzer_scan_date":null,"breadcrumb_settings":null,"limit_modified_date":false,"reviewed_by":null,"ai":null,"created":"2021-02-28 17:39:23","updated":"2022-09-28 03:21:22","focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"gt_translate_keys":[{"key":"link","format":"url"}],"_links":{"self":[{"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/posts\/76291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/comments?post=76291"}],"version-history":[{"count":0,"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/posts\/76291\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/media\/76292"}],"wp:attachment":[{"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/media?parent=76291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/categories?post=76291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prohoster.info\/sq\/wp-json\/wp\/v2\/tags?post=76291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}