<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>R Archives - Coding Campus</title>
	<atom:link href="https://codingcampus.net/category/r/feed/" rel="self" type="application/rss+xml" />
	<link>https://codingcampus.net/category/r/</link>
	<description>Learn to Code</description>
	<lastBuildDate>Fri, 02 Dec 2022 19:35:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://codingcampus.net/wp-content/uploads/2022/11/Coding-Campus-Logo-150x150.png</url>
	<title>R Archives - Coding Campus</title>
	<link>https://codingcampus.net/category/r/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">212689819</site>	<item>
		<title>print() Function in R</title>
		<link>https://codingcampus.net/print-function-in-r/</link>
					<comments>https://codingcampus.net/print-function-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Fri, 17 Mar 2023 19:55:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1312</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/print-function-in-r/">print() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The <strong>print()</strong> function allows you to print objects to the console. For example, you can use the print() function to print vectors, matrices, data frames, and lists. In addition, the print() function can be used to print the results of a computation. This guide will discuss using the <strong>print()</strong> in R.</p>
<h4>Syntax</h4>
<pre><code>print(x)</code></pre>
<h4>Arguments</h4>
<ul>
<li><strong>x</strong> = The object can be of any type, including vectors, matrices, data frames, and lists.</li>
</ul>
<h3>Example 1: Print Values using print() function</h3>
<pre><code>#printing values
print("R Programming Guide")</code></pre>
<h4>Output</h4>
<pre><code>[1] "R Programming Guide"</code></pre>
<h3>Example 2: Print Variables using print() function</h3>
<pre><code>#creating a variable
x &lt;- 25
#printing variable
print(x)</code></pre>
<h4>Output</h4>
<pre><code>[1] 25</code></pre>
<h3>Example 3: Print a Vector using print() function</h3>
<pre><code>#creating a vector
x &lt;- c(1:5)
#printing vector
print(x)</code></pre>
<h4>Output</h4>
<pre><code>[1] 1 2 3 4 5</code></pre>
<h3>Example 4: Print a List using print() function</h3>
<pre><code>#creating a list
x &lt;- list('Bruce','Wayne',1.6,'Martial Arts')
#printing list
print(x)</code></pre>
<h4>Output</h4>
<pre><code>[[1]]
[1] "Bruce"

[[2]]
[1] "Wayne"

[[3]]
[1] 1.6

[[4]]
[1] "Martial Arts"</code></pre>
<h3>Example 5: Print a Data Frame using print() Function</h3>
<pre><code>#printing the preloaded iris data frame
print(iris)</code></pre>
<h4>Output</h4>
<figure id="attachment_25710" aria-describedby="caption-attachment-25710" style="width: 590px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" class="size-medium wp-image-25710" src="http://codingcampus.net/wp-content/uploads/2022/09/Print-Data-Frame-600x464-1.jpg" alt="Print Data Frame" width="600" height="464" /><figcaption id="caption-attachment-25710" class="wp-caption-text">Printing data frame to the console.</figcaption></figure>
<p>The post <a href="https://codingcampus.net/print-function-in-r/">print() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/print-function-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1312</post-id>	</item>
		<item>
		<title>How to Create Stacked Histograms in R</title>
		<link>https://codingcampus.net/how-to-create-stacked-histograms-in-r/</link>
					<comments>https://codingcampus.net/how-to-create-stacked-histograms-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Wed, 15 Mar 2023 21:45:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1326</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/how-to-create-stacked-histograms-in-r/">How to Create Stacked Histograms in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A stacked histogram is a graphical representation of data where the individual values are placed on top of each other. This type of histogram is useful for seeing data distribution and spotting patterns. With stacked histograms, we plot more than one population on one graph. Using the<strong> hist()</strong> and <strong>ggplot()</strong> functions, you can create stacked histograms. This guide explains how to create stacked histograms in R.</p>
<h2>Using hist() Function to Create Stacked Histogram in R</h2>
<p>You should use the <strong>hist()</strong> function to create a stacked histogram when you have two different variables that are to be plotted on the same graph. The <strong>hist()</strong> is a built-in function in R that takes a vector as input and creates a histogram.</p>
<p>To create a stacked histogram of two variables, we need to call the <strong>hist()</strong> function twice, as there are two variables. When you call the <strong>hist()</strong> function a second time, make sure to include the “add” argument. This argument plots the first and the second histogram on a single plot.</p>
<h4>Syntax</h4>
<pre class="brush: php; title: ; notranslate">hist(x, col=NULL,add=TRUE)</pre>
<h4>Arguments</h4>
<ul>
<li>x = It is the vector of data that you want to create the histogram from.</li>
<li>col = It is the color of the bars.</li>
<li>add = TRUE to merge the histograms.</li>
</ul>
<h3>Example</h3>
<p>In this example, we will create a stacked histogram from two different variables.</p>
<pre class="brush: php; title: ; notranslate"> #creating 1st variable
x = c(50,40,45,20,25)
#creating 2nd variable
y = c(15,5,40,10,45)
#creating histogram of x
hist(x,col = &quot;green&quot;)
#creating histogram of y and stacking x
hist(y,col = &quot;red&quot;,add=TRUE)</pre>
<h4>Output</h4>
<figure id="attachment_29475" aria-describedby="caption-attachment-29475" style="width: 426px" class="wp-caption aligncenter"><img decoding="async" class="size-full wp-image-29475" src="http://codingcampus.net/wp-content/uploads/2022/10/hist-stacked-hist.png" alt="hist - stacked hist" width="436" height="347" /><figcaption id="caption-attachment-29475" class="wp-caption-text">Two variables stacked on a single plot.</figcaption></figure>
<p>In the above example, we created a histogram of variable x and stacked it on top of the histogram of variable y.</p>
<h2>Using ggplot() Function to Create Stacked Histogram in R</h2>
<p>While the <strong>hist()</strong> function is used when you have two different variables, <strong>ggplot()</strong> is used when you have one variable with different categories. <strong>ggplot()</strong> is not a built-in function in R.</p>
<h4>Syntax of ggplot()</h4>
<pre class="brush: php; title: ; notranslate">ggplot(data=, aes(x=, fill=)) + geom_histogram()</pre>
<h4>Argument</h4>
<ul>
<li>data = It is the data frame you want to use to create the plot.</li>
<li>aes() = It is used to specify the variable to use on axes.</li>
<li>x = The variable to map on the x-axis.</li>
<li>fill = It defines the categorical variable based on which you want to color the histogram.</li>
<li>geom_histogram() = It specifies that you want to create a histogram.</li>
</ul>
<h3>Installing ggplot2</h3>
<p><strong>ggplot()</strong> comes from the ggplot2 package. To install and load the package, run the following code:</p>
<pre class="brush: php; title: ; notranslate"># install ggplot2 package
install.packages('ggplot2')
# load ggplot2 package
library('ggplot2')</pre>
<h3>Example</h3>
<p>To demonstrate how to use the <strong>ggplot()</strong> to create a stacked histogram, we will use the <strong>PlantGrowth</strong> data set. This data set is included with the base R installation. The data set contains information on the experiments to compare yields. It has two columns: weight and group.</p>
<p>In this example, we will plot the histogram of mpg column using the ggplot() function.</p>
<pre class="brush: php; title: ; notranslate">ggplot(data=PlantGrowth, aes(x=weight,fill=group)) + geom_histogram()</pre>
<h4>Output</h4>
<figure id="attachment_29474" aria-describedby="caption-attachment-29474" style="width: 426px" class="wp-caption aligncenter"><img decoding="async" class="size-full wp-image-29474" src="http://codingcampus.net/wp-content/uploads/2022/10/ggplot-stacked-hist.png" alt="ggplot - stacked hist" width="436" height="347" /><figcaption id="caption-attachment-29474" class="wp-caption-text">Stacked histogram using the ggplot() function.</figcaption></figure>
<p>In the above example, we created a plot containing stacked histograms. We specified the weight column to be plotted and then colored the histogram based on the column group.</p>
<p>The post <a href="https://codingcampus.net/how-to-create-stacked-histograms-in-r/">How to Create Stacked Histograms in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/how-to-create-stacked-histograms-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1326</post-id>	</item>
		<item>
		<title>Create an Empty Vector in R</title>
		<link>https://codingcampus.net/create-an-empty-vector-in-r/</link>
					<comments>https://codingcampus.net/create-an-empty-vector-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Tue, 14 Mar 2023 13:09:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1321</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/create-an-empty-vector-in-r/">Create an Empty Vector in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A vector is a sequence of data elements of the same basic type. A vector whose <strong>length is zero</strong> is known as an <strong>empty vector</strong>. You can create an empty vector using the <strong>c()</strong>, <strong>vector()</strong>, <strong>rep()</strong>, and <strong>numeric()</strong> function. This guide shows different ways to create an empty vector in R.</p>
<h2>Method #1: Create an empty vector using the c() function</h2>
<p>The combine or <strong>c()</strong> function in R is used to create vectors by combining multiple values. To create an empty vector using the <strong>c()</strong> function, do not pass anything inside the parentheses.</p>
<h4>Syntax</h4>
<pre><code>vector name &lt;- c()</code></pre>
<p>Here, “vector name” can be any name you want to give to the empty vector.</p>
<h4>Example</h4>
<pre><code>x &lt;- c()
x</code></pre>
<h4>Output</h4>
<pre><code>NULL</code></pre>
<p>In the above example, the output is NULL which denotes that the vector is empty.</p>
<h2>Method #2: Create an empty vector using the vector() function</h2>
<p>To create an empty vector using the <strong>vector()</strong> function, do not pass anything inside the parentheses.</p>
<h4>Syntax</h4>
<pre><code>vector name &lt;- vector()</code></pre>
<p>Here, “vector name” can be any name you want to give to the empty vector.</p>
<h4>Example</h4>
<pre><code>v &lt;- vector()
v</code></pre>
<h4>Output</h4>
<pre><code>logical(0)</code></pre>
<p>In the above example, logical(0) denotes an empty vector.</p>
<h2>Method #3: Create an empty vector using a NULL object</h2>
<p>You can assign <strong>NULL</strong> to an existing or a new vector to create an empty vector.</p>
<h4>Example</h4>
<p>If you have a vector<strong> x = c(1, 2, 3)</strong>, you can convert it into an empty vector using the following code:</p>
<pre><code>x &lt;-c(1,2,3,5)
#converting to an empty vector
x &lt;- NULL
x</code></pre>
<h4>Output</h4>
<pre><code>NULL</code></pre>
<p>In the above example, we converted an existing non-empty vector to an empty vector by assigning the NULL object.</p>
<h2>Method #4: Create an empty vector using the rep() function</h2>
<p><strong>rep()</strong> function in R repeats the specified object a specified number of times. It is used to create a vector with a specified number of entries. To create an empty vector using the rep() function, do not pass anything inside the parentheses.</p>
<h4>Syntax</h4>
<pre><code>vector name &lt;- rep()</code></pre>
<p>Here, “vector name” can be any name you want to give to the empty vector.</p>
<h4>Example</h4>
<pre><code>y &lt;- rep()
y</code></pre>
<h4>Output</h4>
<pre><code>NULL</code></pre>
<h2>Method #5: Create an empty vector using the numeric() function</h2>
<p>The <strong>numeric()</strong> function creates a numeric vector of a defined length. To create an empty vector using the <strong>numeric()</strong> function, do not pass anything inside the parentheses.</p>
<h4>Syntax</h4>
<pre><code>vector name &lt;- numeric()</code></pre>
<p>Here, “vector name” can be any name you want to give to the empty vector.</p>
<h4>Example</h4>
<pre><code>a &lt;- numeric()
a</code></pre>
<h4>Output</h4>
<pre><code>numeric(0)</code></pre>
<p>The post <a href="https://codingcampus.net/create-an-empty-vector-in-r/">Create an Empty Vector in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/create-an-empty-vector-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1321</post-id>	</item>
		<item>
		<title>cos() Function in R</title>
		<link>https://codingcampus.net/cos-function-in-r/</link>
					<comments>https://codingcampus.net/cos-function-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Mon, 13 Mar 2023 12:25:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1322</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/cos-function-in-r/">cos() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>cos()</strong> function is a built-in function in R that calculates the cosine of a single number or a vector of numbers. This guide shows how to use the <strong>cos()</strong> function in R.</p>
<h2>Syntax</h2>
<pre><code>cos(x)</code></pre>
<h2>Argument</h2>
<ul>
<li><strong>x</strong> = It is the numeric vector you want to calculate the cosine of.</li>
</ul>
<p>Here are some examples of the cos() function in R:</p>
<h2>Example 1: Calculate the cosine of a number</h2>
<pre><code>#calculating the cosine value of a number
cos(0)
#calculating the cosine value of a number stored in a variable
x = 5
cos(x)</code></pre>
<h4>Output</h4>
<pre><code>[1] 1
[1] 0.2836622</code></pre>
<h2>Example 2: Calculate the cosine of a vector</h2>
<pre><code>#calculating the cosine value of a vector
x = (c(0,1, 2))
cos(x)</code></pre>
<h4>Output</h4>
<pre><code>[1] 1.0000000 0.5403023 -0.4161468</code></pre>
<p>The post <a href="https://codingcampus.net/cos-function-in-r/">cos() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/cos-function-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1322</post-id>	</item>
		<item>
		<title>How to Calculate Square in R</title>
		<link>https://codingcampus.net/how-to-calculate-square-in-r/</link>
					<comments>https://codingcampus.net/how-to-calculate-square-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Sat, 11 Mar 2023 21:49:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1320</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/how-to-calculate-square-in-r/">How to Calculate Square in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>You can square a number, vector, matrix, and data frame in R. This guide will show you how to calculate the square of a single value, vector, matrix, and data frame in R.</p>
<h2>Calculate the Square of a Single Value in R</h2>
<p>There are 3 ways to calculate the square of a vector in R.</p>
<h3>Method #1: Using the ^ operator</h3>
<p>To calculate the square of a single value, use the <strong>^</strong> operator.</p>
<p>For example, to calculate the square of <strong>5</strong>, you would use the following code:</p>
<pre><code>5^2</code></pre>
<h4>Output</h4>
<pre><code>[1] 25</code></pre>
<h3>Method #2:  Using the * operator</h3>
<p>You can square a number using the multiplication operator, also known as an asterisk symbol (‘*’). To use it, take the number and multiply it by itself. For example, to take the square of 5, you can use the following formula:</p>
<pre><code>5*5</code></pre>
<h4>Output</h4>
<pre><code>[1] 25</code></pre>
<h3>Method #3: Using the ** operator</h3>
<p>You can also square a number using the double asterisk ** operator. So we type the number to square, then **, and end with 2. For example, to get the square of 5, use the following formula:</p>
<pre><code>5**2</code></pre>
<h4>Output</h4>
<pre><code>[1] 25</code></pre>
<h2>Calculate the Square of a Vector in R</h2>
<p>There are three ways to calculate the square of a vector in R.</p>
<h3>Method #1: Using the ^ operator</h3>
<p>To calculate the square of a vector, use the <strong>^</strong> operator.</p>
<p>For example, if you have a vector <strong>x = c(1,2,3)</strong>, you can calculate the square of each element in the vector using the following code:</p>
<pre><code>x = c(1,2,3)
x^2</code></pre>
<h4>Output</h4>
<pre><code>[1] 1 4 9</code></pre>
<h3>Method #2: Using the * operator</h3>
<p>Alternatively, you can use the <strong>*</strong> operator to square a vector.</p>
<h4>Syntax</h4>
<pre><code>vector * vector</code></pre>
<p>For example, if you have a vector <strong>x = c(1,2,3) </strong>you would use this code:</p>
<pre><code>x = c(1,2,3)
x*x</code></pre>
<h4>Output</h4>
<pre><code>[1] 1 4 9</code></pre>
<h3>Method #3: Using the ** operator</h3>
<p>Use the<strong> **</strong> operator to square a vector.</p>
<h4>Syntax</h4>
<pre><code>vector ** 2</code></pre>
<p>For example, if you have a vector <strong>x = c(1,2,3)</strong> you would use this code:</p>
<pre><code>x = c(1,2,3)
x**2</code></pre>
<h4>Output</h4>
<pre><code>[1] 1 4 9</code></pre>
<h2>Calculate the Square of a Matrix in R</h2>
<p>To calculate the square of a matrix in R, use the <strong>^</strong> operator with the matrix.</p>
<p>For example, if you have a matrix <strong>A = matrix(1:9, 3, 3)</strong> you can calculate the square of each element in the matrix using the following code:</p>
<pre><code>A = matrix(1:9, 3, 3)
A^2</code></pre>
<h4>Output</h4>
<pre><code>[1] 1 4 9 16 25 36 49 64 81</code></pre>
<h2>Calculate the Square of a Data Frame in R</h2>
<p>To calculate the square of a data frame in R, use the <strong>^</strong> operator with the data frame.</p>
<p>For example, if you have a data frame <strong>df</strong>, you can calculate the square of each element in the data frame using the following code:</p>
<pre><code>df = data.frame(x = c(1,2,3), y = c(4,5,6))
df^2</code></pre>
<h4>Output</h4>
<pre><code>  x y
1 1 16
2 4 25
3 9 36</code></pre>
<p>The post <a href="https://codingcampus.net/how-to-calculate-square-in-r/">How to Calculate Square in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/how-to-calculate-square-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1320</post-id>	</item>
		<item>
		<title>dim() in R</title>
		<link>https://codingcampus.net/dim-in-r/</link>
					<comments>https://codingcampus.net/dim-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Fri, 10 Mar 2023 04:18:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1315</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/dim-in-r/">dim() in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>dim()</strong> function is used to get the dimension of a matrix, array, data frame, or vector. It is a part of the base R package. This guide explains how to use the dim() in R.</p>
<h2>Syntax</h2>
<pre><code>dim(x)</code></pre>
<h2>Arguments</h2>
<ul>
<li><strong>x</strong> = a vector, matrix, array, or data frame</li>
</ul>
<h2>Using dim() to get the dimension of a Vector</h2>
<p>A vector is a one-dimensional data structure. To get the dimension of a vector, you need to specify the vector as an argument to the dim() function. It will always return <strong>NULL</strong>.</p>
<h4>Example</h4>
<pre><code>#creating a vector
x &lt;- 1:3
#printing vector
x
#getting the dimension of a vector
dim(x)</code></pre>
<h4>Output</h4>
<pre><code>[1] 1 2 3
NULL</code></pre>
<h2>Using dim() to get the dimension of a Matrix</h2>
<p>A matrix is a two-dimensional array. To get the dimension of a matrix, you need to specify the matrix as an argument to the dim() function. It will return a vector of <strong>two integers</strong> that indicate <strong>the number of rows and columns in the matrix</strong>.</p>
<h4>Example</h4>
<pre><code>#creating a matrix
y &lt;- matrix(1:9, nrow=3, ncol=3)
#printing matrix
y
#getting the dimension of a matrix
dim(y)</code></pre>
<h4>Output</h4>
<pre><code>[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

[1] 3 3</code></pre>
<h2>Using dim() to get the dimension of an Array</h2>
<p>An array is a three-dimensional data structure. To get the dimension of an array, you need to specify the array as an argument to the dim() function. It will return a vector of <strong>three integers</strong> that indicate the <strong>size of the array</strong>.</p>
<h4>Example</h4>
<pre><code>#creating an array
a &lt;- array(1:8, c(2, 2, 2))
#printing array
a
#getting the dimension of the array
dim(a)</code></pre>
<h4>Output</h4>
<pre><code>, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4

, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8

[1] 2 2 2</code></pre>
<h2>Using dim() to get the dimension of a Data Frame</h2>
<p>A data frame is a two-dimensional data structure. To get the dimension of a data frame, you need to specify the data frame as an argument to the dim() function. It will return a vector of <strong>two integers</strong> that indicate <strong>the number of rows and columns in the data frame</strong>.</p>
<h4>Example</h4>
<pre><code>#creating a data frame
df &lt;- data.frame(x=1:3, y=4:6)
#printing data frame
df
#getting the dimension of the data frame
dim(df)</code></pre>
<h4>Output</h4>
<pre><code>x y
1 1 4
2 2 5
3 3 6

[1] 3 2</code></pre>
<p>Here, 3 indicates the number of rows in the data frame, and 2 indicates the number of columns.</p>
<p>The post <a href="https://codingcampus.net/dim-in-r/">dim() in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/dim-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1315</post-id>	</item>
		<item>
		<title>sample() Function in R</title>
		<link>https://codingcampus.net/sample-function-in-r/</link>
					<comments>https://codingcampus.net/sample-function-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Thu, 09 Mar 2023 10:53:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1310</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/sample-function-in-r/">sample() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The <strong>sample()</strong> function allows you to select a <strong>random set of data points from a vector</strong>. This enables you to perform statistical analysis on a <strong>sample</strong> of a data set without having to input all the data manually. This guide explains how to use the <strong>sample()</strong> function in R.</p>
<h4>Syntax</h4>
<pre><code>sample(x, size, replace, probability)</code></pre>
<h4>Arguments</h4>
<ul>
<li><strong>x</strong> = a vector of data points from which you want to select a random sample.</li>
<li><strong>size</strong> = the number of data points you want to select.</li>
<li><strong>replace</strong> = can be <strong>TRUE</strong> or <strong>FALSE</strong>. If TRUE, the values can be repeated in the sample. By default, it is set to FALSE.</li>
<li><strong>prob</strong> = a vector of probability weights.</li>
</ul>
<p>Now that we&#8217;ve gone over the basics of the function, let&#8217;s look at some examples of how to use it.</p>
<h3>Example 1: Creating a sample without replacement</h3>
<p>We can use the sample() function to select a random sample of data points <strong>without replacement</strong>. This means that once a data point is selected, it will not be selected again. Let&#8217;s say we have a vector x that contains 100 data points. To select a random sample of 10 data points from this vector, we use the following code:</p>
<pre><code>#creating a vector of 100 values
x = c(1:100)
#generating a sample of 10 values
sample(x, size = 10)</code></pre>
<h4>Output</h4>
<pre><code>[1] 10 62 56 97 64 9 26 87 52 41</code></pre>
<p>In this example, the function returned a vector containing ten randomly selected data points from x, where no data point was selected more than once. Also, note that values are not repeated because, by default, the replace argument is set to be <strong>FALSE</strong>.</p>
<h3>Example 2: Creating a sample with replacement</h3>
<p>Let&#8217;s take the same example used in Example 1. We can use the sample() function to select a random sample of 10 data points from this vector <strong>with replacement of the values</strong> using the following code:</p>
<pre><code>#creating a vector of 100 values
x = c(1:100)
#generating a sample of 10 values
sample(x, size = 10, replace=TRUE)</code></pre>
<h4>Output</h4>
<pre><code>[1] 28 34 32 68 67 91 88 2 14 88</code></pre>
<p>In this example, the function returned a vector containing ten randomly selected data points from x. Also, note that values may be repeated because we have set the replace argument as <strong>TRUE</strong>. In this example, 88 got repeated twice.</p>
<h3>Example 3: Creating a sample with uneven probabilities</h3>
<p>In the following example, we will define each element&#8217;s probabilities in the data set. Kindly note that the probability value should be between 0 to 1.</p>
<pre><code>#creating a vector of 5 values
x = c(25,50,75,100,125)
#generating a sample of 3 values
sample(x, size = 3, replace=TRUE, prob = c(0.7,0.3,0.8,0.6,0.7))</code></pre>
<h4>Output</h4>
<pre><code>[1] 75 25 25</code></pre>
<p>In the above example, we have defined the probability weights of each element using the prob argument. Notice that the value 75 has the highest probability of 0.8 and thus was represented in the sample output.</p>
<h3>Example 4: Reordering the elements inside a vector</h3>
<p>If you use the sample() function without defining the size, the function will return the same data set but with reordering it in ascending order.</p>
<pre><code>#creating a vector of 5 values
x = c(25,50,75,100,125)
#reordering the data
sample(x)</code></pre>
<h4>Output</h4>
<pre><code>[1] 25 50 100 125 75</code></pre>
<p>The post <a href="https://codingcampus.net/sample-function-in-r/">sample() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/sample-function-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1310</post-id>	</item>
		<item>
		<title>dnorm() Function in R</title>
		<link>https://codingcampus.net/dnorm-function-in-r/</link>
					<comments>https://codingcampus.net/dnorm-function-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Wed, 08 Mar 2023 04:12:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1303</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/dnorm-function-in-r/">dnorm() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>dnorm()</strong> function in R allows you to calculate the probability density function for a normal distribution for a given <a href="https://codingcampus.net/mean-median-and-mode-in-r/">mean</a> and <a href="https://codingcampus.net/standard-deviation-in-r/">standard deviation</a>. This guide shows you how to use the <strong>dnorm()</strong> function in R.</p>
<h4>dnorm() syntax</h4>
<pre><code>dnorm(x, mean = 0, sd = 1)</code></pre>
<h4>Arguments</h4>
<ul>
<li><strong>x</strong> = numeric value to test for probability</li>
<li><strong>mean</strong> = mean of the distribution</li>
<li><strong>sd</strong> = standard deviation of the distribution</li>
</ul>
<p>The mean and sd arguments are optional. If you do not pass a value to them, they will take the default values as mean = 0 and sd = 1.</p>
<h3>Example 1: dnorm() function with a single value</h3>
<p>Suppose we want to calculate the probability density function of 5 with a mean of 10 and a standard deviation of 3.</p>
<pre><code>x &lt;- dnorm(5, mean=10, sd=3)</code></pre>
<h4>Output</h4>
<pre><code>[1] 0.03315905</code></pre>
<h3>Example 2: dnorm() Function with a vector of values</h3>
<p>Say we want to calculate the probability density function of each element inside a vector with a mean of 10 and a standard deviation of 3.</p>
<pre><code>#creating a vector
my_data &lt;-c(1,2,3,4,5)
#calculating probability density function of the vector
x &lt;- dnorm(my_data, mean=10, sd=3)
x</code></pre>
<h4>Output</h4>
<pre><code>[1] 0.001477283 0.003798662 0.008740630 0.017996989 0.033159046</code></pre>
<p>The post <a href="https://codingcampus.net/dnorm-function-in-r/">dnorm() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/dnorm-function-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1303</post-id>	</item>
		<item>
		<title>tryCatch() Function in R</title>
		<link>https://codingcampus.net/trycatch-function-in-r/</link>
					<comments>https://codingcampus.net/trycatch-function-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Tue, 07 Mar 2023 01:06:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1309</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/trycatch-function-in-r/">tryCatch() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><strong>tryCatch()</strong> function in R is used for handling errors. The function returns the expression value or generates a custom message when an error or warning occurs. This guide explains how to use the tryCatch() function in R.</p>
<h4>Syntax</h4>
<pre><code>tryCatch(expr, ..., finally)
tryCatch({
expr
}, error = function(e) {
code for handling error
}, warning = function(w) {
code for handling warning
}, finally = {
clean-up code
}</code></pre>
<h4>Arguments</h4>
<ul>
<li><strong>expr</strong> = expression to be evaluated.</li>
<li><strong>error</strong> = error handling function. It is called if an error occurs when evaluating the expression.</li>
<li><strong>warning</strong> = warning handling function. it is called if a warning occurs when evaluating the expression.</li>
<li><strong>finally</strong> = function to run after the execution of the tryCatch function. This argument is optional.</li>
</ul>
<h3>Example 1</h3>
<p>In this example, we evaluate an expression that causes an error. Then, we will use the <strong>tryCatch()</strong> function to handle the error.</p>
<pre><code># creating an error by dividing a character by an integer
x/12</code></pre>
<h4>Output</h4>
<pre><code>Error in x/12: non-numeric argument to binary operator</code></pre>
<pre><code>#using the tryCatch() function to handle the error
tryCatch({
expr = x/12 #expression
}, error = function(e) {
print("There is an error!") #printing a message when prompted with an error
}, finally ={
print("code executed") #printing a message after code execution
})</code></pre>
<h4>Output</h4>
<pre><code>[1] "There is an error!"
[1] "code executed"</code></pre>
<h3>Example 2</h3>
<p>In this example, we evaluate an expression that will cause a warning. Then, we will use the <strong>tryCatch()</strong> function to handle the warning.</p>
<pre><code># taking a square root of a negative number generates a warning
sqrt(-1)</code></pre>
<h4>Output</h4>
<pre><code>[1] NaN
Warning message:
In sqrt(-1) : NaNs produced</code></pre>
<pre><code>#using the tryCatch() function to handle the warning
tryCatch({
expr = sqrt(-1) #expression
}, warning = function(w) {
print("There is a warning!") #printing a message when prompted with a warning
}, finally ={
print("code executed") #printing a message after code execution
})</code></pre>
<h4>Output</h4>
<pre><code>[1] "There is a warning!"
[1] "code executed"</code></pre>
<p>The post <a href="https://codingcampus.net/trycatch-function-in-r/">tryCatch() Function in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/trycatch-function-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1309</post-id>	</item>
		<item>
		<title>Natural Log in R</title>
		<link>https://codingcampus.net/natural-log-in-r/</link>
					<comments>https://codingcampus.net/natural-log-in-r/#respond</comments>
		
		<dc:creator><![CDATA[Kenneth Stenberg]]></dc:creator>
		<pubDate>Mon, 06 Mar 2023 23:04:00 +0000</pubDate>
				<category><![CDATA[R]]></category>
		<guid isPermaLink="false">https://codingcampus.net/?p=1292</guid>

					<description><![CDATA[<p>The post <a href="https://codingcampus.net/natural-log-in-r/">Natural Log in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The natural logarithm is a logarithmic function that is the <strong>inverse of the exponential function</strong>. It is the logarithm of <strong>a number to the base e</strong>. The natural log is often used to take the antilog of a number. It can model simple growth patterns such as population growth or compound interest.</p>
<p>There are two ways to find the natural log in R. This guide shows you how to use both methods.</p>
<h2>Method #1: Calculating Natural Log using log() function</h2>
<p><strong>log()</strong> function comes preloaded in R. It allows the users to input the base of the log. But, if you don&#8217;t mention the base, the log() function will, by default, calculate the natural logarithm.</p>
<h4>Syntax of log() Function</h4>
<pre><code>log(x, base)</code></pre>
<h4>Arguments</h4>
<ul>
<li><strong>x</strong> = a numeric or complex vector</li>
<li><strong>base</strong> = base of the log</li>
</ul>
<h3>Example 1: Calculating Natural Log using the log() function</h3>
<pre><code>#calculating natural log of 5
log(5)</code></pre>
<h4>Output</h4>
<pre><code>[1] 1.609438</code></pre>
<h3>Example 2: Calculating Natural Log of a vector using the log() function</h3>
<pre><code>#creating a vector
x &lt;- c(1,2,3,4,5)
#calculating natural log of 5
log(x)</code></pre>
<h4>Output</h4>
<pre><code>[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379</code></pre>
<p>In this example, the log() function calculates each element&#8217;s natural log inside the vector.</p>
<h2>Method 2: Calculating Natural Log using ln() function</h2>
<h3>Installing SciViews</h3>
<p><strong>ln()</strong> comes from the <strong>SciViews</strong> package. To install and load the package, run the following code:</p>
<pre><code>install.packages('SciViews')
library('SciViews')</code></pre>
<h4>Syntax of ln() Function</h4>
<pre><code>ln(x)</code></pre>
<h4>Arguments</h4>
<ul>
<li><strong>x</strong> = a numeric or complex vector.</li>
</ul>
<h3>Example 1: Calculating Natural Log using the ln() Function</h3>
<pre><code>#loading the SciViews package
library('SciViews')
#calculating natural log of 5
ln(5)</code></pre>
<h4>Output</h4>
<pre><code>[1] 1.609438</code></pre>
<h3>Example 2: Calculating Natural Log of a Vector using the log() Function</h3>
<pre><code>#loading the SciViews package
library('SciViews')
#creating a vector
x &lt;- c(1,2,3,4,5)
#calculating natural log of 5
ln(x)</code></pre>
<h4>Output</h4>
<pre><code>[1] 0.0000000 0.6931472 1.0986123 1.3862944 1.6094379</code></pre>
<p>In this example, the ln() function calculates each element&#8217;s natural log inside the vector.</p>
<p>The post <a href="https://codingcampus.net/natural-log-in-r/">Natural Log in R</a> appeared first on <a href="https://codingcampus.net">Coding Campus</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codingcampus.net/natural-log-in-r/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1292</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Page Caching using Disk: Enhanced 

Served from: codingcampus.net @ 2026-04-24 00:45:57 by W3 Total Cache
-->