<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[code_R - Medium]]></title>
        <description><![CDATA[This is a page to post R codes used in the analysis of different data sets. - Medium]]></description>
        <link>https://medium.com/code-r?source=rss----d797b6d754c---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>code_R - Medium</title>
            <link>https://medium.com/code-r?source=rss----d797b6d754c---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 22:28:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/code-r" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[R Code for Covid top 6 Countries]]></title>
            <link>https://medium.com/code-r/r-code-for-covid-top-6-countries-d35e26cbd9dc?source=rss----d797b6d754c---4</link>
            <guid isPermaLink="false">https://medium.com/p/d35e26cbd9dc</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[india]]></category>
            <category><![CDATA[world]]></category>
            <category><![CDATA[covid19]]></category>
            <dc:creator><![CDATA[Indranil Chowdhury]]></dc:creator>
            <pubDate>Thu, 03 Sep 2020 05:21:21 GMT</pubDate>
            <atom:updated>2020-09-03T05:37:18.101Z</atom:updated>
            <content:encoded><![CDATA[<h3>R Code for Covid top 6 Countries*</h3><h4><strong>*Written by Subhanil (subhanilc@gmail.com) and I.</strong></h4><p>This code is to prepare and analyze the Covid-19 cases in the world. Specifically, this code will generate the progression of the disease in the top 6 countries, according to number of latest confirmed Covid cases. We will undertake a step by step formulation of how to do it.</p><pre>install.packages(&quot;tidyverse&quot;)<br>install.packages(&quot;ggthemes&quot;)<br>install.packages(&quot;zoo&quot;)<br>install.packages(&quot;gtable&quot;)<br>install.packages(&quot;grid&quot;)<br>install.packages(&quot;gridextra&quot;)<br>install.packages(&quot;egg&quot;)</pre><pre>library(tidyverse)<br>library(ggthemes)<br>library(zoo)<br>library(gtable)<br>library(grid)<br>library(gridExtra)<br>library(egg)</pre><p>Once we have called the libraries, we need to get the date for the analysis. This is obtained from the website, <a href="http://www.ourworldindata.org/">www.ourworldindata.org</a> directly through the following command. When the code is run successfully, the data will be seen in the global environment as a dataframe named ‘datacovid’.</p><pre>datacovid &lt;- read.csv(&quot;<a href="https://covid.ourworldindata.org/data/owid-covid-data.csv">https://covid.ourworldindata.org/data/owid-covid-data.csv</a>&quot;)</pre><p>After we get the data, we can see the variables through the following set of code.</p><pre>str(datacovid)</pre><pre>## &#39;data.frame&#39;:    41170 obs. of  40 variables:<br> ##  $ iso_code                       : Factor w/ 212 levels &quot;&quot;,&quot;ABW&quot;,&quot;AFG&quot;,..: 2 2 2 2 2 2 2 2 2 2 ...<br> ##  $ continent                      : Factor w/ 7 levels &quot;&quot;,&quot;Africa&quot;,&quot;Asia&quot;,..: 5 5 5 5 5 5 5 5 5 5 ...<br> ##  $ location                       : Factor w/ 212 levels &quot;Afghanistan&quot;,..: 10 10 10 10 10 10 10 10 10 10 ...<br> ##  $ date                           : Factor w/ 247 levels &quot;2019-12-31&quot;,&quot;2020-01-01&quot;,..: 74 80 81 82 83 84 85 86 87 88 ...<br> ##  $ total_cases                    : num  2 NA 4 NA NA NA 12 17 19 28 ...<br> ##  $ new_cases                      : num  2 NA 2 NA NA NA 8 5 2 9 ...<br> ##  $ new_cases_smoothed             : num  NA 0.286 0.286 0.286 0.286 ...<br> ##  $ total_deaths                   : num  0 NA 0 NA NA NA 0 0 0 0 ...<br> ##  $ new_deaths                     : num  0 NA 0 NA NA NA 0 0 0 0 ...<br> ##  $ new_deaths_smoothed            : num  NA 0 0 0 0 0 0 0 0 0 ...<br> ##  $ total_cases_per_million        : num  18.7 NA 37.5 NA NA ...<br> ##  $ new_cases_per_million          : num  18.7 NA 18.7 NA NA ...<br> ##  $ new_cases_smoothed_per_million : num  NA 2.68 2.68 2.68 2.68 ...<br> ##  $ total_deaths_per_million       : num  0 NA 0 NA NA NA 0 0 0 0 ...<br> ##  $ new_deaths_per_million         : num  0 NA 0 NA NA NA 0 0 0 0 ...<br> ##  $ new_deaths_smoothed_per_million: num  NA 0 0 0 0 0 0 0 0 0 ...<br> ##  $ new_tests                      : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ total_tests                    : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ total_tests_per_thousand       : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ new_tests_per_thousand         : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ new_tests_smoothed             : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ new_tests_smoothed_per_thousand: num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ tests_per_case                 : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ positive_rate                  : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ tests_units                    : Factor w/ 6 levels &quot;&quot;,&quot;people tested&quot;,..: 1 1 1 1 1 1 1 1 1 1 ...<br> ##  $ stringency_index               : num  0 33.3 33.3 44.4 44.4 ...<br> ##  $ population                     : num  106766 106766 106766 106766 106766 ...<br> ##  $ population_density             : num  585 585 585 585 585 ...<br> ##  $ median_age                     : num  41.2 41.2 41.2 41.2 41.2 41.2 41.2 41.2 41.2 41.2 ...<br> ##  $ aged_65_older                  : num  13.1 13.1 13.1 13.1 13.1 ...<br> ##  $ aged_70_older                  : num  7.45 7.45 7.45 7.45 7.45 ...<br> ##  $ gdp_per_capita                 : num  35974 35974 35974 35974 35974 ...<br> ##  $ extreme_poverty                : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ cardiovasc_death_rate          : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ diabetes_prevalence            : num  11.6 11.6 11.6 11.6 11.6 ...<br> ##  $ female_smokers                 : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ male_smokers                   : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ handwashing_facilities         : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ hospital_beds_per_thousand     : num  NA NA NA NA NA NA NA NA NA NA ...<br> ##  $ life_expectancy                : num  76.3 76.3 76.3 76.3 76.3 ...</pre><p>It is seen from above that the class of the Date variable is not according to our desired format. Therefore, we convert that variable into a Date using the as.Date command. Additionally, we need to know the latest date for which the data is available. We assign the latest date to a variable d with the max command run on the date variable.</p><pre>datacovid$date &lt;- as.Date(datacovid$date)<br>d &lt;-max(datacovid$date)</pre><p>Now, we need to identify the countries having the largest number of infections. For this, we run two sets of codes. Firstly, we filter the data for the latest date and group the data into countries (iso_code variable) and get the number of cases in the latest date. The code is as follows:</p><pre>df1 &lt;- datacovid %&gt;% <br>  filter(date== max(datacovid$date) ) %&gt;% <br>  group_by(iso_code, total_cases) %&gt;% <br>  summarise()</pre><p>The df1 dataframe has the countries and the total number of cases for the latest date. But we need to arrange the countries in a descending order of cases. This is done through the following codes.</p><pre>df1 &lt;- df1[order(df1$total_cases, decreasing = T),]<br>df1</pre><pre>## # A tibble: 210 x 2<br> ## # Groups:   iso_code [210]<br> ##    iso_code total_cases<br> ##    &lt;fct&gt;          &lt;dbl&gt;<br> ##  1 OWID_WRL    25776601<br> ##  2 USA          6075652<br> ##  3 BRA          3950931<br> ##  4 IND          3769523<br> ##  5 RUS          1000048<br> ##  6 PER           657129<br> ##  7 ZAF           628259<br> ##  8 COL           624069<br> ##  9 MEX           606036<br> ## 10 ARG           417722<br> ## # ... with 200 more rows</pre><p>When we execute the code, we have a problem. World as a whole becomes the number 1 country, because it obviously has the maximum number of cases. So we need to get rid of the iso_code labeled World and extract the top 6 countries by using the [] operator and assign in to a vector named top_6. The code is as follows:</p><pre>df1 &lt;- df1[-1,]<br>top_6 &lt;- as.character(df1[1:6,]$iso_code)</pre><p>Now we have the names of the top 6 countries according to Covid cases. We now will now draw plots of these countries together. For that we create a function which does three things: 1) Select the relevant variables, 2) filter the date for cases&gt;50 and 3) calculate the 7 day moving average. The code for the function is given below.</p><pre>country &lt;- function(df){<br>  df_country &lt;- datacovid %&gt;% <br>  select(iso_code, location, date, new_cases, total_cases) %&gt;%<br>  filter(iso_code == df, new_cases&gt;=50) %&gt;% <br>  mutate(mov_avg = rollmean(new_cases, 7, fill = NA, align = right&quot;))<br>  assign(df, df_country)}</pre><p>Now, with this function, we will create a list which will include the relevant date for the top 6 countries. The code is as follows:</p><pre>df_list &lt;- map(top_6, country )</pre><p>Now we need to create another function which will create the relevant plots. For this we use the <em>ggplot2</em> package.</p><pre>gr &lt;- function(df){ggplot(data = df, aes(date, new_cases))+<br>    geom_col(fill = &quot;Blue&quot;, colour = &quot;black&quot;) +<br>    geom_line(aes(date, mov_avg), data = df, colour=&quot;red&quot;,size=1)+<br>    xlab(NULL) + ylab(NULL) + theme_bw()+<br>    ggtitle(df[1,2])}</pre><p>With this gr function we create another list of plots by using the map function.</p><pre>plots &lt;- map(df_list, gr)</pre><p>The list named ‘plots’ now contains all the six necessary graphs that we want to construct. What remains is to plot them all in a single page. Here library grid and gridextra comes handy along with the command grid.arrange . The code is given below.</p><pre>grid.newpage()</pre><pre>grid.arrange(grobs = plots, <br>             top = textGrob(paste(&quot;Top Six Countries on&quot;, d, sep = &quot; &quot;), vjust = 1, gp = gpar(fontface = &quot;bold&quot;, cex = 1.0)),<br>             left = textGrob(&quot;New cases&quot;, rot = 90, vjust = 1),<br>             bottom = textGrob(&quot;Created by Subhanil &amp; Indranil. Source: ourworldindata.org&quot;))</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*zOIF0jjPxyAGAWo1ehjRzA.png" /><figcaption>The Rplot output of the code</figcaption></figure><p>Obviously, many more operations can be done as per your requirements. This was just an illustration!</p><p>Happy Coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d35e26cbd9dc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/code-r/r-code-for-covid-top-6-countries-d35e26cbd9dc">R Code for Covid top 6 Countries</a> was originally published in <a href="https://medium.com/code-r">code_R</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>