<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r-tip-use-named-vectors-to-re-map-values/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/03/r-tip-use-named-vectors-to-re-map-values/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Here is an <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">tip</a>. Want to re-map a column of values? Use a named vector as the mapping.</p>
<p><span></span></p>
<p>Example:</p>
<pre>
library(&quot;dplyr&quot;)
library(&quot;wrapr&quot;)

head(starwars[, qc(name, gender)])

# # A tibble: 6 x 2
# name           gender
# </pre>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r-tip-use-let-to-re-map-names/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/03/r-tip-use-let-to-re-map-names/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Another <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">tip</a>. Need to replace a name in some R code or make R code re-usable? Use <a rel="nofollow" href="https://winvector.github.io/wrapr/reference/let.html" target="_blank"><code>wrapr::let()</code></a>.</p>
<p><center><br>
<a rel="nofollow" href="https://cran.r-project.org/package=wrapr" target="_blank"><img src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2017/08/wraprs.png?w=660" data-recalc-dims="1"></a><br>
</center></p>
<p><span></span></p>
<p>Here is an example involving <a rel="nofollow" href="https://cran.r-project.org/package=dplyr" target="_blank"><code>dplyr</code></a>.</p>
<p>Let’s look at some example data:</p>
<pre>
library(&quot;dplyr&quot;)
library(&quot;wrapr&quot;)

starwars %>%
  select(., name, homeworld, species) %>%
  head(.)

# # A tibble: 6 x 3
# name           homeworld species
# </pre>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/on-mimes-software-versions-and-web-site-promiscuity-a-k-a-three-new-packages-to-round-out-the-week/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2018/03/23/on-mimes-software-versions-and-web-site-promiscuity-a-k-a-three-new-packages-to-round-out-the-week/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A quick Friday post to let folks know about three in-development R packages that you’re encouraged to poke the tyres o[fn] and also jump in and file issues or PRs for.</p>
<h3>Alleviating aversion to versions</h3>
<p>I introduced a “version chart” in a <a rel="nofollow" href="https://rud.is/b/2018/03/07/handling-semantic-version-string-like-a-boss-with-the-semver-package/" target="_blank">recent post</a> and one key element of tagging years (which are <em>really</em> helpful to get a feel for scope of exposure + technical/cyber-debt) is knowing the dates of product version releases. You can pay for such a database but it’s also possible to cobble one together, and that activity will be much easier as time goes on with the <a rel="nofollow" href="https://github.com/hrbrmstr/vershist" target="_blank"><code>vershist</code><img alt="🔗" src="https://i2.wp.com/s.w.org/images/core/emoji/2.4/72x72/1f517.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></a> package.</p>
<p>Here’s a sample:</p>
<pre><code>apache_httpd_version_history()
## # A tibble: 29 x 8
##    vers   rls_date   rls_year major minor patch prerelease build
##    </code></pre>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r-tip-use-stringsasfactors-false/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/03/r-tip-use-stringsasfactors-false/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> <a rel="nofollow" href="http://www.win-vector.com/blog/tag/r-tips/" target="_blank">tip</a>: use <code>stringsAsFactors = FALSE</code>.</p>
<p>R often uses a concept of <code>factor</code>s to re-encode strings. This can be too early and too aggressive. Sometimes a string is just a string.</p>
<p><center><br>
<img border="0" src="https://i1.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/03/800px-Sigmund_Freud_by_Max_Halberstadt_cropped.jpg?resize=441%2C600" alt="800px Sigmund Freud by Max Halberstadt cropped" width="441" title="800px-Sigmund_Freud,_by_Max_Halberstadt_(cropped).jpg" data-recalc-dims="1" height="600">
<p>
<p>Sigmund Freud, it is often claimed, said: “Sometimes a cigar is just a cigar.”<br>
</p>
<p><span></span></p>
<p>To avoid problems delay re-encoding of strings by using <code>stringsAsFactors = FALSE</code> when creating <code>data.frame</code>s.</p>
<p>Example:</p>
<pre>
d &lt;- data.frame(label = rep(&quot;tbd&quot;, 5))

d$label[[2]] &lt;- &quot;north&quot;
#> Warning in `[[&lt;-.factor`(`*tmp*`, 2, value = structure(c(1L, NA, 1L, 1L, :
#> invalid factor level, NA generated

print(d)
#>   label
#> 1   tbd
#> 2  </pre></center>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/installing-package-dependencies-without-external-https-requests/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.peteredewitt.com/dewittpe.github.io/dewittpe.github.io/2018/installing-dependencies/"> Peter E. DeWitt</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Consider you have a server that is running behind a firewall and, for security<br>
reasons, cannot make external http(s) requests. Further, you have R running on<br>
this server and you need to install a set of packages. The simple approach of</p>
<div>
<div>
<pre><code>install.packages(&quot;</code></pre></div></div>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/our-logo-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.jumpingrivers.com/posts/2018/logo_in_r/"> R on The Jumping Rivers Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Hi all, so given our logo here at Jumping Rivers is a set of lines designed to look like a Gaussian Process, we thought it would be a neat idea to recreate this image in R. To do so we’re going to need a couple packages. We do the usual <code>install.packages()</code> dance (remember this step can be performed in <a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2017/speed_package_installation/" target="_blank">parallel</a>)</p>
<pre><code>install.packages(c(&quot;ggplot2&quot;, &quot;ggalt&quot;, &quot;readr&quot;))</code></pre>
<p>We’re also going to need the data containing the points for the lines and which set of points belongs to which line. There is a Gist available to download via <a rel="nofollow" href="https://gist.github.com/jumpingrivers/6e88357ef28697c612bb5553251a473d" target="_blank">Jumping Rivers</a>. To read in the csv file we’re going to use the raw data link.</p>
<pre><code>(dd = readr::read_csv(&quot;https://goo.gl/HzNbAp&quot;, col_types = &quot;ddc&quot;))
## # A tibble: 40 x 3
##       x     y type 
##   </code></pre>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-highlight-countries-on-a-map/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/highlight-countries-on-map/"> r-bloggers – SHARP SIGHT LABS</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a href="http://sharpsightlabs.com/join-newsletter-rapid-mastery/"><br>
<img sizes="(max-width: 1586px) 100vw, 1586px" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/12/world_oil_production_OPEC-highlight_MAP.png?w=450" alt="" class="wp-image-3197" data-recalc-dims="1"><br>
</a></p>
<p>Last week, we created a <a href="http://sharpsightlabs.com/blog/map-oil-production-country-r/">map of world oil production, by country</a>.</p>
<p>This week, I’ll show you how to make a slight modification. I’ll show you how to highlight specific countries according to a variable in your data frame.</p>
<p>In this code, we will re-use our data from the last tutorial. You can <a href="http://sharpsightlabs.com/blog/map-oil-production-country-r/">find the code to create the data there</a>.</p>
<p>Before we do any plotting, let’s just inspect the data:</p>
<pre class="wp-code-highlight">
map.oil %>% glimpse()

# Observations: 99,338
# Variables: 9
# $ long            </pre>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/r%e2%81%b6-series-random-sampling-from-apache-drill-tables-with-r-sergeant/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2017/12/20/r%E2%81%B6-series-random-sampling-from-apache-drill-tables-with-r-sergeant/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><span>(For first-timers, R⁶ tagged posts are short &amp; sweet with minimal expository; <a>R⁶ feed</a>)</span></p>
<p>At work-work I mostly deal with medium-to-large-ish data. I often want to poke at new or existing data sets w/o working across billions of rows. I also use Apache Drill for much of my exploratory work.</p>
<p>Here’s how to uniformly sample data from Apache Drill using the <code>sergeant</code> package:</p>
<pre><code>library(sergeant)

db &lt;- src_drill(&quot;sonar&quot;)
tbl &lt;- tbl(db, &quot;dfs.dns.`aaaa.parquet`&quot;)

summarise(tbl, n=n())
## # Source:   lazy query [?? x 1]
## # Database: DrillConnection
##          n
##      </code></pre>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/live-earthquakes-app/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.analyticsforfun.com/2017/12/live-earthquakes-app.html"> analytics for fun</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It’s awesome when you are asked to build a product demo and you end up building something you actually use yourself.</p>
<p>That is what happened to me with the Live Earthquake Shiny App.  A few…</p>
<p>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/visualising-ssh-attacks-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.enchufa2.es/archives/visualising-ssh-attacks-with-r.html"> R – Enchufa2</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>If you have any machine with an SSH server open to the world and you take a look at your logs, you may be alarmed to see so many login attempts from so many unknown IP addresses. <a rel="nofollow" href="http://denyhosts.sourceforge.net/" target="_blank">DenyHosts</a> is a pretty neat service for Unix-based systems which works in the background reviewing such logs and appending the offending addresses into the <code>hosts.deny</code> file, thus avoiding brute-force attacks.</p>
<p>The following R snippet may be useful to quickly visualise a <code>hosts.deny</code> file with logs from DenyHosts. Such file may have comments (lines starting with <code>#</code>), and actual records are stored in the form <code></code>
<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/store-data-about-rows/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.questionflow.org/2017/11/20/store-data-about-rows/"> QuestionFlow </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> Introduction to keyholder package. Tools for keeping track of information about rows.</p>
<div>
<h1>Prologue</h1>
<p>During development of my other R package (<a rel="nofollow" href="https://github.com/echasnovski/ruler" target="_blank">ruler</a>), I encountered the following problem: how to track rows of data frame after application of some <strong>user defined</strong> function? It is assumed that this function takes data frame as input, subsets it (with possible creation of new columns, but not rows) and returns the result. The typical example using <a rel="nofollow" href="http://dplyr.tidyverse.org/" target="_blank">dplyr</a> and <a rel="nofollow" href="http://magrittr.tidyverse.org/" target="_blank">magrittr</a>’s pipe:</p>
<pre><code>suppressMessages(library(dplyr))

# Custom `mtcars` for more clear explanation
mtcars_tbl &lt;- mtcars %>%
  select(mpg, vs, am) %>%
  as_tibble()

# A handy way of creating function with one argument
modify &lt;- . %>%
  mutate(vs_am = vs * am) %>%
  filter(vs_am == 1) %>%
  arrange(desc(mpg))

# The question is: which rows of `mtcars_tbl` are returned?
mtcars_tbl %>% modify()
## # A tibble: 7 x 4
##     mpg    vs    am vs_am
##   </code></pre></div>
<p>I’ve been using the <a rel="nofollow" href="http://ggplot2.tidyverse.org/" target="_blank">ggplot2</a> package a lot recently. When creating a legend or tick marks on the axes, ggplot2 uses the levels of a character or factor vector. Most of the time, I am working with coded variables that use some abbreviation of the “true” meaning (e.g. “f” for female and “m” for male or single characters for some single character for a location: “S” for Stuttgart and “M” for Mannheim).</p>
<p>In my plots, I don’t want these codes but the full name of the level. Since I am not aware of any super-fast and easy to use function in base R (let me know in the comments if there is one), I came up with a very simple function and put this in my .Rprofile (that means that it is available whenever I start R). I called it “<b>replace.levels</b>“. The dot before the name means that it is invisible and does not show up in my Global Environment overview in RStudio. You have to call it with .replace.levels(