tag:blogger.com,1999:blog-23526755400997167092024-03-05T02:35:23.143-08:00Statistic R Programming by JoeLearning R Programming Language as Beginner | Herman NzWebAdminhttp://www.blogger.com/profile/15897264215997541341noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-2352675540099716709.post-527878755809124252015-04-06T19:42:00.003-07:002015-04-06T22:39:43.395-07:00Learn Vectors 3<br />
<span style="color: #990000;"><u><b>We will learn:</b></u></span><br />
<br />
1. <a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-3-r-programming.html#vector-math" target="_parent" title="Vector Math in R | Pemrograman R">Vector Math (Using Scalar Value in <u><b>c</b></u> Vector)</a><br />
<ul>
<li><a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-3-r-programming.html#trigonometric-function" target="_parent" title="Trigonometric Function in c Vectors | Pemrograman R">Trigonometric Function in c Vectors</a></li>
</ul>
2. <a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-3-r-programming.html#scatter-plots" target="_parent" title="Scatter Plots in R | Pemrograman R">Scatter Plots</a><br />
3. <a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-3-r-programming.html#na-values-vectors" target="_parent" title="N/A Values in R | Pemrograman R">N/A Values</a><br />
<br />
<span style="color: #990000;"><u><b>Details:</b></u></span><br />
<br />
<h3>
<span id="vector-math"><b>E. Vector Math</b></span></h3>
Now we will try to add <i>scalar</i> value in <u><b>c</b></u> vector. <b>What is Scalar?</b> <i><b>Scalar</b> is <b>a single</b> and <b>real value</b></i>. In <a href="https://en.wikipedia.org/wiki/Scalar_%28mathematics%29" rel="nofollow" target="_blank">Arithmetic Operation</a>, you will find this math operation;<br />
<br />
<br />
<div style="text-align: left;">
x = <b>1</b> + <b>1</b></div>
<div style="text-align: left;">
y = <b>4</b> / <b>2</b></div>
<div style="text-align: left;">
... etc.</div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-3-r-programming.html#vector-math" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img alt="Vector Math in R Programming | Pemrograman R" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjoYoM9YY7Mr4fIDUc5k313GPE0l7m0YN5FmQ_OFAIiSXTEO6Vex9ZlvK7OENSJlZKU9Y78poHL8m_S2PgaetiBzePMz3Ss1OQX1u7smDMnKQ8uDmtvXuGj6khjmpypErb23oZIUMLQ1zpf/s1600/scalar-vector-r-programming.png" title="Vector Math in R Programming | Pemrograman R" /></a></div>
<br />
<div style="text-align: left;">
Other <i>samples</i>;</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
24 m -> <i><b>Scalar</b></i> Value</div>
<div style="text-align: left;">
<a href="https://en.wikipedia.org/wiki/Vector_space" rel="nofollow" target="_blank">24 <b>m<sup>2</sup></b></a> -> <i><b>Vector</b></i> Value<br />
<br />
<br />
</div>
<div style="text-align: left;">
<br /></div>
<div style="text-align: left;">
<span style="font-family: Arial,Helvetica,sans-serif; font-size: xx-small;">Note: For my math teacher, Drs.Sutikno, in SMA Negeri 1 Bukit Kemuning, North Lampung, Lampung Province (<u>studied in 1992</u>) : "This is the first time for me to understand what Scalar & Vector is. ..... - I understand this when I understand English Language ... (crying)."</span><br />
<br />
<br />
<br />
<br /></div>
<span style="font-size: xx-small;"><span style="font-family: Arial,Helvetica,sans-serif;">i</span></span>n <i><b>R</b></i>, like our previous lesson, we have:<br />
<br />
> a <- c(3, 9, 7)<br />
<br />
Then, we add "2" to each values in <u><b>c</b></u> vectors, as in:<br />
<br />
> a + 2<br />
Result: [1] 5 11 9<br />
<br />
This is simple example where a is 3, a is 9 and a is 7. Each of the a(s) is counted up to 2 (Scalar).<br />
<br />
Further, you can do that with other arithmetic operation, as follows:<br />
<br />
> a * 3<br />
Result: [1] 9 27 21<br />
<br />
> a / 3<br />
Result: [1] 1.000000 3.000000 2.333333<br />
<br />
Now, if we have other <u><b>c</b></u> vector value after the previous one > a <- c(3, 9, 7), as in:<br />
<br />
b <- c(2,1,4)<br />
<br />
and, add them (a + b) up, that will be as follows:<br />
<br />
> b <- c(2,1,4)<br />
> a + b<br />
Result: [1] 5 10 11<br />
<br />
The result is coming from:<br />
<br />
3 (in <b><i>a</i></b>) + 2 (in <b><i>b</i></b>)<br />
9 (in <i><b>a</b></i>) + 1 (in <i><b>b</b></i>)<br />
7 (in <b>a</b>) + 4 (in <i><b>b</b></i>)<br />
<br />
Try other operation by substract b - a or vice verse!<br />
.......<br />
<i>Take notice</i> that, when you try to compare <u><b>c</b></u> vector in previous <i><b>a</b></i> to <i>other</i> <i>new <b>a</b></i> vector values:<br />
<br />
<i>For example:</i><br />
<br />
previous <i><b>a</b></i> vector: > a <- c(3, 9, 7)<br />
new <i><b>a</b></i> vector: > a <- c(1, 9, 7)<br />
<br />
> a == c(1, 9, 7)<br />
Result: [1] FALSE TRUE TRUE<br />
<br />
then <i><b>R</b></i> does not sum up both vectors, but index <i><b>each values</b></i> of both vectors.<br />
Now we try to use > (more than) to compare each values in a and b vectors, as follows:<br />
<br />
> a > c (1,9,7)<br />
Result: [1] TRUE FALSE FALSE<br />
<br />
<i><span id="trigonometric-function"><b>Vectors in Trigonometric Function</b></span></i><br />
<br />
When you use Trigonometric Function, such as, <i><b>Sin</b></i>, <i><b>Cos</b></i> or <i><b>Tan</b></i> then <i><b>R</b></i> will figure each values of <i><b>a</b></i> vector <i>against</i> Sin, Cos or Tan, as follows:<br />
<br />
> sin(a)<br />
Result: [1] 0.1411200 0.4121185 0.6569866<br />
<br />
> cos(a)<br />
Result: [1] -0.9899925 -0.9111303 0.7539023<br />
<br />
> tan(a)<br />
Result: [1] -0.1425465 -0.4523157 0.8714480 <br />
<br />
Now, using sqrt function in a vectors:<br />
<br />
> sqrt(a)<br />
Result: [1] 1.732051 3.000000 2.645751<br />
<br />
<h3>
<span id="scatter-plots"><b>F. Scatter Plots</b></span></h3>
In <i><b>R</b></i>, <b>Plot</b> function handles graphic as written below:<br />
<br />
> plot(x,y)<br />
<br />
<table summary="R argblock"><tbody>
<tr valign="top"><td><b><code>x</code></b></td>
<td>the coordinates of points in the plot. Alternatively, a
single plotting structure, function or <i>any <span style="color: #666666; font-family: Courier New,Courier;"><b>R</b></span> object with <b>a</b>
<code>plot</code> method</i> can be provided.</td></tr>
<tr valign="top"><td><b><code>y</code></b></td>
<td>the <i><b>y</b></i> coordinates of points in the plot, <i>optional</i>
if <code>x</code> is an appropriate structure.<br />
<a href="http://127.0.0.1:27091/library/graphics/html/plot.html" rel="nofollow" target="_blank"><span style="font-size: xx-small;"><span style="color: #666666;"><span style="font-family: Arial,Helvetica,sans-serif;">(R-Documentation)</span></span></span></a></td></tr>
</tbody></table>
<br />
By using <b>plot()</b> we can <i><b>draw a graph</b></i> by relating <i><b>x</b></i> to <i><b>y</b></i> coordinates. <i><b>To draw it</b></i>, we then need <u><b>data</b></u>. In this case, the data must contains data <i><b>x</b></i> coordinate and <i><b>y</b></i> coordinates.<br />
<br />
<i>For example:</i><br />
<br />
> x <- seq(1, 20, 0.1)<br />
> y <- sin(x)<br />
<span style="font-size: xx-small;"><span style="color: #666666;"><span style="font-family: Arial,Helvetica,sans-serif;">(taken from: <a href="http://tryr.codeschool.com/levels/2/challenges/35" target="_blank">http://tryr.codeschool.com/levels/2/challenges/35</a>)</span></span></span><br />
<br />
Then we do the plot on both data, as follows:<br />
<br />
> plot(x,y)<br />
Result: <span style="color: #666666;"><i># ........ see this following graph, awesome!</i></span><br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-3-r-programming.html#scatter-plots" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Scatter Plots in R Programming | Pemrograman R" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOOlnA4-w4u7CkWLE3Xl2PuzyjrQe6y5uutt2YQPsE22iZ2fok6EeSTvPRQ48huDslGYjVls3IP8unT-XAysc_AbZI69N7SuEIDZ9CULkbRrdtpsD_sH2F-f2hMgdxDlLCHvZzmM-0eIMT/s1600/pemrograman-r-plot-vector-learn-vector-3.png" height="310" title="Scatter Plots in R Programming | Pemrograman R" width="400" /></a></div>
<br />
<br />
Now, let's take another example by using <i>negative values</i> in one of the vectors values and assign the first vector into absolute function in second vector, as follows:<br />
<br />
<u><b>Data</b></u><br />
<br />
1. First vector values (using negative values)<br />
<b>> mylesson <- -2:7</b><br />
2. Second vector values (using negative values)<br />
<b> > mygrade <- abs(mylesson)</b><br />
<br />
<b>> plot(mylesson,mygrade) </b><br />
Result:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-3-r-programming.html#scatter-plots" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Scatter Plots in R Programming | Pemrograman R" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTzipsqbNKluWqHCvM5jROXuDZRJanJWJVRkNZOXKuU1PsoyX43iln4EoXD_rthmxVGR3i7Gdh21R-S_iWfORMjV1mGeuYpk0ZsBeA7zrMPnfmxTvFQcZA-8UIDGWMFXxm3yPKTgADACfM/s1600/vector-plot-pemrograman-r.png" height="300" title="Scatter Plots in R Programming | Pemrograman R" width="400" /></a></div>
<br />
<br />
<h3>
<span id="na-values-vectors"><b>G. N/A Values</b></span></h3>
In a sample of a vector, one of the values is not available. This sometimes occurs in a column in Database where the data in that column is not filled by user for optional form-sheet. In database, we usually set it active as NULL data.<br />
In R, this NULL data means not exists or not available, then R assigns it as Non Active or N/A Values.<br />
<br />
<i>For example:</i><br />
<br />
> a <- c(1, 3, NA, 7, 9)<br />
<br />
,where within <u><b>c</b></u> vector, we assign that the third value is <i>NA</i> status. When we need the <i>result of <b>a</b> vector</i>, <i><b>R</b></i> will give you N/A or NA, as in:<br />
<br />
> sum(a)<br />
Result: [1] NA<br />
<br />
In this case, we use <i><b>sum</b></i> function to test it. <i>sum function</i> considers it as NA since the calculation is not complete yet (NA means: can not be calculated). see the <a href="http://127.0.0.1:25716/library/base/html/sum.html" rel="nofollow" target="_blank">sum documentation in R</a> by typing <a href="http://127.0.0.1:25716/library/base/html/sum.html" rel="nofollow" target="_blank"><b>help(sum)</b></a>.<br />
<blockquote class="tr_bq">
"As you see in the documentation, sum can take an optional named argument, na.rm. It's set to FALSE by default, but if you set it to TRUE, all NA arguments will be removed from the vector before the calculation is performed." <span style="font-size: x-small;">(<a href="http://tryr.codeschool.com/levels/2/challenges/38" target="_blank">http://tryr.codeschool.com/levels/2/challenges/38</a>)</span></blockquote>
However, R can ignore the NA values by calling <a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-3-r-programming.html#na-values-vectors"><b>na.rm</b></a> and set it to <i><b>TRUE</b></i>, as in:<br />
<br />
> sum(a,na.rm=TRUE)<br />
Result: [1] 20 <span style="color: #666666;">#<i>where 20 is coming from: 1+3+7+9</i></span>.<br />
<br />
<br />
<br />
<br />
<span style="color: #666666;"><i>See you on the next lesson!</i></span><br />
<span style="color: #666666;"><i>My best regards</i></span><br />
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2352675540099716709.post-47286532948350397912015-04-03T12:19:00.000-07:002015-04-06T22:54:10.353-07:00Learn Vectors 2<br />
<span style="color: #990000;"><u><b>We will learn:</b></u></span><br />
<br />
1. <a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-2.html#vector-names" target="_parent" title="Vector Names in R | Pemrograman R">Vector Names</a><br />
2. <a href="http://pemrograman-r.blogspot.com/2015/04/learn-vectors-2.html#plotting-one-vectors" target="_parent" title="Plotting One Vectors in R | Pemrograman R">Plotting One Vectors</a><br />
<br />
<span style="color: #990000;"><u><b>Details:</b></u></span><br />
<br />
This is part two of learning vectors in R programming language. If you haven't learned about the first part, you can <b><a href="http://pemrograman-r.blogspot.com/2015/03/learn-vectors-r-programming.html" target="_blank">start to learn vectors part one</a></b>.<br />
<br />
<h3>
<span id="vector-names"><b>D. Vector Names</b></span></h3>
<br />
For instance, I have three values (e.g. red, yellow and black) which is stored in <b>wordlist</b> <u><b>c</b></u> vector. That will be written as follows;<br />
<br />
wordlist <- c("red","yellow","black") <span style="color: #999999;"># wordlist stored "red", "yellow" and "black" value.</span><br />
<br />
As explained in <a href="http://pemrograman-r.blogspot.com/2015/03/learn-vectors-r-programming.html#vector-access" target="_blank"><i><b>Vector Access</b></i></a>, when you need to pick one of the values, you can do like this:<br />
<br />
rank[1:3]<br />
Result: [1] "red","yellow","black"<br />
<br />
Now, this time we will learn that "we can assign <i>names</i> to a vector's elements by passing a second vector filled with names to the <i><b>"names"</b></i> <b>assignment function</b>".(<span style="color: #666666;"><span style="font-size: xx-small;"><i><span style="font-size: x-small;">taken from</span></i>. <a href="http://tryr.codeschool.com/levels/2/challenges/18" rel="nofollow" target="_blank">http://tryr.codeschool.com/levels/2/challenges/18</a></span></span>) , as in:<br />
<br />
names(wordlist) <- c("red","yellow","black")<br />
<br />
<span style="color: red;"><b>(Note</b></span>: to see more about names function in R, type:<br />
help(names)<br />
<span style="color: red;"><b>)</b></span><br />
<br />
When we want to access or pick one of the values, we can type like this:<br />
<br />
wordlist["yellow"]<br />
Result: yellow<br />
2<br />
<br />
From the example above, that means, that we can access the value directly beside our common way, as in:<br />
<br />
wordlist[2]<br />
<br />
Result: yellow<br />
2<br />
<br />
From here, we also can change the position of "yellow" (that is: 2) to other position, for example, 7.<br />
<br />
wordlist["yellow] <- 7<br />
<br />
Now, see if it already changes:<br />
<br />
wordlist["yellow"]<br />
Result: "7"<br />
<br />
or, with other way;<br />
<br />
wordlist[7]<br />
Result: "yellow"<br />
<br />
<h3>
<span id="plotting-one-vectors"><b>E. Plotting One Vector</b></span></h3>
<br />
This section, we will learn about barplot. What is it? <b>barplot</b> is <i>R function</i> which is used to <i><b>create vertical or horizontal bars (e.g. chart) with vector values. </b></i>For further and details about that, just ask R to answer by typing: -<br />
<br />
help(barplot)<br />
<br />
<div style="text-align: center;">
<span style="color: red;"><b>Hint</b></span>: Talking about Vectors will force you to remember 2 things: <i><b>Variables</b></i> and <i><b>Values</b></i>.</div>
<br />
For example, we have ( again and again ... :p ) three values in c vector, they are: 3, 5 and 1, as follows:<br />
1. Variable : stuDentS (<span style="color: #999999;"><b>warning</b>: case sensitive!</span>)<br />
2. Value: 3, 5, 1.<br />
<br />
in R, as usual, you can type:<br />
<br />
stuDentS<- c(3,5,1)<br />
<br />
Then, let's start using barplot (hurrayyyy):<br />
<br />
barplot(stuDentS)<br />
<br />
Done! R will give us this amazing result!!!<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikUqLaJf5pf_8R5BrrcNudOA8jT_l27tbfKkMbslElRxorezSf7Oa_BQxwzsToOBnT0CYaZp6FAOWPb65_O5JHXfEE9NUiG0smCp08b131ZPNa0lNyUKW2HslYLF8CU-BFlfqjuNaHgFZX/s1600/barplot-pemrograman-r-blogspot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Barplot in R Programming" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikUqLaJf5pf_8R5BrrcNudOA8jT_l27tbfKkMbslElRxorezSf7Oa_BQxwzsToOBnT0CYaZp6FAOWPb65_O5JHXfEE9NUiG0smCp08b131ZPNa0lNyUKW2HslYLF8CU-BFlfqjuNaHgFZX/s1600/barplot-pemrograman-r-blogspot.png" height="536" title="Barplot in R Programming | pemrograman-r.blogspot.com" width="640" /></a></div>
<br />
<br />
Further, if you <i>assign new names in the vector values</i> (... say, <i>barney</i>, <i>herman </i>and <i>adhya</i>), R will take it as <i><b>Labels</b></i> which will be placed below each bars based on the order you give.<br />
For example:<br />
<br />
names(stuDentS)<-c("Aminah","Herman","Adhya")<br />
<br />
Then, R will give you this awesome chart!<br />
<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcIJM7AsQYBZHemL6ZA0bRrl57XfMe8FgBBXTjeNMAR-JNgbZ0YMRJIIR50CthxaaON56YRUfl4gc2h6sAmT-Vz9scLBl4DwbT93ydHx8y1vWnA1zUUXhk3RQfVKMvJ2Kt1EDaCsvpjm14/s1600/barplot-names-pemrograman-r-blogspot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="Barplot in R Programming" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjcIJM7AsQYBZHemL6ZA0bRrl57XfMe8FgBBXTjeNMAR-JNgbZ0YMRJIIR50CthxaaON56YRUfl4gc2h6sAmT-Vz9scLBl4DwbT93ydHx8y1vWnA1zUUXhk3RQfVKMvJ2Kt1EDaCsvpjm14/s1600/barplot-names-pemrograman-r-blogspot.png" height="363" title="Barplot in R Programming | pemrograman-r.blogspot.com" width="640" /></a></div>
<br />
<br />
Now, to show other types of chart, you can give range from 1 through 100, as in:<br />
<br />
barplot(1:100)<br />
<br />
Here's the result:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMjNAqsfHTNsQfvvj-SeGyOyOc3gorngB-6oPEcmCRJkAxNkvDsUkhXMx8T3WneJwcKu0trNiRKpGbJVc61hiIuIOByvCWH60gbXLUr260AJRhkoVPGy3dZeLgENJlEwJvbmDOFu8ZoYxI/s1600/pemrograman-r-blogspot-barplot.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgMjNAqsfHTNsQfvvj-SeGyOyOc3gorngB-6oPEcmCRJkAxNkvDsUkhXMx8T3WneJwcKu0trNiRKpGbJVc61hiIuIOByvCWH60gbXLUr260AJRhkoVPGy3dZeLgENJlEwJvbmDOFu8ZoYxI/s1600/pemrograman-r-blogspot-barplot.png" height="412" width="640" /></a></div>
<br />
<br />
<br />
<span style="color: #666666;"><i>See you on the next lesson :D</i><i>, my best regards.</i></span><br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2352675540099716709.post-60245664155262276302015-03-31T18:11:00.000-07:002015-04-03T14:24:34.085-07:00Learn Vectors<br />
<span style="color: #990000;"><u><b>We will learn:</b></u></span><br />
<br />
1. <a href="http://pemrograman-r.blogspot.com/2015/03/learn-vectors-r-programming.html#vectors" target="_parent" title="Vectors in R | Pemrograman R">Vectors</a><br />
2. <a href="http://pemrograman-r.blogspot.com/2015/03/learn-vectors-r-programming.html#sequence-vectors" target="_parent" title="Sequence Vectors in R | Pemrograman R">Sequence Vectors</a><br />
3. <a href="http://pemrograman-r.blogspot.com/2015/03/learn-vectors-r-programming.html#vector-access" target="_parent" title="Vectors Access in R | Pemrograman R">Vector Access</a><br />
<br />
<span style="color: #990000;"><u><b>Details:</b></u></span><br />
<br />
In <b>R Programming</b>, R handles many data types; such as, scalars, vectors (numerical, character/strings, logical), data frames, matrices, and lists. (as we learn in <a href="http://pemrograman-r.blogspot.com/2015/03/introduction-to-r.html" target="_blank">Introduction to R Programming Language For Beginner</a> page). However, in this chance, we'll discuss about Vectors.<br />
<br />
<h3>
<span id="vectors"><b>A. Vectors</b></span></h3>
<br />
When using vectors in R, we need to have the <b>same data types</b>, as follows:<br />
<br />
> c(1,9,5)<br />
Result: [1] 1 9 5 <br />
<br />
From the example above, we see that 1, 9 and 5 has the same data type. Note that <u><b>c</b></u> is short for word "<u><i>combine</i></u>". It combines (the same) values inside <u><b>c</b></u>'s braces.<br />
<br />
When <u><b>c</b></u> has different values of data types, it would shows all in strings data type. See below!<br />
<br />
> c(7,"Hello",FALSE)<br />
Result: [1] "7" "Hello" "FALSE"<br />
<br />
Now, let's go more details about Vectors.<br />
<br />
<h3>
<span id="sequence-vectors"><b>B. Sequence Vectors</b></span></h3>
<br />
Vectors also handles <i>sequence values</i> as in <u><b>c</b></u> functions. <i>e.g. c(1,2,3,4,5)</i>.<br />
The sequence value vector can be written as follows:<br />
<br />
> c(5:7)<br />
Result: [1] 5 6 7<br />
<br />
That means, when you write: 5:7, R will give the output 5 6 and 7. This is the way for you to order numbers in R. Other method to call the data inside braces which has the same function as in <u><b>c</b></u>, namely, <u><b>seq</b></u>.<br />
For example:<br />
<br />
> seq(5:7)<br />
Result: [1] 5 6 7<br />
<br />
When you input values that <i>less then</i> 1 in the back of that value, e.g. seq(5,7, <b>0.5</b>), etc., the last value (<b>0.5</b>) will be added up to each value:<br />
<br />
> seq(5,7,0.5)<br />
[1] 5.0 5.5 6.0 6.5 7.0 <br />
<br />
How is it done?<br />
<i>See this following illustration!</i><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPphy_Wllea4z3riRZRxrk-if_BZUhnYhBy9LotXLHpqShKgked9v4X2Ff5lqNiI07VEzZnugHt-h9k4a89rzkm-B9bKXp2bIGqhWE_1NDmK-hYULX_AryNU1BrZdS0NA3tFqUFSLsxYby/s1600/sequence-vector.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgPphy_Wllea4z3riRZRxrk-if_BZUhnYhBy9LotXLHpqShKgked9v4X2Ff5lqNiI07VEzZnugHt-h9k4a89rzkm-B9bKXp2bIGqhWE_1NDmK-hYULX_AryNU1BrZdS0NA3tFqUFSLsxYby/s1600/sequence-vector.png" /></a></div>
<br />
From the illustration we can see where 5 must reach 7 by adding 0.5.<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
When the value has 7 to 5 as in:<br />
<br />
7:5<br />
<i>or</i>,<br />
> seq(7:5)<br />
Result: [1] 7 6 5<br />
<br />
<h3>
<span id="vector-access"><b>C. Vector Access</b></span></h3>
<br />
<i><u>c.1.</u></i> Store Variable inside Vector C <br />
Now we will store vector value inside a variable. Let's take one variable, say, <b>wordlist</b>. Here, <b>wordlist</b> will store 3 values inside <u><b>c</b></u> vector.<br />
Example:<br />
<br />
> wordlist <- c('I', 'learn', 'R-language')<br />
<br />
If we order (<i>as well as</i> R interprets) the value, that will be like this:<br />
<br />
I = 1<br />
learn = 2<br />
R-language = 3<br />
<br />
Then, we try to pick number 2 of the values.<br />
Type like this! <br />
<br />
> wordlist [2]<br />
<br />
Then, R will give you the result:<br />
<br />
[1] "learn"<br />
<br />
From the example above we can see that the variable <b>wordlist</b> stores <u><b>c</b></u> vector value; <i>I</i>, <i>learn</i> and <i>R-language</i>. In R, the <i>three</i> will be ordered into 1, 2 and 3.<br />
<br />
<div style="text-align: center;">
<span style="color: red;"><b>Warning</b></span>! Array in R starts from <u><b>1</b></u> NOT <u><b>0</b></u> (zero)!</div>
<br />
<i><u>c.2.</u></i> Change Value <br />
<br />
Now, lets change one of the values stored in <u><b>c</b></u> vector. As we do above, the second value (no.2) is the word <i><b>learn</b></i>. Try to change it to <b>love</b>, as in:<br />
<br />
> wordlist[2] <- 'love'<br />
<br />
Then, pick the number to show it has successfully changed by typing as follows:<br />
<br />
> wordlist[2]<br />
<br />
R will give you the result: [1] "love"<br />
<br />
<i><u>c.3.</u></i> Add New Value <br />
<br />
Type as follows:<br />
> wordlist[4] <- 'badly'<br />
<br />
This is the way if you need to add new value and store it in <b>wordlist</b> variable. So, there are 4 values now; <b><i>I</i></b>, <i><b>love</b></i>, <i><b>R-language</b></i> and <i><b>badly</b></i>.<br />
<br />
<i><u>c.4.</u></i> Access more than one Value.<br />
<br />
If you want to access the stored variable and pick two or more value to show, then use <b>Square Brackets</b> / [ .......] where <u><b>c</b></u> vector within it.<br />
Example:<br />
<br />
wordlist [ c (2,4) ]<br />
Result: [1] "love" "badly"<br />
<br />
<i><u>c.5.</u></i> Access more than one value in a certain range.<br />
<br />
From the discussion above, we already have 4 values; <i>I</i>, <i>love</i>, <i>R-language</i>, <i>badly</i>. If we need to show more than one value <u>in a range</u>, say, <i><b>2</b></i> to <i><b>4</b></i> or <u><i>love</i></u> through <u><i>badly</i></u>, then you can type as follows:<br />
<br />
> wordlist [2:4]<br />
Result: [1] "love" "R-language" "badly"<br />
<br />
<i><b>Note</b></i> that the range value is inside square brackets!<br />
<br />
<i><u>c.6.</u></i> Add more than one value inside <u><b>c</b></u> vector<br />
<br />
If you want to add more than one value and store them inside the variable <i><b>wordlist</b></i>, then just type it and R will do that for you :p<br />
For example, we add three values at number 5,6 and 7.<br />
<br />
wordlist[5:7]<-c('and','you','do')<br />
<br />
Then, show it by typing as follows:<br />
<br />
> wordlist [6]<br />
Result: [1] "you" <br />
<br />
<br />Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2352675540099716709.post-46367426694725042112015-03-31T07:07:00.002-07:002015-04-03T14:30:11.752-07:00Introduction to R<br />
<br />
<u><span style="color: #cc0000;"><b>We will learn:</b></span></u><br />
<br />
1. <a href="http://pemrograman-r.blogspot.com/2015/03/introduction-to-r.html#expression" target="_parent" title="Expression | Pemrograman R">Expression</a><br />
2. <a href="http://pemrograman-r.blogspot.com/2015/03/introduction-to-r.html#logical-value" target="_parent" title="Logical Value| Pemrograman R">Logical Value</a><br />
3. <a href="http://pemrograman-r.blogspot.com/2015/03/introduction-to-r.html#variables" target="_parent" title="Variables | Pemrograman R">Variables</a><br />
4. <a href="http://pemrograman-r.blogspot.com/2015/03/introduction-to-r.html#functions" target="_parent" title="Functions | Pemrograman R">Functions</a><br />
5. <a href="http://pemrograman-r.blogspot.com/2015/03/introduction-to-r.html#help" target="_parent" title="Help | Pemrograman R">Help</a><br />
6. <a href="http://pemrograman-r.blogspot.com/2015/03/introduction-to-r.html#files" target="_parent" title="Files | Pemrograman R">Files</a><br />
<br />
<u><span style="color: #990000;"><b>Details:</b></span></u><br />
<br />
This following lesson is taken from <a href="http://tryr.codeschool.com/" target="_blank">http://tryr.codeschool.com/</a> . As a beginner, I finally found a place to practice R excellently (which is recommended by <a href="http://www.rstudio.com/resources/training/online-learning/#R" target="_blank">RStudio</a>. Thanks, bro - I also recommend you to start your R days in that amazing site.<br />
<br />
Here, we will learn some simple things of R commands and execution including, Expression, Logical Values, Variables, Functions, Helps and Files.<br />
<br />
<div style="text-align: center;">
<span style="color: red;"><b>Warning</b></span>! <b>R is Case Sensitive</b>! </div>
<br />
<h3>
<span id="expression"><b>A. Expression</b></span></h3>
<br />
<u>a.1</u>. <i>Simple Math Operation</i>. When we would like to get result from 1+1, then we don't need equals sign, as follows:<br />
<br />
> 1+1<br />
Result: [1] 2<br />
<br />
we can do the same with minus [-], multiplying [*] and devision [/]. See this following examples:<br />
<br />
<b>Minus</b><br />
> 9 -3<br />
Result: [1] 6<br />
<br />
<b>Multiplying</b><br />
> 4*2<br />
Result: [1] 8<br />
<br />
<b>Division</b><br />
> 10/2<br />
Result: [1] 5<br />
<br />
<u>a.2.</u> <i>String Value</i>. R takes string value written with Quotes, as follows:<br />
<br />
> "Hi, Everyone!"<br />
Result: [1] "Hi, Everyone!"<br />
<br />
<h3>
<span id="logical-value"><b>B. Logical Value</b></span></h3>
Talking about Logical value means talking about TRUE or FALSE. R has it as well as other programming languages do. Let's start!<br />
<br />
<u>b.1</u>. <i>To check if a statement is TRUE or FALSE </i>(<i>TRUE</i> or <i>FALSE</i> written in uppercase. Remember! It's a case sensitive).<br />
<br />
<br />
<b>Less than < </b><br />
> 6<9<br />
Result: [1] TRUE<br />
<br />
<b>More than ></b><br />
> 4>1<br />
Result: [1] TRUE<br />
<br />
<b>Equals </b>(Use double-equal-sign to do that!)<br />
6+4 == 12<br />
Result: [1] FALSE<br />
<br />
<b>T and F</b> (TRUE and FALSE shorthand)<br />
F==FALSE<br />
Result: [1] TRUE<br />
<br />
<h3>
<span id="variables"><b>C. Variables </b></span><span style="color: #999999;">(<i>"I love this stuff in all programming language"</i>)</span><b><br /></b></h3>
As you know that variables are used to store values. That stored values will be accessed later.<br />
Let's start!<br />
<br />
<u>c.1.</u> <b>Math Variable</b>. Here we will use a variable to store and, at last, we will do with simple math operation.<br />
Examples:<br />
<br />
> x<-50<br />
> x/2<br />
Result: [1] 25 <br />
<br />
<u>c.2.</u><b> String Variable</b>. Here, we store string in /z/ and execute it.<br />
Example:<br />
<br />
> z<- "Welcome!"<br />
> z<br />
Result: [1] "Welcome!"<br />
<br />
<u>c.3.</u><b> Logical Value</b>.<br />
Example:<br />
<br />
> x<-FALSE<br />
Result: [1] FALSE <br />
<br />
<h3>
<span id="functions"><b>D. Functions</b></span></h3>
Time to more complicated :D ... Here's we will do some Math Functions, such as, Sum, Average, etc.<br />
Lets' start!<br />
<br />
<u>d.1</u>. <b>Sum</b> (a comma means '<i>Plus</i>')<br />
> sum(2,5,3)<br />
Result: [1] 10<br />
<br />
<u>d.2.</u> <b>Repeat</b> a value (we use <b>rep</b> with times=...)<br />
<u><b>Formula</b></u>:<br />
<b>rep</b>(<i>string-value</i>,times=<i>numeric</i>)<br />
<br />
Example:<br />
> rep("Go!",times=4)<br />
Result: [1] "Go!" "Go!" "Go!" "Go!"<br />
<br />
<u>d.3.</u> <b>Square Root</b> Operation<br />
Example:<br />
<br />
> sqrt(16)<br />
Result [1] 4<br />
<br />
<h3>
<span id="help"><b>E. Help</b></span></h3>
How to get Help in R ?<br />
Lets' start!<br />
<br />
<u>e.1.</u> For <i>global help</i>, we type:<br />
> help()<br />
<br />
<u>e.2.</u> For <i>specific help</i>, type the specific word inside the braces, as follows:<br />
> help(c)<br />
<br />
Another example:<br />
> help(sum)<br />
<br />
<u><b>Note</b></u>: when you need to know how /sum/ or /c/ works in an example, then you type as follows:<br />
> example(sum)<br />
<br />
<i>or</i>,<br />
> example(c)<br />
<br />
<i>Another example</i>:<br />
> example (rep) <br />
<br />
R then gives you some examples about how to use them<br />
<br />
<h3>
<span id="files"><b>F. Files</b></span></h3>
When you have files listed (R Projects) in your directory (R's files has <i>dot+R</i> in its extension, e.g. <i>myfile.R</i>), you can list of them by typing <b>list.files()</b> functions, as follows:<br />
<br />
> list.files()<br />
<br />
Let's say that one of the files listed is: myfile.R. Now, run the files with:<br />
<br />
> source("myfile.R")<br />
<br />
<span style="color: #8e7cc3;">Warning! do not forget to use quotes inside the braces</span>.<br />
<i>Well</i>, that's all for the start since the tutorial I've got stop at that "F" too. :DUnknownnoreply@blogger.com0