<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>R on Alok Regmi</title>
		<link>https://alokregmi.com/tags/r/</link>
		<description>Recent content in R on Alok Regmi</description>
		<generator>Hugo</generator>
		<language>en-us</language>
		
		
		
		
			<lastBuildDate>Tue, 22 Sep 2020 08:34:51 +0545</lastBuildDate>
		
			<atom:link href="https://alokregmi.com/tags/r/index.xml" rel="self" type="application/rss+xml" />
			<item>
				<title>R Programming 101- Datacamp Beginner</title>
				<link>https://alokregmi.com/braindump/2020/09/r-programming-101-datacamp-beginner/</link>
				<pubDate>Tue, 22 Sep 2020 00:00:00 +0545</pubDate>
				<guid>https://alokregmi.com/braindump/2020/09/r-programming-101-datacamp-beginner/</guid>
				<description>&lt;h2 id=&#34;datacamp-beginner-course-notes&#34;&gt;Datacamp Beginner Course Notes&lt;/h2&gt;&#xA;&lt;h3 id=&#34;symbol&#34;&gt;Symbol&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Assign is used as &amp;lt;- rather than =.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;arithmetic-operations&#34;&gt;Arithmetic Operations&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Same as other languages.&lt;/li&gt;&#xA;&lt;li&gt;Modulo is represented by %%.&lt;/li&gt;&#xA;&lt;li&gt;Logical variables True or False represented by &lt;strong&gt;TRUE&lt;/strong&gt; and &lt;strong&gt;FALSE&lt;/strong&gt;.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;matrix-and-vectors&#34;&gt;Matrix and Vectors&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;Index starts at 1&lt;/strong&gt;&lt;/li&gt;&#xA;&lt;li&gt;Methods:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;rowSum&lt;/strong&gt; : Find sum across a row.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;colSum&lt;/strong&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;colnames&lt;/strong&gt;: Specify column names to a matrix.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;rowNames&lt;/strong&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;factors&#34;&gt;Factors&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Basically these are used for categorical variables.&lt;/li&gt;&#xA;&lt;li&gt;If we have a vector with categorical values, we can use &lt;em&gt;factor()&lt;/em&gt; to get it&amp;rsquo;s categorical values.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;types&#34;&gt;Types&lt;/h3&gt;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;&lt;strong&gt;Nominal&lt;/strong&gt; : Categorical Variable without implied order.&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Ordinal&lt;/strong&gt;: Categorical Variable with order.&#xA;&lt;ul&gt;&#xA;&lt;li&gt;e.g. [0, 1, 2]&lt;/li&gt;&#xA;&lt;li&gt;Defined by passing parameter &lt;em&gt;order = TRUE&lt;/em&gt;.&lt;/li&gt;&#xA;&lt;li&gt;Also need to pass levels as &lt;em&gt;levels = vector&lt;/em&gt;.&lt;/li&gt;&#xA;&lt;li&gt;By specifying levels for the input vector, we can specify levels and then later use &lt;em&gt;summary(factors_vector)&lt;/em&gt; to get the summary on it&amp;rsquo;s levels.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;&lt;h3 id=&#34;dataframe&#34;&gt;DataFrame&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Creating:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;data.frame(array1, array2&amp;hellip; )&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;Indexing:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;df$column to get only one column of data.&lt;/li&gt;&#xA;&lt;li&gt;Similar indexing as numpy matrices&lt;/li&gt;&#xA;&lt;li&gt;Also can use &amp;ldquo;column names&amp;rdquo; to get elements by index.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;Filtering :&#xA;&lt;ul&gt;&#xA;&lt;li&gt;subset(df, condition)&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;Sorting:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;order()&lt;/li&gt;&#xA;&lt;li&gt;Using order to sort whole dataframe, we need to get index using order() and&#xA;later use it to sort by indexing into the dataframe as &lt;strong&gt;df[sorted_indexes, ]&lt;/strong&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;p&gt;&lt;strong&gt;NOTE: We have a built in dataframe known as mtcars.&lt;/strong&gt;&lt;/p&gt;</description>
			</item>
			<item>
				<title>R Programming 101- Datacamp Intermediate</title>
				<link>https://alokregmi.com/braindump/2020/09/r-programming-101-datacamp-intermediate/</link>
				<pubDate>Tue, 22 Sep 2020 00:00:00 +0545</pubDate>
				<guid>https://alokregmi.com/braindump/2020/09/r-programming-101-datacamp-intermediate/</guid>
				<description>&lt;h2 id=&#34;datacamp-intermediate-course-notes&#34;&gt;Datacamp Intermediate Course Notes&lt;/h2&gt;&#xA;&lt;h3 id=&#34;logical-operators&#34;&gt;Logical Operators&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&amp;amp; means and and | means or operator.&lt;/li&gt;&#xA;&lt;li&gt;&amp;amp;&amp;amp; and || will first do element wise comparison on just the first element and&#xA;give a single result.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;conditionals&#34;&gt;Conditionals&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;if (condition){ } else if (condition2) {} else {}&lt;/li&gt;&#xA;&lt;li&gt;However the else statement should be written on the same line as closing brace&#xA;of if statement.&lt;/li&gt;&#xA;&lt;li&gt;while(condition) {expression}&lt;/li&gt;&#xA;&lt;li&gt;break for breaking the loop&lt;/li&gt;&#xA;&lt;li&gt;for(item in item_list) { operate on item }&lt;/li&gt;&#xA;&lt;li&gt;for (i in 1:length(list)) { operate on list[i] }&lt;/li&gt;&#xA;&lt;li&gt;Double brackets when subsetting over a list.&lt;/li&gt;&#xA;&lt;li&gt;nchar : Number of characters&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;functions&#34;&gt;Functions&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;help(fn_name) or ?fn_name to get documentation&lt;/li&gt;&#xA;&lt;li&gt;Argument matching by position&lt;/li&gt;&#xA;&lt;li&gt;fn_name &amp;lt;- function(args) {fn_body}&lt;/li&gt;&#xA;&lt;li&gt;R passes arguments by values.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;r-packages&#34;&gt;R Packages&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;install.packages(&amp;ldquo;pkg_name&amp;rdquo;)  from CRAN Comprehensive R Archive Network&lt;/li&gt;&#xA;&lt;li&gt;Load packages:  library(&amp;ldquo;pkg_name&amp;rdquo;) and library(pkg_name) both works&lt;/li&gt;&#xA;&lt;li&gt;View loaded packages: search()&lt;/li&gt;&#xA;&lt;li&gt;7 packages loaded by default such as base package, globalEnv  and so on.&lt;/li&gt;&#xA;&lt;li&gt;require()  : Also works with and without string&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;lapply&#34;&gt;Lapply()&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Apply a functions to all items of a list one by one similar to map() in python.&lt;/li&gt;&#xA;&lt;li&gt;Also it maintains the output list names as well from the original list.&lt;/li&gt;&#xA;&lt;li&gt;For using operations on simple lists, we may not want names then we can use unlist() to actually get the same thing as operating one by one.&lt;/li&gt;&#xA;&lt;li&gt;Also we can define some other function that we want to apply over all items one by one and use lapply for it.&lt;/li&gt;&#xA;&lt;li&gt;We can have additional arguments as well for the operations.&lt;/li&gt;&#xA;&lt;li&gt;Can also use similar to λ function in python using function() {} directly inside the lapply call.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;sapply-simplify-apply&#34;&gt;Sapply(): Simplify apply&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Generally lapply() can return heterogenous contents which may not be useful in all the cases that we are working with.&lt;/li&gt;&#xA;&lt;li&gt;We can give option to not use original names from the original object using USE.NAMES = FALSE.&lt;/li&gt;&#xA;&lt;li&gt;Also with some functions that return NULL when called with lapply, sapply works pretty fine. e.g. sum(), mean() and so on.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;vapply&#34;&gt;Vapply()&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Need to specify output type&lt;/li&gt;&#xA;&lt;li&gt;Lapply under the hood&lt;/li&gt;&#xA;&lt;li&gt;FUN.VALUE =&amp;gt; to specify what to return. e.g. numeric(1) will say that the output should be a single number.&lt;/li&gt;&#xA;&lt;li&gt;There can be problems if output size differs from what you specify.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;useful-functions&#34;&gt;Useful Functions&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;seq(1,10,by=3)&lt;/li&gt;&#xA;&lt;li&gt;seq(8,2,by=-2)&lt;/li&gt;&#xA;&lt;li&gt;rep(list, times=n)&lt;/li&gt;&#xA;&lt;li&gt;rep(list, each=n) : repeat element by element&lt;/li&gt;&#xA;&lt;li&gt;sort(list, decreasing=TRUE)&lt;/li&gt;&#xA;&lt;li&gt;str(): structure of component&lt;/li&gt;&#xA;&lt;li&gt;is.*(): listobj.islist()&lt;/li&gt;&#xA;&lt;li&gt;append(list, another_list)&lt;/li&gt;&#xA;&lt;li&gt;as.*() : convert other objects to list. e.g. vector to list&lt;/li&gt;&#xA;&lt;li&gt;rev(list) : reverses a list&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;regex&#34;&gt;Regex&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;grep, grepl, sub, gsub&lt;/li&gt;&#xA;&lt;li&gt;grepl(pattern, list): Returns truth values&lt;/li&gt;&#xA;&lt;li&gt;grep(pattern, list): Returns the indexes of matches.&lt;/li&gt;&#xA;&lt;li&gt;which(grepl(&amp;hellip;)): returns similar thing to grep&lt;/li&gt;&#xA;&lt;li&gt;sub(pattern, replacement, list): replace one regex with the other. similar to re.sub() in python. &lt;strong&gt;Looks only for first match in the string&lt;/strong&gt;.&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;h3 id=&#34;times-and-dates&#34;&gt;Times and Dates&lt;/h3&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;Sys.Date() for today&lt;/li&gt;&#xA;&lt;li&gt;Sys.time() with time in small case gets time and date as well.&lt;/li&gt;&#xA;&lt;li&gt;as.Date(&amp;ldquo;YYYY-MM-DD&amp;rdquo;) ISO format&lt;/li&gt;&#xA;&lt;li&gt;else pass format to as.Date(format= &amp;ldquo;%Y-%d-%m&amp;rdquo;) and it will parse it properly  for all kinds of dates&lt;/li&gt;&#xA;&lt;li&gt;date can be added to numbers and also subtracted from each other.&lt;/li&gt;&#xA;&lt;li&gt;time object increments time object by 1 second, date object increments date object by 1 day when +1 is used.&lt;/li&gt;&#xA;&lt;li&gt;unclass(my_date) shows that the date is represented by simple numbers by which such operations can be done.&lt;/li&gt;&#xA;&lt;li&gt;Other packages for this: &amp;ldquo;lubridate&amp;rdquo;, &amp;ldquo;zoo&amp;rdquo;, &amp;ldquo;xts&amp;rdquo;&lt;/li&gt;&#xA;&lt;li&gt;Date Formats:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;%Y: 4-digit year (1982)&lt;/li&gt;&#xA;&lt;li&gt;%y: 2-digit year (82)&lt;/li&gt;&#xA;&lt;li&gt;%m: 2-digit month (01)&lt;/li&gt;&#xA;&lt;li&gt;%d: 2-digit day of the month (13)&lt;/li&gt;&#xA;&lt;li&gt;%A: weekday (Wednesday)&lt;/li&gt;&#xA;&lt;li&gt;%a: abbreviated weekday (Wed)&lt;/li&gt;&#xA;&lt;li&gt;%B: month (January)&lt;/li&gt;&#xA;&lt;li&gt;%b: abbreviated month (Jan)&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;li&gt;format(dateobject, &amp;ldquo;%b %Y&amp;rdquo;) would show date as Jan 1987. Similarly, more can be done.&lt;/li&gt;&#xA;&lt;li&gt;Time Formats:&#xA;&lt;ul&gt;&#xA;&lt;li&gt;%H: hours as a decimal number (00-23)&lt;/li&gt;&#xA;&lt;li&gt;%I: hours as a decimal number (01-12)&lt;/li&gt;&#xA;&lt;li&gt;%M: minutes as a decimal number&lt;/li&gt;&#xA;&lt;li&gt;%S: seconds as a decimal number&lt;/li&gt;&#xA;&lt;li&gt;%T: shorthand notation for the typical format %H:%M:%S&lt;/li&gt;&#xA;&lt;li&gt;%p: AM/PM indicator&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;!--list-separator--&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&#xA;&lt;p&gt;Example&lt;/p&gt;&#xA;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-jupyter-R&#34; data-lang=&#34;jupyter-R&#34;&gt;# Definition of character strings representing times&#xA;str1 &amp;lt;- &amp;#34;May 23, &amp;#39;96 hours:23 minutes:01 seconds:45&amp;#34;&#xA;str2 &amp;lt;- &amp;#34;2012-3-12 14:23:08&amp;#34;&#xA;&#xA;# Convert the strings to POSIXct objects: time1, time2&#xA;time1 &amp;lt;- as.POSIXct(str1, format = &amp;#34;%B %d, &amp;#39;%y hours:%H minutes:%M seconds:%S&amp;#34;)&#xA;time2 &amp;lt;- as.POSIXct(str2)&#xA;&#xA;# Convert times to formatted strings&#xA;format(time1, &amp;#34;%M&amp;#34;)&#xA;format(time2, &amp;#34;%I:%M %p&amp;#34;)&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;pre tabindex=&#34;0&#34;&gt;&lt;code class=&#34;language-jupyter-R&#34; data-lang=&#34;jupyter-R&#34;&gt;df = data.frame(&#xA;x = c(4,5,6),&#xA;y = c(&amp;#34;a&amp;#34;, &amp;#34;b&amp;#34;, &amp;#34;c&amp;#34;)&#xA;)&#xA;class(df)&#xA;df[&amp;#34;x&amp;#34;] &amp;lt;- c(1,2,3)&#xA;df&#xA;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;</description>
			</item>
	</channel>
</rss>
