{"id":2180,"date":"2024-07-13T22:03:24","date_gmt":"2024-07-13T22:03:24","guid":{"rendered":"https:\/\/ibp.ir\/?p=2180"},"modified":"2024-12-17T07:55:19","modified_gmt":"2024-12-17T08:55:19","slug":"3-%d8%b3%d8%a7%d8%ae%d8%aa%d8%a7%d8%b1-%d8%af%d8%a7%d8%af%d9%87%d8%a7-%d8%af%d8%b1-r","status":"publish","type":"post","link":"https:\/\/ibp.ir\/?p=2180","title":{"rendered":"3: \u0633\u0627\u062e\u062a\u0627\u0631 \u062f\u0627\u062f\u0647\u0627 \u062f\u0631 R"},"content":{"rendered":"<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>1-3R-programming-language-training-course-By-Babak-Babaabasi<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2149\" src=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-13-192003.png\" alt=\"\" width=\"570\" height=\"312\" srcset=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-13-192003.png 1153w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-13-192003-300x164.png 300w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-13-192003-1024x560.png 1024w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/Screenshot-2024-07-13-192003-768x420.png 768w\" sizes=\"auto, (max-width: 570px) 100vw, 570px\" \/><\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>(2)R Data Structures:<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">R\u2019s basic data structures include the <strong>vector, list, matrix, data frame, and factors<\/strong>.<\/p>\n<p dir=\"ltr\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2181\" src=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8.png\" alt=\"\" width=\"622\" height=\"425\" srcset=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8.png 1134w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-300x205.png 300w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-1024x700.png 1024w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-768x525.png 768w\" sizes=\"auto, (max-width: 622px) 100vw, 622px\" \/><\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Vectors<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">A vector is simply a list of items that are of the same type.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">To combine the list of items to a vector, use the c() function and separate the items by a comma.<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">In the example below, we create a vector variable called <strong>fruits<\/strong>, that combine strings:<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Vector of strings<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits = c(&#8220;banana&#8221;, &#8220;apple&#8221;, &#8220;orange&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print fruits<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>In this example, we create a vector that combines numerical values:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Vector of numerical values<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers = c(1, 2, 3)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print numbers<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To create a vector with numerical values in a sequence, use the : operator:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Vector with numerical values in a sequence<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers = 1:10<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can also create numerical values with decimals in a sequence, but note that if the last element does not belong to the sequence, it is not used:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Vector with numerical decimals in a sequence<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers1 &lt;- 1.5:6.5<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers1<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Vector with numerical decimals in a sequence where the last element is not used<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers2 &lt;- 1.5:6.3<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers2<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>In the example below, we create a vector of logical values:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">log_values &lt;- c(TRUE, FALSE, TRUE, FALSE)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">log_values<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Vector Length<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To find out how many items a vector has, use the length() function<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits &lt;- c(&#8220;banana&#8221;, &#8220;apple&#8221;, &#8220;orange&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">length(fruits)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Sort a Vector<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To sort items in a vector alphabetically or numerically, use the sort() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits &lt;- c(&#8220;banana&#8221;, &#8220;apple&#8221;, &#8220;orange&#8221;, &#8220;mango&#8221;, &#8220;lemon&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers &lt;- c(13, 3, 5, 7, 20, 2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">sort(fruits)\u00a0 # Sort a string<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">sort(numbers) # Sort numbers<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>*****Access Vectors<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can access the vector items by referring to its index number inside brackets []. The first item has index 1, the second item has index 2, and so on:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits &lt;- c(&#8220;banana&#8221;, &#8220;apple&#8221;, &#8220;orange&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Access the first item (banana)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits[1]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can also access multiple elements by referring to different index positions with the c() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits &lt;- c(&#8220;banana&#8221;, &#8220;apple&#8221;, &#8220;orange&#8221;, &#8220;mango&#8221;, &#8220;lemon&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Access the first and third item (banana and orange)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits[c(1:3)]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits[c(1,3)]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can also use negative index numbers to access all items except the ones specified:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits &lt;- c(&#8220;banana&#8221;, &#8220;apple&#8221;, &#8220;orange&#8221;, &#8220;mango&#8221;, &#8220;lemon&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Access all items except for the first item<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits[c(-1)]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits[c(-2)]<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Change an Item<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To change the value of a specific item, refer to the index number:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits &lt;- c(&#8220;banana&#8221;, &#8220;apple&#8221;, &#8220;orange&#8221;, &#8220;mango&#8221;, &#8220;lemon&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Change &#8220;banana&#8221; to &#8220;pear&#8221;<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits[1] = &#8220;pear&#8221;<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print fruits<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">fruits<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Repeat Vectors<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To repeat vectors, use the rep() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Repeat each value:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">repeat_each = rep(c(1,2,3), each = 3)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">repeat_each<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Repeat the sequence of the vector:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">repeat_times &lt;- rep(c(1,2,3), times = 3)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">bb=rep(c(1,2),repeat_times=6)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">repeat_times<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Repeat each value independently:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">repeat_indepent &lt;- rep(c(1,2,3), times = c(5,2,1))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">repeat_indepent<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Generating Sequenced Vectors<\/strong><\/h2>\n<h2 dir=\"ltr\" style=\"text-align: left;\">One of the examples on top, showed you how to create a vector with numerical values in a sequence with the : operator<\/h2>\n<p>&nbsp;<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers &lt;- 1:10<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To make bigger or smaller steps in a sequence, use the seq() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers &lt;- seq(from = 0, to = 100, by = 20)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">numbers<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">help(&#8220;rep&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Note:<\/strong> The seq() function has three parameters: from is where the sequence starts, to is where the sequence stops, and by is the interval of the sequence.<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>R Lists<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2181\" src=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8.png\" alt=\"\" width=\"633\" height=\"433\" srcset=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8.png 1134w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-300x205.png 300w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-1024x700.png 1024w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-768x525.png 768w\" sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>A list in R can contain many different data types inside it. A list is a collection of data which is ordered and changeable.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To create a list, use the list() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist &lt;- list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the list<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Change Item Value<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To change the value of a specific item, refer to the index number:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist &lt;- list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist[1] &lt;- &#8220;blackcurrant&#8221;<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the updated list<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>List Length<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To find out how many items a list has, use the length() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist &lt;- list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">length(thislist)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Check if Item Exists<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To find out if a specified item is present in a list, use the %in% operator:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist &lt;- list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;,&#8221;pendar&#8221;,&#8221;maryam&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">&#8220;maryam&#8221; %in% thislist<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Add List Items<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To add an item to the end of the list, use the append() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist &lt;- list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">append(thislist, &#8220;orange&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To add an item to the right of a specified index, add \u201cafter=index number\u201d in the append() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist = list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">append(thislist, &#8220;orange&#8221;, after = 2)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Remove List Items<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can also remove list items. The following example creates a new, updated list without an \u201capple\u201d item:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist = list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">newlist = thislist[-1]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the new list<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">newlist<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Range of Indexes<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can specify a range of indexes by specifying where to start and where to end the range, by using the : operator:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist &lt;- list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;, &#8220;orange&#8221;, &#8220;kiwi&#8221;, &#8220;melon&#8221;, &#8220;mango&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">(thislist)[2:5]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist[2:5]<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Loop Through a List<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can loop through the list items by using a for loop:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thislist &lt;- list(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">for (x in thislist) {<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 print(x)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">}<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Join Two Lists<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>There are several ways to join, or concatenate, two or more lists in R.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>The most common way is to use the c() function, which combines two elements together:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list1 = list(&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list2 = list(1,2,3)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list3 = c(list1,list2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list3<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># list1 and list2 are uni-dimensional lists<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list1 = list (c(1:5), &#8220;hi&#8221;, 0 + 5i)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list1<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list2 = list(c(6:8))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list2<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># create a list_data with two lists as its elements<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">list_data = list(list1, list2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">print (&#8220;The two-dimensional list is : &#8220;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">print (list_data)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">cat(&#8220;Length of nested list is : &#8220;,<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0\u00a0\u00a0\u00a0 length (list_data))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">cat(&#8220;Length of first inner list is : &#8220;,<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0\u00a0\u00a0\u00a0 length (list_data[[1]]))<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>R Matrices<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2182\" src=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10.png\" alt=\"\" width=\"684\" height=\"468\" srcset=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10.png 1134w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-300x205.png 300w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-1024x700.png 1024w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-768x525.png 768w\" sizes=\"auto, (max-width: 684px) 100vw, 684px\" \/><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>A matrix is a two dimensional data set with columns and rows.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>A column is a vertical representation of data, while a row is a horizontal representation of data.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>A matrix can be created with the matrix() function. Specify the nrow and ncol parameters to get the amount of rows and columns:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Create a matrix<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix = matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the matrix<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can also create a matrix with strings:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix &lt;- matrix(c(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;, &#8220;orange&#8221;), nrow = 2, ncol = 2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Access Matrix Items<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can access the items by using [ ] brackets. The first number \u201c1\u201d in the bracket specifies the row-position, while the second number \u201c2\u201d specifies the column-position:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix &lt;- matrix(c(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;, &#8220;orange&#8221;), nrow = 2, ncol = 2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix[1, 2]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>The whole row can be accessed if you specify a comma after the number in the bracket:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix &lt;- matrix(c(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;, &#8220;orange&#8221;), nrow = 2, ncol = 2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix[2,]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>The whole column can be accessed if you specify a comma before the number in the bracket:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix &lt;- matrix(c(&#8220;apple&#8221;, &#8220;banana&#8221;, &#8220;cherry&#8221;, &#8220;orange&#8221;), nrow = 2, ncol = 2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thismatrix[,2]<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>R Arrays<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2181\" src=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8.png\" alt=\"\" width=\"668\" height=\"457\" srcset=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8.png 1134w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-300x205.png 300w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-1024x700.png 1024w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/2-8-768x525.png 768w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Compared to matrices, arrays can have more than two dimensions.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>We can use the array() function to create an array, and the dim parameter to specify the dimensions:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># An array with one dimension with values ranging from 1 to 24<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thisarray &lt;- c(1:24)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thisarray<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># An array with more than one dimension<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray &lt;- array(thisarray, dim = c(4, 3, 2))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>How does dim=c(4,3,2) work? The first and second number in the bracket specifies the amount of rows and columns. The last number in the bracket specifies how many dimensions we want.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Note:<\/strong> Arrays can only have one data type.<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Access Array Items<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can access the array elements by referring to the index position. You can use the [] brackets to access the desired elements from an array:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thisarray &lt;- c(1:24)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray &lt;- array(thisarray, dim = c(4, 3, 2))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray[2, 3, 2]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can also access the whole row or column from a matrix in an array, by using the c() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thisarray &lt;- c(1:24)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Access all the items from the first row from matrix one<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray &lt;- array(thisarray, dim = c(4, 3, 2))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray[c(1),,1]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Access all the items from the first column from matrix one<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray &lt;- array(thisarray, dim = c(4, 3, 2))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray[,c(1),1]<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Check if an Item Exists<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To find out if a specified item is present in an array, use the %in% operator:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thisarray &lt;- c(1:24)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray &lt;- array(thisarray, dim = c(4, 3, 2))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">2 %in% multiarray<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Amount of Rows and Columns<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the dim() function to find the amount of rows and columns in an array:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thisarray &lt;- c(1:24)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray &lt;- array(thisarray, dim = c(4, 3, 2))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">dim(multiarray)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Array Length<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the length() function to find the dimension of an array:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thisarray &lt;- c(1:24)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray &lt;- array(thisarray, dim = c(4, 3, 2))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">length(multiarray)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Loop Through an Array<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can loop through the array items by using a for loop:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">thisarray &lt;- c(1:24)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">multiarray &lt;- array(thisarray, dim = c(4, 3, 2))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">for(x in multiarray){<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 print(x)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">}<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>R Data Frames<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2182\" src=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10.png\" alt=\"\" width=\"671\" height=\"459\" srcset=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10.png 1134w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-300x205.png 300w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-1024x700.png 1024w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-768x525.png 768w\" sizes=\"auto, (max-width: 671px) 100vw, 671px\" \/><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Data Frames are data displayed in a format as a table.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Data Frames can have different types of data inside it. While the first column can be character, the second and third can be numeric or logical. However, each column should have the same type of data.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the data.frame() function to create a data frame:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Create a data frame<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">da_fr &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the data frame<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">da_fr<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">#or<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">View(da_fr)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">a=c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">b=c(100, 150, 120)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">c=c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">df = data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = a,<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = b,<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">View(df)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Summarize the Data<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the summary() function to summarize the data from a Data Frame:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">da_fr &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">da_fr<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">summary(da_fr)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Access Items<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>We can use single brackets [ ], double brackets [[ ]] or $ to access columns from a data frame:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">da_fr &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">bb=da_fr[1]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">bb<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">da_fr[[&#8220;Training&#8221;]]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">da_fr$Training<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Add Rows<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the rbind() function to add new rows in a Data Frame:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">da_fr &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Add a new row<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">New_row_DF &lt;- rbind(da_fr, c(&#8220;Strength&#8221;, 110, 110))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the new row<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">New_row_DF<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Add Columns<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the cbind() function to add new columns in a Data Frame:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Add a new column<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">New_col_DF &lt;- cbind(Data_Frame, Steps = c(1000, 6000, 2000))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the new column<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">New_col_DF<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Remove Rows and Columns<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the c() function to remove rows and columns in a Data Frame:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Remove the first row and column<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame_New &lt;- Data_Frame[-c(1), -c(1)]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the new data frame<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame_New<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Amount of Rows and Columns<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the dim() function to find the amount of rows and columns in a Data Frame:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">dim(Data_Frame)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">class(Data_Frame)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can also use the ncol() function to find the number of columns and nrow() to find the number of rows:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">ncol(Data_Frame)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">nrow(Data_Frame)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Data Frame Length<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the length() function to find the number of columns in a Data Frame (similar to ncol()):&#8217;<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">ncol(Data_Frame)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">nrow(Data_Frame)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">length(Data_Frame)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Combining Data Frames<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the rbind() function to combine two or more data frames in R vertically:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame1 &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame2 &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Stamina&#8221;, &#8220;Stamina&#8221;, &#8220;Strength&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(140, 150, 160),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(30, 30, 20)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">New_Data_Frame &lt;- rbind(Data_Frame1, Data_Frame2)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">New_Data_Frame<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>And use the cbind() function to combine two or more data frames in R horizontally:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame3 &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Training = c(&#8220;Strength&#8221;, &#8220;Stamina&#8221;, &#8220;Other&#8221;),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Pulse = c(100, 150, 120),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Duration = c(60, 30, 45)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">Data_Frame4 &lt;- data.frame (<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Steps = c(3000, 6000, 2000),<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">\u00a0 Calories = c(300, 400, 300)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">New_Data_Frame1 &lt;- cbind(Data_Frame3, Data_Frame4)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">New_Data_Frame1<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>R Factors<\/strong><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2182\" src=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10.png\" alt=\"\" width=\"680\" height=\"465\" srcset=\"https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10.png 1134w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-300x205.png 300w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-1024x700.png 1024w, https:\/\/ibp.ir\/wp-content\/uploads\/2024\/07\/4-10-768x525.png 768w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Factors are used to categorize data. Examples of factors are:<\/strong><\/p>\n<ul dir=\"ltr\" style=\"text-align: left;\">\n<li>Demography: Male\/Female<\/li>\n<li>Music: Rock, Pop, Classic, Jazz<\/li>\n<li>Training: Strength, Stamina<\/li>\n<\/ul>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To create a factor, use the factor() function and add a vector as argument:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Create a factor<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre &lt;- factor(c(&#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Classic&#8221;, &#8220;Classic&#8221;, &#8220;Pop&#8221;, &#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Jazz&#8221;))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"># Print the factor<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can see from the example above that that the factor has four levels (categories): Classic, Jazz, Pop and Rock.<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To only print the levels, use the levels() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre &lt;- factor(c(&#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Classic&#8221;, &#8220;Classic&#8221;, &#8220;Pop&#8221;, &#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Jazz&#8221;))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">levels(music_genre)<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>You can also set the levels, by adding the levels argument inside the factor() function:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre &lt;- factor(c(&#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Classic&#8221;, &#8220;Classic&#8221;, &#8220;Pop&#8221;, &#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Jazz&#8221;), levels = c(&#8220;Classic&#8221;, &#8220;Jazz&#8221;, &#8220;Pop&#8221;, &#8220;Rock&#8221;, &#8220;Other&#8221;))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">levels(music_genre)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Factor Length<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Use the length() function to find out how many items there are in the factor:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre &lt;- factor(c(&#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Classic&#8221;, &#8220;Classic&#8221;, &#8220;Pop&#8221;, &#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Jazz&#8221;))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">length(music_genre)<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Access Factors<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To access the items in a factor, refer to the index number, using [] brackets:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre &lt;- factor(c(&#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Classic&#8221;, &#8220;Classic&#8221;, &#8220;Pop&#8221;, &#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Jazz&#8221;))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre[3]<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>Change Item Value<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>To change the value of a specific item, refer to the index number:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre &lt;- factor(c(&#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Classic&#8221;, &#8220;Classic&#8221;, &#8220;Pop&#8221;, &#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Jazz&#8221;))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre[3] = &#8220;Pop&#8221;<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre[3]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>Note that you cannot change the value of a specific item if it is not already specified in the factor. The following example will produce an error:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre &lt;- factor(c(&#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Classic&#8221;, &#8220;Classic&#8221;, &#8220;Pop&#8221;, &#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Jazz&#8221;))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre[3] &lt;- &#8220;Opera&#8221;<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre[3]<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\"><strong>However, if you have already specified it inside the levels argument, it will work:<\/strong><\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre &lt;- factor(c(&#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Classic&#8221;, &#8220;Classic&#8221;, &#8220;Pop&#8221;, &#8220;Jazz&#8221;, &#8220;Rock&#8221;, &#8220;Jazz&#8221;), levels = c(&#8220;Classic&#8221;, &#8220;Jazz&#8221;, &#8220;Pop&#8221;, &#8220;Rock&#8221;, &#8220;Opera&#8221;))<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre[3] &lt;- &#8220;Opera&#8221;<\/p>\n<p dir=\"ltr\" style=\"text-align: left;\">music_genre[3]<\/p>\n<h2 dir=\"ltr\" style=\"text-align: left;\"><strong>END(1-3)<\/strong><\/h2>\n<p dir=\"ltr\" style=\"text-align: left;\">\n","protected":false},"excerpt":{"rendered":"<p>1-3R-programming-language-training-course-By-Babak-Babaabasi (2)R Data Structures: R\u2019s basic data structures include the vector, list, matrix, data frame, and factors. Vectors A vector is simply a list of items that are of the same type. To combine the list of items to a vector, use the c() function and separate the items by a comma. In the example below, we create a vector variable called fruits, that combine strings: # Vector of strings fruits = c(&#8220;banana&#8221;, &#8220;apple&#8221;, &#8220;orange&#8221;) # Print[&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":2181,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2180","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-1"],"views":80,"_links":{"self":[{"href":"https:\/\/ibp.ir\/index.php?rest_route=\/wp\/v2\/posts\/2180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibp.ir\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibp.ir\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibp.ir\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibp.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2180"}],"version-history":[{"count":6,"href":"https:\/\/ibp.ir\/index.php?rest_route=\/wp\/v2\/posts\/2180\/revisions"}],"predecessor-version":[{"id":2193,"href":"https:\/\/ibp.ir\/index.php?rest_route=\/wp\/v2\/posts\/2180\/revisions\/2193"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibp.ir\/index.php?rest_route=\/wp\/v2\/media\/2181"}],"wp:attachment":[{"href":"https:\/\/ibp.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibp.ir\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibp.ir\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}