{"id":895,"date":"2025-07-04T05:52:50","date_gmt":"2025-07-04T05:52:50","guid":{"rendered":"https:\/\/www.actualtests.com\/blog\/?p=895"},"modified":"2025-07-04T05:53:00","modified_gmt":"2025-07-04T05:53:00","slug":"an-introduction-to-data-structures-in-r","status":"publish","type":"post","link":"https:\/\/www.actualtests.com\/blog\/an-introduction-to-data-structures-in-r\/","title":{"rendered":"An Introduction to Data Structures in R"},"content":{"rendered":"\n<p>Data structures are fundamental for organizing and managing data efficiently in any programming language. Essentially, a data structure is a method to arrange data within a system to facilitate effective access and modification. The main goal is to minimize complexities related to both space and time during various computational tasks.<\/p>\n\n\n\n<p>When working with programming languages like R, variables serve as the basic containers for storing different types of data. Each variable reserves a specific memory location where its values are stored. Once a variable is created, memory is allocated for it to hold the data.<\/p>\n\n\n\n<p>In R, data structures are the primary objects that users interact with and manipulate. They provide an organized way to store data, making data manipulation, analysis, and other operations more efficient. R supports several types of data structures, each designed to handle data in specific formats or forms. This part introduces the most basic and widely used data structures in R, starting with vectors.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Recap: What Is a Vector?<\/strong><\/h2>\n\n\n\n<p>A vector is a one-dimensional array that holds elements of the same data type. This homogeneity is a key property: no mixing of types is allowed without automatic coercion.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Vectors<\/strong><\/h2>\n\n\n\n<p>R has several atomic vector types:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Logical<\/strong> (TRUE, FALSE)<br><\/li>\n\n\n\n<li><strong>Integer<\/strong> (1L, 2L) \u2014 note the L suffix forces integer type<br><\/li>\n\n\n\n<li><strong>Double (numeric)<\/strong> (e.g., 3.14, 5.0) \u2014 default for numbers with decimals<br><\/li>\n\n\n\n<li><strong>Character<\/strong> (strings)<br><\/li>\n\n\n\n<li><strong>Complex<\/strong> (complex numbers like 1 + 2i)<br><\/li>\n<\/ul>\n\n\n\n<p>You can check the type of any vector with:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>typeof(vec)<\/p>\n\n\n\n<p>and its class with<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>class(vec)<\/p>\n\n\n\n<p>Typically, vectors are also considered objects of class numeric, integer, etc., for method dispatch.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Creating Vectors<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using <\/strong><strong>c()<\/strong><\/h3>\n\n\n\n<p>The most common way:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(1, 2, 3, 4)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using <\/strong><strong>vector()<\/strong><\/h3>\n\n\n\n<p>Creates an empty vector of specified type and length:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- vector(&#8220;numeric&#8221;, length = 5)<\/p>\n\n\n\n<p>print(v)&nbsp; # prints 0 0 0 0 0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using <\/strong><strong>seq()<\/strong><\/h3>\n\n\n\n<p>Generates regular sequences:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>seq(1, 10, by = 2)&nbsp; # 1 3 5 7 9<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using <\/strong><strong>rep()<\/strong><\/h3>\n\n\n\n<p>Repeats values:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>rep(1:3, times = 3)&nbsp; # 1 2 3 1 2 3 1 2 3<\/p>\n\n\n\n<p>rep(1:3, each = 3) &nbsp; # 1 1 1 2 2 2 3 3 3<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Coercion Rules and Mixing Types<\/strong><\/h2>\n\n\n\n<p>When combining elements of different types in a vector, R coerces them to the most general type according to the hierarchy:<\/p>\n\n\n\n<p>sql<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>logical &lt; integer &lt; double &lt; complex &lt; character<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>mixed &lt;- c(TRUE, 1L, 3.14, &#8220;hello&#8221;)<\/p>\n\n\n\n<p>typeof(mixed)&nbsp; # &#8220;character&#8221;<\/p>\n\n\n\n<p>print(mixed) &nbsp; # &#8220;TRUE&#8221; &#8220;1&#8221; &#8220;3.14&#8221; &#8220;hello&#8221;<\/p>\n\n\n\n<p>This implicit coercion can sometimes cause bugs, so be mindful.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Vector Indexing \u2014 More Details<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Positive indexing<\/strong><\/h3>\n\n\n\n<p>Access elements by position (starting from 1):<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(10, 20, 30, 40)<\/p>\n\n\n\n<p>v[2]&nbsp; # 20<\/p>\n\n\n\n<p>v[c(1, 4)]&nbsp; # 10 40<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Negative indexing<\/strong><\/h3>\n\n\n\n<p>Excludes elements:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v[-3]&nbsp; # all except the 3rd element: 10 20 40<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Logical indexing<\/strong><\/h3>\n\n\n\n<p>Use a logical vector to pick elements:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v[c(TRUE, FALSE, TRUE, FALSE)]&nbsp; # 10 30<\/p>\n\n\n\n<p>This must be the same length or recycled accordingly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Named vectors<\/strong><\/h3>\n\n\n\n<p>You can assign names to elements and index by name:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(a = 10, b = 20, c = 30)<\/p>\n\n\n\n<p>v[&#8220;b&#8221;]&nbsp; # 20<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using <\/strong><strong>which()<\/strong><\/h3>\n\n\n\n<p>Find indices that satisfy a condition:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(5, 10, 15, 20)<\/p>\n\n\n\n<p>which(v &gt; 10)&nbsp; # 3 4<\/p>\n\n\n\n<p>v[which(v &gt; 10)]&nbsp; # 15 20<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Vector Arithmetic in Depth<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Element-wise operations<\/strong><\/h3>\n\n\n\n<p>Standard arithmetic operations (+, -, *, \/, ^) are <strong>element-wise<\/strong>:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>a &lt;- c(1, 2, 3)<\/p>\n\n\n\n<p>b &lt;- c(4, 5, 6)<\/p>\n\n\n\n<p>a + b&nbsp; # 5 7 9<\/p>\n\n\n\n<p>a * b&nbsp; # 4 10 18<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Recycling rule<\/strong><\/h3>\n\n\n\n<p>If vectors differ in length, the shorter one is recycled:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>a &lt;- c(1, 2, 3, 4)<\/p>\n\n\n\n<p>b &lt;- c(10, 20)<\/p>\n\n\n\n<p>a + b&nbsp; # 11 22 13 24&nbsp; (b recycled as 10,20,10,20)<\/p>\n\n\n\n<p>Be cautious: if the longer vector length is not a multiple of the shorter vector length, R throws a warning.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Vectorized Functions and Operations<\/strong><\/h2>\n\n\n\n<p>One of R\u2019s strengths is vectorization \u2014 functions that operate on vectors element-wise efficiently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Mathematical functions:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>sqrt(v) \u2014 square root<br><\/li>\n\n\n\n<li>log(v) \u2014 natural logarithm<br><\/li>\n\n\n\n<li>exp(v) \u2014 exponentiation<br><\/li>\n\n\n\n<li>abs(v) \u2014 absolute value<br><\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(-1, 0, 1, 4)<\/p>\n\n\n\n<p>abs(v)&nbsp; # 1 0 1 4<\/p>\n\n\n\n<p>sqrt(abs(v))&nbsp; # 1 0 1 2<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Summary functions:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sum (v) \u2014 sum of all elements<br><\/li>\n\n\n\n<li>prod(v) \u2014 product of all elements<br><\/li>\n\n\n\n<li>mean(v) \u2014 average<br><\/li>\n\n\n\n<li>median(v) \u2014 median<br><\/li>\n\n\n\n<li>min(v), max(v) \u2014 minimum and maximum<br><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Logical Vectors and Boolean Operations<\/strong><\/h2>\n\n\n\n<p>Logical vectors store TRUE or FALSE.<\/p>\n\n\n\n<p>You can create them by comparisons:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(2, 5, 8, 1)<\/p>\n\n\n\n<p>v &gt; 4&nbsp; # FALSE TRUE TRUE FALSE<\/p>\n\n\n\n<p>Logical operations work element-wise:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>&amp; (and)<br><\/li>\n\n\n\n<li>| (or)<br><\/li>\n\n\n\n<li>! (not)<br><\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>a &lt;- c(TRUE, FALSE, TRUE)<\/p>\n\n\n\n<p>b &lt;- c(FALSE, FALSE, TRUE)<\/p>\n\n\n\n<p>a &amp; b&nbsp; # FALSE FALSE TRUE<\/p>\n\n\n\n<p>a | b&nbsp; # TRUE FALSE TRUE<\/p>\n\n\n\n<p>!a &nbsp; &nbsp; # FALSE TRUE FALSE<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Naming Vector Elements<\/strong><\/h2>\n\n\n\n<p>Naming vector elements can make your code clearer:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>temps &lt;- c(30, 35, 28, 25)<\/p>\n\n\n\n<p>names(temps) &lt;- c(&#8220;Mon&#8221;, &#8220;Tue&#8221;, &#8220;Wed&#8221;, &#8220;Thu&#8221;)<\/p>\n\n\n\n<p>temps[&#8220;Tue&#8221;]&nbsp; # 35<\/p>\n\n\n\n<p>You can also name elements on creation:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>temps &lt;- c(Mon = 30, Tue = 35, Wed = 28, Thu = 25)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Modifying Vector Elements<\/strong><\/h2>\n\n\n\n<p>Assign new values using indices:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(1, 2, 3)<\/p>\n\n\n\n<p>v[2] &lt;- 10<\/p>\n\n\n\n<p>print(v)&nbsp; # 1 10 3<\/p>\n\n\n\n<p>Assigning with logical indices:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v[v &gt; 2] &lt;- 0<\/p>\n\n\n\n<p>print(v)&nbsp; # 1 10 0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Useful Vector Functions<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>length(v) \u2014 returns the number of elements<br><\/li>\n\n\n\n<li>unique(v) \u2014 returns unique elements<br><\/li>\n\n\n\n<li>duplicated(v) \u2014 returns a logical vector indicating duplicates<br><\/li>\n\n\n\n<li>Rev (v) \u2014 reverses the vector.<br>Any y(v) \u2014 returns TRUE if any element is TRUE (useful with logical vectors)<br>All l(v) \u2014 returns TRUE if all elements are TRUE.E<br><\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(1, 2, 2, 3, 4)<\/p>\n\n\n\n<p>unique(v) &nbsp; &nbsp; &nbsp; # 1 2 3 4<\/p>\n\n\n\n<p>duplicated(v) &nbsp; # FALSE FALSE TRUE FALSE FALSE<\/p>\n\n\n\n<p>rev(v)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 4 3 2 2 1<\/p>\n\n\n\n<p>any(v &gt; 3)&nbsp; &nbsp; &nbsp; # TRUE<\/p>\n\n\n\n<p>all(v &gt; 0)&nbsp; &nbsp; &nbsp; # TRUE<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Coercion and Type CheckinFunctionse<\/strong>s. numeric(v)<br><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>is.integer(v)<br><\/li>\n\n\n\n<li>is.character(v)<br><\/li>\n\n\n\n<li>is.logical(v)<br><\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(1, 2, 3)<\/p>\n\n\n\n<p>is.numeric(v)&nbsp; # TRUE<\/p>\n\n\n\n<p>is.integer(v)&nbsp; # FALSE, because the default numeric type is double<\/p>\n\n\n\n<p>v2 &lt;- c(1L, 2L)<\/p>\n\n\n\n<p>is.integer(v2) # TRUE<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Missing Values (NA) in Vectors<\/strong><\/h2>\n\n\n\n<p>NA represents missing or undefined values in R.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(1, NA, 3)<\/p>\n\n\n\n<p>sum(v)&nbsp; # returns NA by default<\/p>\n\n\n\n<p>sum(v, na.rm = TRUE)&nbsp; # 4, removes NA<\/p>\n\n\n\n<p>You can test for NA values with is.na():<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>is.na(v)&nbsp; # FALSE TRUE FALSE<\/p>\n\n\n\n<p>Missing values propagate through most operations unless handled explicitly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Factors vs. Character Vectors<\/strong><\/h2>\n\n\n\n<p>Factors are special vectors that represent categorical data with fixed levels.<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- factor(c(&#8220;low&#8221;, &#8220;medium&#8221;, &#8220;high&#8221;, &#8220;medium&#8221;))<\/p>\n\n\n\n<p>levels(v)&nbsp; # &#8220;high&#8221; &#8220;low&#8221; &#8220;medium&#8221;<\/p>\n\n\n\n<p>typeof(v)&nbsp; # &#8220;integer&#8221;<\/p>\n\n\n\n<p>class(v) &nbsp; # &#8220;factor&#8221;<\/p>\n\n\n\n<p>They store the underlying data as integers but print as categories. Factors are essential for statistical modeling.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Subsetting with <\/strong><strong>which()<\/strong><strong> and Logical Conditions<\/strong><\/h2>\n\n\n\n<p>To extract elements meeting conditions, use:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(3, 6, 9, 12)<\/p>\n\n\n\n<p>v[v &gt; 5]&nbsp; &nbsp; &nbsp; &nbsp; # 6 9 12<\/p>\n\n\n\n<p>v[which(v &gt; 5)] # same as above<\/p>\n\n\n\n<p>Which gives indices; direct logical indexing extracts elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Combining Vectors<\/strong><\/h2>\n\n\n\n<p>You can concatenate vectors with c():<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>a &lt;- c(1, 2, 3)<\/p>\n\n\n\n<p>b &lt;- c(4, 5)<\/p>\n\n\n\n<p>c &lt;- c(a, b)&nbsp; # 1 2 3 4 5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Vector Recycling \u2014 Detailed Example<\/strong><\/h2>\n\n\n\n<p>Recycling is a powerful feature, but it must be used with care.<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v1 &lt;- c(1, 2, 3, 4, 5)<\/p>\n\n\n\n<p>v2 &lt;- c(10, 20)<\/p>\n\n\n\n<p>v1 + v2&nbsp; # 11 22 13 24 15&nbsp; (v2 recycled as 10 20 10 20 10)<\/p>\n\n\n\n<p>If lengths don\u2019t align evenly, R warns:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v1 &lt;- c(1, 2, 3, 4, 5)<\/p>\n\n\n\n<p>v2 &lt;- c(10, 20, 30)<\/p>\n\n\n\n<p>v1 + v2<\/p>\n\n\n\n<p># Warning: longer object length is not a multiple of shorter object length<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Sorting and Ordering Vectors<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>sort() returns a sorted vector.<br><\/li>\n\n\n\n<li>Order () returns indices to sort the vector.<br><\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- c(7, 2, 9, 4)<\/p>\n\n\n\n<p>sort(v)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 2 4 7 9<\/p>\n\n\n\n<p>order(v) &nbsp; &nbsp; &nbsp; &nbsp; # 2 4 1 3 (positions of sorted elements)<\/p>\n\n\n\n<p>v[order(v)]&nbsp; &nbsp; &nbsp; # 2 4 7 9<\/p>\n\n\n\n<p>You can sort descending with:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>sort(v, decreasing = TRUE)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Additional Useful Vector Functions<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>sample(v, size) \u2014 randomly sample elements from a vector<br><\/li>\n\n\n\n<li>match(x, table) \u2014 find positions of elements of x in table<br><\/li>\n\n\n\n<li>setdiff(x, y) \u2014 elements in x but not in y<br><\/li>\n\n\n\n<li>intersect(x, y) \u2014 common elements of x and y<br><\/li>\n\n\n\n<li>union(x, y) \u2014 all unique elements from x and y combined<br><\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>x &lt;- c(1, 2, 3)<\/p>\n\n\n\n<p>y &lt;- c(3, 4, 5)<\/p>\n\n\n\n<p>setdiff(x, y) &nbsp; &nbsp; # 1 2<\/p>\n\n\n\n<p>intersect(x, y) &nbsp; # 3<\/p>\n\n\n\n<p>union(x, y) &nbsp; &nbsp; &nbsp; # 1 2 3 4 5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Working with Large Vectors<\/strong><\/h2>\n\n\n\n<p>For big data, vectors can become huge. Use functions like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Length () to check size<br><\/li>\n\n\n\n<li>head() and tail() to view subsets<br>Summary y() for quick stats<br><\/li>\n<\/ul>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>v &lt;- rnorm(1000000)<\/p>\n\n\n\n<p>length(v)&nbsp; # 1,000,000<\/p>\n\n\n\n<p>head(v)&nbsp; &nbsp; # first 6 elements<\/p>\n\n\n\n<p>summary(v) # min, max, median, quartiles<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Lists<\/strong><\/h2>\n\n\n\n<p>Unlike vectors, lists are non-homogeneous data structures, meaning they can contain elements of different types. Lists can hold numbers, characters, vectors, other lists, matrices, and even functions.<\/p>\n\n\n\n<p>Lists are created using the list() function.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>list1 &lt;- list(&#8220;Sam&#8221;, &#8220;Green&#8221;, c(8, 2, 67), TRUE, 51.99, 11.78, FALSE)<\/p>\n\n\n\n<p>print(list1)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>[[1]]<\/p>\n\n\n\n<p>[1] &#8220;Sam&#8221;<\/p>\n\n\n\n<p>[[2]]<\/p>\n\n\n\n<p>[1] &#8220;Green&#8221;<\/p>\n\n\n\n<p>[[3]]<\/p>\n\n\n\n<p>[1] 8 2 67<\/p>\n\n\n\n<p>[[4]]<\/p>\n\n\n\n<p>[1] TRUE<\/p>\n\n\n\n<p>[[5]]<\/p>\n\n\n\n<p>[1] 51.99<\/p>\n\n\n\n<p>[[6]]<\/p>\n\n\n\n<p>[1] 11.78<\/p>\n\n\n\n<p>[[7]]<\/p>\n\n\n\n<p>[1] FALSE<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing Elements in a List<\/strong><\/h3>\n\n\n\n<p>Elements of a list can be accessed by using their indices.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>list2 &lt;- list(matrix(c(3, 9, 5, 1, -2, 8), nrow = 2), c(&#8220;Jan&#8221;, &#8220;Feb&#8221;, &#8220;Mar&#8221;), list(3, 4, 5))<\/p>\n\n\n\n<p>print(list2[1])<\/p>\n\n\n\n<p>print(list2[2])<\/p>\n\n\n\n<p>print(list2[3])<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>[[1]]<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,1] [,2] [,3]<\/p>\n\n\n\n<p>[1,]&nbsp; &nbsp; 3&nbsp; &nbsp; 5 &nbsp; -2<\/p>\n\n\n\n<p>[2,]&nbsp; &nbsp; 9&nbsp; &nbsp; 1&nbsp; &nbsp; 8<\/p>\n\n\n\n<p>[[2]]<\/p>\n\n\n\n<p>[1] &#8220;Jan&#8221; &#8220;Feb&#8221; &#8220;Mar&#8221;<\/p>\n\n\n\n<p>[[3]]<\/p>\n\n\n\n<p>[[3]][[1]]<\/p>\n\n\n\n<p>[1] 3<\/p>\n\n\n\n<p>[[3]][[2]]<\/p>\n\n\n\n<p>[1] 4<\/p>\n\n\n\n<p>[[3]][[3]]<\/p>\n\n\n\n<p>[1] 5<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding and Deleting List Elements<\/strong><\/h3>\n\n\n\n<p>You can add elements to the end of a list by assigning a new value to the next index, and remove elements by assigning NULL.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>list2[4] &lt;- &#8220;HELLO&#8221;<\/p>\n\n\n\n<p>print(list2[4])<\/p>\n\n\n\n<p>list2[4] &lt;- NULL<\/p>\n\n\n\n<p>print(list2[4])<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>[[1]]<\/p>\n\n\n\n<p>[1] &#8220;HELLO&#8221;<\/p>\n\n\n\n<p>[[1]]<\/p>\n\n\n\n<p>NULL<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Updating Elements of a List<\/strong><\/h3>\n\n\n\n<p>To update an element, assign a new value to the specific index.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<p>list2[3] &lt;- &#8220;Element Updated&#8221;<\/p>\n\n\n\n<p>print(list2[3])<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>[[1]]<\/p>\n\n\n\n<p>[1] &#8220;Element Updated&#8221;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Lists in R Programming<\/strong><\/h2>\n\n\n\n<p>Lists are an important and versatile data structure in R. Unlike vectors, which are homogeneous and contain elements of the same data type, lists are non-homogeneous and can store elements of different types together. These elements can include numbers, characters, vectors, other lists, matrices, functions, or any other objects. This flexibility makes lists very useful when you want to store complex or mixed data types in a single structure.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Lists<\/strong><\/h3>\n\n\n\n<p>Lists are created using the list() function. You can pass any number of elements to this function, each of any type. For example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>list1 &lt;- list(&#8220;Sam&#8221;, &#8220;Green&#8221;, c(8, 2, 67), TRUE, 51.99, 11.78, FALSE)<\/p>\n\n\n\n<p>print(list1)<\/p>\n\n\n\n<p>The output will show each element of the list in its position:<\/p>\n\n\n\n<p>lua<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>[[1]]<\/p>\n\n\n\n<p>[1] &#8220;Sam&#8221;<\/p>\n\n\n\n<p>[[2]]<\/p>\n\n\n\n<p>[1] &#8220;Green&#8221;<\/p>\n\n\n\n<p>[[3]]<\/p>\n\n\n\n<p>[1] 8 2 67<\/p>\n\n\n\n<p>[[4]]<\/p>\n\n\n\n<p>[1] TRUE<\/p>\n\n\n\n<p>[[5]]<\/p>\n\n\n\n<p>[1] 51.99<\/p>\n\n\n\n<p>[[6]]<\/p>\n\n\n\n<p>[1] 11.78<\/p>\n\n\n\n<p>[[7]]<\/p>\n\n\n\n<p>[1] FALSE<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing List Elements<\/strong><\/h3>\n\n\n\n<p>You can access elements in a list by using their indices, enclosed in square brackets. For example, if you have a list:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>list2 &lt;- list(matrix(c(3, 9, 5, 1, -2, 8), nrow = 2), c(&#8220;Jan&#8221;, &#8220;Feb&#8221;, &#8220;Mar&#8221;), list(3, 4, 5))<\/p>\n\n\n\n<p>Accessing the first, second, and third elements can be done as follows:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(list2[1])&nbsp; # Returns the first element (a matrix)<\/p>\n\n\n\n<p>print(list2[2])&nbsp; # Returns the second element (a vector of month names)<\/p>\n\n\n\n<p>print(list2[3])&nbsp; # Returns the third element (a nested list)<\/p>\n\n\n\n<p>If you want to access elements inside the nested list within the list, use double square brackets:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(list2[[3]][[1]])&nbsp; # Outputs 3<\/p>\n\n\n\n<p>print(list2[[3]][[2]])&nbsp; # Outputs 4<\/p>\n\n\n\n<p>print(list2[[3]][[3]])&nbsp; # Outputs 5<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding and Deleting List Elements<\/strong><\/h3>\n\n\n\n<p>You can add elements to a list by assigning a new value at the next index:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>list2[4] &lt;- &#8220;HELLO&#8221;<\/p>\n\n\n\n<p>print(list2[4])<\/p>\n\n\n\n<p>This will add a new element at position 4. To delete an element, assign NULL to that index:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>list2[4] &lt;- NULL<\/p>\n\n\n\n<p>print(list2[4])&nbsp; # This will return NULL since the element is removed<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Updating List Elements<\/strong><\/h3>\n\n\n\n<p>To update an existing element in the list, simply assign a new value to the desired index:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>list2[3] &lt;- &#8220;Element Updated&#8221;<\/p>\n\n\n\n<p>print(list2[3])<\/p>\n\n\n\n<p>This replaces the third element in the list with the string &#8220;Element Updated&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Matrices in R Programming<\/strong><\/h2>\n\n\n\n<p>Matrices are two-dimensional data structures in R that hold elements of the same data type. They can be considered as vectors with a dimension attribute. The elements in a matrix are arranged in rows and columns.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Matrices<\/strong><\/h3>\n\n\n\n<p>You can create a matrix using the matrix() function. The primary arguments include the data elements as a vector, the number of rows, the number of columns, whether to fill the matrix by row or by column, and optional dimension names.<\/p>\n\n\n\n<p>The syntax is:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>matrix(data, nrow, ncol, byrow, dimnames)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Data is a vector containing the elements.<br><\/li>\n\n\n\n<li>nrow specifies the number of rows.<br><\/li>\n\n\n\n<li>ncol specifies the number of columns.<br><\/li>\n\n\n\n<li>byrow is a logical value indicating whether to fill the matrix by rows (TRUE) or by columns (FALSE, default).<br><\/li>\n\n\n\n<li>dimnames is a list containing optional row and column names.<br><\/li>\n<\/ul>\n\n\n\n<p>Example of creating a 3&#215;3 matrix filled by rows:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>M1 &lt;- matrix(c(1:9), nrow = 3, ncol = 3, byrow = TRUE)<\/p>\n\n\n\n<p>print(M1)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>css<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;[,1] [,2] [,3]<\/p>\n\n\n\n<p>[1,]&nbsp; &nbsp; 1&nbsp; &nbsp; 2&nbsp; &nbsp; 3<\/p>\n\n\n\n<p>[2,]&nbsp; &nbsp; 4&nbsp; &nbsp; 5&nbsp; &nbsp; 6<\/p>\n\n\n\n<p>[3,]&nbsp; &nbsp; 7&nbsp; &nbsp; 8&nbsp; &nbsp; 9<\/p>\n\n\n\n<p>Similarly, creating a matrix filled with columns:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>M2 &lt;- matrix(c(1:9), nrow = 3, ncol = 3, byrow = FALSE)<\/p>\n\n\n\n<p>print(M2)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>css<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;[,1] [,2] [,3]<\/p>\n\n\n\n<p>[1,]&nbsp; &nbsp; 1&nbsp; &nbsp; 4&nbsp; &nbsp; 7<\/p>\n\n\n\n<p>[2,]&nbsp; &nbsp; 2&nbsp; &nbsp; 5&nbsp; &nbsp; 8<\/p>\n\n\n\n<p>[3,]&nbsp; &nbsp; 3&nbsp; &nbsp; 6&nbsp; &nbsp; 9<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding Row and Column Names<\/strong><\/h3>\n\n\n\n<p>You can name the rows and columns by passing a list to the dimnames argument:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>rownames &lt;- c(&#8220;row1&#8221;, &#8220;row2&#8221;, &#8220;row3&#8221;)<\/p>\n\n\n\n<p>colnames &lt;- c(&#8220;col1&#8221;, &#8220;col2&#8221;, &#8220;col3&#8221;)<\/p>\n\n\n\n<p>M3 &lt;- matrix(c(1:9), nrow = 3, byrow = TRUE, dimnames = list(rownames, colnames))<\/p>\n\n\n\n<p>print(M3)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>markdown<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;col1 col2 col3<\/p>\n\n\n\n<p>row1&nbsp; &nbsp; 1&nbsp; &nbsp; 2&nbsp; &nbsp; 3<\/p>\n\n\n\n<p>row2&nbsp; &nbsp; 4&nbsp; &nbsp; 5&nbsp; &nbsp; 6<\/p>\n\n\n\n<p>row3&nbsp; &nbsp; 7&nbsp; &nbsp; 8&nbsp; &nbsp; 9<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing Matrix Elements<\/strong><\/h3>\n\n\n\n<p>To access elements of a matrix, you specify the row and column indices within square brackets. The syntax is:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>matrixName[row, column]<\/p>\n\n\n\n<p>For example, using matrix M3:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(M3[1, 1])&nbsp; # First row, first column<\/p>\n\n\n\n<p>print(M3[3, 3])&nbsp; # Third row, third column<\/p>\n\n\n\n<p>print(M3[2, 3])&nbsp; # Second row, third column<\/p>\n\n\n\n<p>The output will be:<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>[1] 1<\/p>\n\n\n\n<p>[1] 9<\/p>\n\n\n\n<p>[1] 6<\/p>\n\n\n\n<p>You can also extract entire rows or columns by leaving the other index blank:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(M3[1, ])&nbsp; # All elements in first row<\/p>\n\n\n\n<p>print(M3[, 2])&nbsp; # All elements in second column<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Factors in R<\/strong><\/h2>\n\n\n\n<p>Factors are a special data structure used for fields that take a limited number of unique values, often called categorical data. Factors are useful in statistical modeling and data analysis because they represent categories and their levels efficiently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Factors<\/strong><\/h3>\n\n\n\n<p>You create factors from vectors using the factor() function. For example:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>data &lt;- c(&#8220;Male&#8221;, &#8220;Female&#8221;, &#8220;Male&#8221;, &#8220;Child&#8221;, &#8220;Child&#8221;, &#8220;Male&#8221;, &#8220;Female&#8221;, &#8220;Female&#8221;)<\/p>\n\n\n\n<p>print(data)<\/p>\n\n\n\n<p>factor.data &lt;- factor(data)<\/p>\n\n\n\n<p>print(factor.data)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>[1] &#8220;Male&#8221; &nbsp; &#8220;Female&#8221; &#8220;Male&#8221; &nbsp; &#8220;Child&#8221;&nbsp; &#8220;Child&#8221;&nbsp; &#8220;Male&#8221; &nbsp; &#8220;Female&#8221; &#8220;Female&#8221;<\/p>\n\n\n\n<p>Levels: Child, Female, Male<\/p>\n\n\n\n<p>The unique values in the vector become the factor levels.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using Factors in Data Frames<\/strong><\/h3>\n\n\n\n<p>When a data frame contains text columns, R often treats them as factors by default. For instance, consider a data frame, emp. finalda, ta with a column,umn empdept:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(is.factor(emp.finaldata$empdept))<\/p>\n\n\n\n<p>print(emp.finaldata$empdept)<\/p>\n\n\n\n<p>Output might be:<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>[1] TRUE<\/p>\n\n\n\n<p>[1] Sales &nbsp; &nbsp; Marketing HR R&amp;D IT&nbsp; &nbsp; &nbsp; &nbsp; Operations Finance&nbsp;&nbsp;<\/p>\n\n\n\n<p>Levels: HR, Marketing, R&amp;D, Sales, Finance, IT Operations<\/p>\n\n\n\n<p>This shows that the empdept column is treated as a factor, which can be beneficial for grouping and analysis.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Data Frames in R Programming<\/strong><\/h2>\n\n\n\n<p>Data frames are one of the most widely used data structures in R. They are essentially tables or 2D data structures where each column can be of a different data type (numeric, character, factor, etc.). Data frames are used to store tabular data similar to spreadsheets or SQL tables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating a Data Frame<\/strong><\/h3>\n\n\n\n<p>You can create a data frame using the data.frame() function:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>empdata &lt;- data.frame(<\/p>\n\n\n\n<p>&nbsp;&nbsp;empid = c(1001, 1002, 1003, 1004),<\/p>\n\n\n\n<p>&nbsp;&nbsp;empname = c(&#8220;Alice&#8221;, &#8220;Bob&#8221;, &#8220;Charlie&#8221;, &#8220;David&#8221;),<\/p>\n\n\n\n<p>&nbsp;&nbsp;empdept = c(&#8220;Sales&#8221;, &#8220;Marketing&#8221;, &#8220;HR&#8221;, &#8220;IT&#8221;),<\/p>\n\n\n\n<p>&nbsp;&nbsp;empsalary = c(50000, 55000, 45000, 60000)<\/p>\n\n\n\n<p>)<\/p>\n\n\n\n<p>print(empdata)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>yaml<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>&nbsp;empid&nbsp; empname&nbsp; empdept&nbsp; &nbsp; empsalary<\/p>\n\n\n\n<p>1&nbsp; 1001&nbsp; &nbsp; Alice&nbsp; &nbsp; Sales &nbsp; &nbsp; &nbsp; 50000<\/p>\n\n\n\n<p>2&nbsp; 1002&nbsp; &nbsp; &nbsp; Bob Marketing &nbsp; &nbsp; &nbsp; 55000<\/p>\n\n\n\n<p>3&nbsp; 1003&nbsp; Charlie &nbsp; &nbsp; &nbsp; HR &nbsp; &nbsp; &nbsp; 45000<\/p>\n\n\n\n<p>4&nbsp; 1004&nbsp; &nbsp; David &nbsp; &nbsp; &nbsp; IT &nbsp; &nbsp; &nbsp; 60000<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing Data Frame Elements<\/strong><\/h3>\n\n\n\n<p>You can access elements of a data frame in several ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>By column name:<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(empdata$empname)&nbsp; # Prints the empname column<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>By row and column indices:<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(empdata[2, 3])&nbsp; # Row 2, column 3: &#8220;Marketing&#8221;<\/p>\n\n\n\n<p>print(empdata[ , 2])&nbsp; # All rows, column 2 (empname)<\/p>\n\n\n\n<p>print(empdata[1, ]) &nbsp; # Row 1, all columns<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Adding Rows and Columns<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To add a new column:<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>empdata$empbonus &lt;- c(5000, 4000, 3000, 6000)<\/p>\n\n\n\n<p>print(empdata)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To add a new row (using rbind()):<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>newrow &lt;- data.frame(empid=1005, empname=&#8221;Eve&#8221;, empdept=&#8221;Finance&#8221;, empsalary=52000, empbonus=4500)<\/p>\n\n\n\n<p>empdata &lt;- rbind(empdata, newrow)<\/p>\n\n\n\n<p>print(empdata)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Arrays in R Programming<\/strong><\/h2>\n\n\n\n<p>Arrays are similar to matrices but can have more than two dimensions. They hold elements of the same data type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Arrays<\/strong><\/h3>\n\n\n\n<p>You use the array() function to create an array by specifying the data and dimensions:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p># Create a 3D array with dimensions 2 x 3 x 4<\/p>\n\n\n\n<p>arr &lt;- array(1:24, dim = c(2, 3, 4))<\/p>\n\n\n\n<p>print(arr)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing Array Elements<\/strong><\/h3>\n\n\n\n<p>You specify indices for each dimension in square brackets:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(arr[1, 2, 3])&nbsp; # Element at 1st row, 2nd column, 3rd matrix<\/p>\n\n\n\n<p>print(arr[, , 2])&nbsp; &nbsp; # The entire 2nd matrix<\/p>\n\n\n\n<p>Arrays are useful for working with multi-dimensional data, like image processing, scientific datasets, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Handling Missing Data in R<\/strong><\/h2>\n\n\n\n<p>Missing data is common in real-world datasets, and R provides ways to detect and manage missing values.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Representing Missing Data<\/strong><\/h3>\n\n\n\n<p>In R, missing data is represented by the special value NA.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Detecting Missing Values<\/strong><\/h3>\n\n\n\n<p>Use the is.na() function to check for missing values:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>data &lt;- c(10, 20, NA, 40, NA)<\/p>\n\n\n\n<p>print(is.na(data))<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>graphql<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>[1] FALSE FALSE&nbsp; TRUE FALSE&nbsp; TRUE<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Removing Missing Values<\/strong><\/h3>\n\n\n\n<p>You can remove missing values using the na.omit() function or by using logical indexing:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>clean_data &lt;- na.omit(data)<\/p>\n\n\n\n<p>print(clean_data)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>[1] 10 20 40<\/p>\n\n\n\n<p>Alternatively:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>clean_data &lt;- data[!is.na(data)]<\/p>\n\n\n\n<p>print(clean_data)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Replacing Missing Values<\/strong><\/h3>\n\n\n\n<p>You can replace missing values with a specific value:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>data[is.na(data)] &lt;- 0<\/p>\n\n\n\n<p>print(data)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>[1] 10 20&nbsp; 0 40&nbsp; 0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Lists in R Programming<\/strong><\/h2>\n\n\n\n<p>Lists are very flexible data structures in R. Unlike vectors or arrays, lists can hold elements of different types and structures \u2014 including vectors, matrices, other lists, data frames, and even functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Lists<\/strong><\/h3>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>mylist &lt;- list(<\/p>\n\n\n\n<p>&nbsp;&nbsp;name = &#8220;John&#8221;,<\/p>\n\n\n\n<p>&nbsp;&nbsp;age = 28,<\/p>\n\n\n\n<p>&nbsp;&nbsp;scores = c(85, 90, 88),<\/p>\n\n\n\n<p>&nbsp;&nbsp;passed = TRUE<\/p>\n\n\n\n<p>)<\/p>\n\n\n\n<p>print(mylist)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing List Elements<\/strong><\/h3>\n\n\n\n<p>You can access list elements by name or position:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(mylist$name)&nbsp; &nbsp; # &#8220;John&#8221;<\/p>\n\n\n\n<p>print(mylist[[2]])&nbsp; &nbsp; # 28 (age)<\/p>\n\n\n\n<p>print(mylist$scores)&nbsp; # c(85, 90, 88)<\/p>\n\n\n\n<p>To access an element inside a list element:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>print(mylist$scores[2])&nbsp; # 90<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Factors in R Programming<\/strong><\/h2>\n\n\n\n<p>Factors are used to handle categorical data \u2014 data that has a fixed number of possible values (levels). They are stored as integers with labels.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Creating Factors<\/strong><\/h3>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>colors &lt;- c(&#8220;red&#8221;, &#8220;blue&#8221;, &#8220;red&#8221;, &#8220;green&#8221;, &#8220;blue&#8221;, &#8220;blue&#8221;)<\/p>\n\n\n\n<p>color_factor &lt;- factor(colors)<\/p>\n\n\n\n<p>print(color_factor)<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>[1] red &nbsp; blue&nbsp; red &nbsp; green blue&nbsp; blue&nbsp;<\/p>\n\n\n\n<p>Levels: blue, green, red<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Why Factors?<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Factors help with statistical modeling.<br><\/li>\n\n\n\n<li>They save memory compared to storing character vectors.<br><\/li>\n\n\n\n<li>Levels can be ordered or unordered.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Accessing Levels<\/strong><\/h3>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>levels(color_factor)&nbsp; # &#8220;blue&#8221; &#8220;green&#8221; &#8220;red&#8221;<\/p>\n\n\n\n<p>You can specify the order of levels:<\/p>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>ordered_factor &lt;- factor(colors, levels = c(&#8220;red&#8221;, &#8220;green&#8221;, &#8220;blue&#8221;), ordered = TRUE)<\/p>\n\n\n\n<p>print(ordered_factor)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Basic Data Manipulation with dplyr<\/strong><\/h2>\n\n\n\n<p>dplyr is a package that makes data manipulation easy and intuitive.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Installing and Loading dplyr<\/strong><\/h3>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>install.packages(&#8220;dplyr&#8221;)&nbsp; # Run once<\/p>\n\n\n\n<p>library(dplyr)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Sample Data Frame<\/strong><\/h3>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>df &lt;- data.frame(<\/p>\n\n\n\n<p>&nbsp;&nbsp;name = c(&#8220;Alice&#8221;, &#8220;Bob&#8221;, &#8220;Charlie&#8221;, &#8220;David&#8221;, &#8220;Eve&#8221;),<\/p>\n\n\n\n<p>&nbsp;&nbsp;age = c(25, 30, 35, 40, 28),<\/p>\n\n\n\n<p>&nbsp;&nbsp;salary = c(50000, 60000, 55000, 65000, 48000)<\/p>\n\n\n\n<p>)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Common dplyr Functions<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>filter() \u2014 Filter rows based on condition:<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>young_employees &lt;- filter(df, age &lt; 30)<\/p>\n\n\n\n<p>print(young_employees)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Select () \u2014 Select specific columns:<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>selected_data &lt;- select(df, name, salary)<\/p>\n\n\n\n<p>print(selected_data)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mutate () \u2014 Add new columns or modify existing ones:<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>df &lt;- mutate(df, bonus = salary * 0.1)<\/p>\n\n\n\n<p>print(df)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Arrange e() \u2014 Sort rows:<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>df_sorted &lt;- arrange(df, desc(salary))<\/p>\n\n\n\n<p>print(df_sorted)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Summarize () \u2014 Summarize data (usually with grouping):<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>avg_salary &lt;- summarize(df, avg_salary = mean(salary))<\/p>\n\n\n\n<p>print(avg_salary)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>group_by() \u2014 Group data before summarizing:<br><\/li>\n<\/ul>\n\n\n\n<p>r<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>df2 &lt;- data.frame(<\/p>\n\n\n\n<p>&nbsp;&nbsp;dept = c(&#8220;Sales&#8221;, &#8220;Sales&#8221;, &#8220;HR&#8221;, &#8220;HR&#8221;, &#8220;IT&#8221;),<\/p>\n\n\n\n<p>&nbsp;&nbsp;salary = c(50000, 55000, 45000, 47000, 60000)<\/p>\n\n\n\n<p>)<\/p>\n\n\n\n<p>dept_avg &lt;- df2 %&gt;%<\/p>\n\n\n\n<p>&nbsp;&nbsp;group_by(dept) %&gt;%<\/p>\n\n\n\n<p>&nbsp;&nbsp;summarize(avg_salary = mean(salary))<\/p>\n\n\n\n<p>print(dept_avg)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Final Thoughts&nbsp;<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Practice is Key<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>R is best learned by doing. Try solving real problems or working on small projects.<br><\/li>\n\n\n\n<li>Use datasets from sources like Kaggle or R\u2019s built-in datasets (mtcars, iris).<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. Understand the Ecosystem<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>R is much more than base functions. Explore popular packages like:<br>\n<ul class=\"wp-block-list\">\n<li>tidyverse (includes dplyr, ggplot2, tidyr, etc.) for data manipulation and visualization.<br><\/li>\n\n\n\n<li>Data. Table for fast data handling.<br><\/li>\n\n\n\n<li>Shiny is for interactive web apps.<br><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Learn to Debug and Read Error Messages<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Errors are part of coding. Carefully read error messages\u2014they often tell you exactly what\u2019s wrong.<br><\/li>\n\n\n\n<li>Use traceback(), debug(), and browser() functions to troubleshoot.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. Write Clean and Reproducible Code<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Comment your code.<br><\/li>\n\n\n\n<li>Use meaningful variable names.<br><\/li>\n\n\n\n<li>Organize your scripts logically.<br><\/li>\n\n\n\n<li>Consider using R Markdown for combining code, output, and explanations in one document.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. Keep Exploring Statistical and Visualization Tools<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>R\u2019s strength is in statistics and graphics.<br><\/li>\n\n\n\n<li>Learn to use ggplot2 for advanced plotting.<br><\/li>\n\n\n\n<li>Explore modeling functions like lm(), glm(), or machine learning packages.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>6. Join the Community<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>R has a welcoming and active community.<br><\/li>\n\n\n\n<li>Participate in forums like<a href=\"https:\/\/stackoverflow.com\/questions\/tagged\/r\"> Stack Overflow<\/a>, RStudio Community, or local user groups.<br><\/li>\n\n\n\n<li>Follow blogs, Twitter accounts, or YouTube channels about R.<br><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Data structures are fundamental for organizing and managing data efficiently in any programming language. Essentially, a data structure is a method to arrange data within a system to facilitate effective access and modification. The main goal is to minimize complexities related to both space and time during various computational tasks. When working with programming languages [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-895","post","type-post","status-publish","format-standard","hentry","category-posts"],"_links":{"self":[{"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/posts\/895"}],"collection":[{"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/comments?post=895"}],"version-history":[{"count":1,"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/posts\/895\/revisions"}],"predecessor-version":[{"id":914,"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/posts\/895\/revisions\/914"}],"wp:attachment":[{"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/media?parent=895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/categories?post=895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.actualtests.com\/blog\/wp-json\/wp\/v2\/tags?post=895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}