Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
R Programming Articles
Page 55 of 174
How to find the mean of list elements without unlisting them in R?
Most of the times, unlisting is used to find the mean of list elements but we can also use double-square brackets for the same purpose. The double-square brackets are basically used to access the values in the elements of the list, thus mean function works with those values directly. Look at the below example to understand how it works.ExampleConsider the below list:> x xOutput[1] 3 3 3 5 3 1 4 7 5 4 5 9 9 7 4 3 6 2 4 3 3 4 7 4 4 [26] 4 5 3 4 4 3 5 7 2 3 ...
Read MoreHow to create frequency table of a string vector in R?
To create a frequency table of a string vector, we just need to use table function. For example, if we have a vector x that contains randomly sampled 100 values of first five English alphabets then the table of vector x can be created by using table(x). This will generate a table along with the name of the vector.Example1> x1 x1Output[1] "d" "d" "a" "c" "a" "a" "c" "a" "d" "c" "a" "d" "d" "b" "c" "a" "b" "c" "d" [20] "b"Example> table(x1)Outputx1 a b c d 6 3 5 6Example2> x2 x2Output[1] "w" "j" "p" "y" "r" "m" "y" ...
Read MoreHow to find the sum of two list elements in R?
The list elements of two lists cannot be added directly but we can do this addition by unlisting the elements. To do this, we would need to use lapply function. For example, if we have two lists defined as x and y then the sum of the elements in these lists can be calculated as follows:Examplelapply(seq_along(x), function(i) unlist(x[i])+unlist(y[i]))Example1> x1 x1Output[[1]] [1] 0 3 0 1 2 0 1 0 1 3 3 0 0 0 1 1 0 1 0 1 1 0 2 0 0 6 1 2 1 1 1 1 2 1 1 0 0 [38] 2 ...
Read MoreHow to create a replicated list of a list in R?
Sometimes we want to created repeated values, this is helpful in different scenarios such as measuring an effect of a constant on multiple variables. The list values can be also replicated for similar purpose of analysis. The replication of list of a list can be created by using rep function. For example, if we have a list called x and we want to create five times replicated list of this list then we can use the code rep(list(x), 5).Example1> List1 List1Output$x1 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
Read MoreHow to convert a date or date vector to POSIXct in R?
To convert a date or date vector to POSIXct, we can use as.POSIXct function but we also need to pass the appropriate date format inside the function. For example, if we have a date "2020-11-14" then it can be converted to POSIXct by using as.POSIXct("2020-11-14", format="%Y-%m-%d").Example1> date1 as.POSIXct(date1, format="%Y-%m-%d")Output[1] "2020-04-01 IST"Example2> date2 date2Output[1] "2020-02-12" "2020-06-01" "2020-04-01" "2020-05-01" "2020-01-21" [6] "2020-01-21" "2020-06-01" "2020-04-27" "2020-05-11" "2020-06-01" [11] "2020-01-21" "2020-03-31" "2020-05-01" "2020-02-12" "2020-01-21" [16] "2020-05-01" "2020-03-31" "2020-04-01" "2020-05-01" "2020-01-21" [21] "2020-05-01" "2020-04-11" "2020-05-11" "2020-04-01" "2020-03-31" [26] "2020-04-11" "2020-04-01" "2020-03-31" "2020-04-01" "2020-04-11" [31] "2020-05-11" "2020-06-01" "2020-03-31" "2020-04-27" "2020-01-21" [36] "2020-01-21" "2020-04-01" "2020-06-01" "2020-05-01" "2020-10-01" ...
Read MoreHow to replace a sub-string with the reverse of that sub-string in R?
The chartr function in base R helps us to replace old strings with new strings and hence it can be also used to replace a subs-string with the reverse of that substring. For example, if we have a vector say x that contains tutorialpsoint and we want to convert it to tutorialspoint then it can be done as chartr("tutorialpsoint ", " tutorialspoint ", x).Example1> x1 x1Output[1] "IDNIA"Example> chartr("DN", "ND", x1)Output[1] "INDIA" Example2> x2 x2Output[1] "IDNIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNONESIA" [7] "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" "IDNONESIA" [13] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" [19] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" ...
Read MoreHow to find the range of a vector in R?
The range function in R provides the minimum and maximum values instead of the difference between the two. Hence, we can find the minimum and maximum by using range function then diff function can be used to find the actual range. For example, if we have a vector x then the range can be found by using diff(range(x)).Example> x1 x1Output[1] 4 2 3 0 2 3 1 3 4 2Example> diff(range(x1))Output[1] 4 Example> x2 x2Output[1] 4 5 3 10 2 4 2 4 8 7 3 1 5 6 7 3 7 3 4 5 3 7 2 7 ...
Read MoreHow to reduce a matrix in R to echelon form?
The echelon form of a matrix is the matrix that has the following characteristics:1. The first non-zero element in each row, called the leading entry, is 1.2. Each leading entry is in a column to the right of the leading entry in the previous row.3. Rows with all zero elements, if any, are below rows having a non-zero element.In R, we can use echelon function of matlib package to find the echelon form of the matrix.Example> M MOutput [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 8 11 3 10 13 [2, ] 9 ...
Read MoreHow to convert a string vector into an integer vector in R?
A string vector contains element inside double-quotes and an integer vector does not have any quotes. Sometimes integer values are stored in double-quotes hence the vector of these values is treated as a string vector in R but we need the integer values to perform mathematical operations. Therefore, we can use as.integer function to convert the string vector into an integer vector.Example1> x1 x1Output[1] "3" "2" "1" "2" "1" "1" "1" "1" "1" "1" "3" "3" "3" "1" "2" "1" "1" "2" [19] "2" "3" "3" "3" "3" "2" "3" "3" "3" "2" "1" "2" "3" "3" "2" "1" "2" "2" [37] "3" "3" "3" "2" "3" "2" "2" "1" "3" "3" "2" "2" "2" "1" "2" "3" "1" "3" [55] "3" "2" "1" "2" "2" "1" "2" "1" "1" "2" "2" "2" "3" "1" "3" "3" "1" "3" [73] "1" "1" "2" "2" "1" "3" "2" "3" "2" "2" "2" "2" "1" "3" "2" "1" "3" "3" [91] "3" "3" "1" "1" "1" "2" "2" "2" "2" "3" "1" "2" "2" "1" "3" "2" "2" "2" [109] "1" "1" "2" "3" "2" "2" "1" "1" "2" "2" "3" "2" "3" "2" "3" "2" "3" "2" [127] "2" "2" "2" "1" "1" "2" "1" "2" "2" "3" "3" "2" "2" "2" "3" "3" "2" "2" [145] "3" "2" "2" "3" "2" "3"Example> x1 x1Output[1] 3 2 1 2 1 1 1 1 1 1 3 3 3 1 2 1 1 2 2 3 3 3 3 2 3 3 3 2 1 2 3 3 2 1 2 2 3 [38] 3 3 2 3 2 2 1 3 3 2 2 2 1 2 3 1 3 3 2 1 2 2 1 2 1 1 2 2 2 3 1 3 3 1 3 1 1 [75] 2 2 1 3 2 3 2 2 2 2 1 3 2 1 3 3 3 3 1 1 1 2 2 2 2 3 1 2 2 1 3 2 2 2 1 1 2 [112] 3 2 2 1 1 2 2 3 2 3 2 3 2 3 2 2 2 2 1 1 2 1 2 2 3 3 2 2 2 3 3 2 2 3 2 2 3 [149] 2 3Example2> x2 x2Output[1] "19" "1" "19" "1" "13" "7" "11" "1" "13" "3" "19" "7" "3" "11" "7" [16] "3" "5" "1" "3" "11" "3" "2" "3" "5" "7" "7" "19" "7" "11" "7" [31] "7" "5" "17" "11" "7" "17" "2" "5" "5" "5" "5" "1" "13" "13" "5" [46] "19" "1" "13" "3" "3" "3" "19" "7" "7" "2" "3" "5" "1" "2" "5" [61] "3" "17" "11" "1" "13" "1" "1" "19" "17" "2" "17" "17" "11" "17" "13" [76] "2" "5" "2" "1" "17" "5" "5" "1" "13" "2" "13" "2" "2" "13" "19" [91] "3" "2" "1" "2" "11" "11" "13" "17" "19" "11" "19" "11" "1" "5" "19" [106] "7" "13" "19" "13" "11" "17" "11" "19" "2" "7" "19" "5" "17" "17" "5" [121] "1" "1" "7" "5" "11" "5" "7" "17" "13" "5" "1" "17" "13" "3" "1" [136] "17" "5" "5" "1" "2" "19" "11" "11" "7" "1" "5" "7" "13" "3" "2" [151] "2" "5" "17" "2" "7" "19" "19" "19" "7" "3"Example> x2 x2Output[1] 19 1 19 1 13 7 11 1 13 3 19 7 3 11 7 3 5 1 3 11 3 2 3 5 7 [26] 7 19 7 11 7 7 5 17 11 7 17 2 5 5 5 5 1 13 13 5 19 1 13 3 3 [51] 3 19 7 7 2 3 5 1 2 5 3 17 11 1 13 1 1 19 17 2 17 17 11 17 13 [76] 2 5 2 1 17 5 5 1 13 2 13 2 2 13 19 3 2 1 2 11 11 13 17 19 11 [101] 19 11 1 5 19 7 13 19 13 11 17 11 19 2 7 19 5 17 17 5 1 1 7 5 11 [126] 5 7 17 13 5 1 17 13 3 1 17 5 5 1 2 19 11 11 7 1 5 7 13 3 2 [151] 2 5 17 2 7 19 19 19 7 3Example3> x3 x3Output[1] "5" "10" "20" "15" "5" "15" "5" "5" "20" "20" "10" "10" "5" "5" "5" [16] "15" "5" "5" "15" "10" "10" "20" "20" "10" "20" "10" "5" "5" "15" "15" [31] "15" "15" "5" "10" "20" "15" "20" "5" "15" "20" "5" "20" "5" "20" "20" [46] "15" "15" "20" "5" "5" "10" "15" "15" "20" "20" "5" "5" "15" "20" "20" [61] "10" "10" "15" "10" "20" "5" "5" "15" "20" "5" "20" "20" "20" "5" "20" [76] "20" "15" "15" "15" "20" "10" "10" "15" "10" "10" "5" "5" "20" "20" "5" [91] "5" "10" "15" "15" "15" "10" "15" "20" "10" "20" "5" "10" "10" "15" "15" [106] "5" "15" "15" "10" "10" "20" "5" "20" "15" "10" "15" "15" "20" "20" "15" [121] "20" "20" "5" "5" "5" "5" "10" "20" "20" "10" "20" "5" "5" "20" "10" [136] "5" "5" "15" "10" "15" "10" "20" "20" "10" "20" "10" "20" "10" "15" "5" [151] "20" "20" "20" "15" "10" "20" "20" "10" "20" "20"Example> x3 x3Output[1] 5 10 20 15 5 15 5 5 20 20 10 10 5 5 5 15 5 5 15 10 10 20 20 10 20 [26] 10 5 5 15 15 15 15 5 10 20 15 20 5 15 20 5 20 5 20 20 15 15 20 5 5 [51] 10 15 15 20 20 5 5 15 20 20 10 10 15 10 20 5 5 15 20 5 20 20 20 5 20 [76] 20 15 15 15 20 10 10 15 10 10 5 5 20 20 5 5 10 15 15 15 10 15 20 10 20 [101] 5 10 10 15 15 5 15 15 10 10 20 5 20 15 10 15 15 20 20 15 20 20 5 5 5 [126] 5 10 20 20 10 20 5 5 20 10 5 5 15 10 15 10 20 20 10 20 10 20 10 15 5 [151] 20 20 20 15 10 20 20 10 20 20Example4> x4 x4Output[1] "501" "515" "515" "501" "515" "525" "501" "515" "515" "520" "525" "520" [13] "515" "501" "501" "525" "520" "515" "525" "525" "525" "525" "515" "515" [25] "515" "525" "520" "520" "525" "501" "520" "525" "520" "520" "501" "515" [37] "525" "520" "501" "501" "515" "520" "515" "520" "520" "520" "515" "501" [49] "515" "501" "520" "501" "525" "501" "501" "501" "525" "520" "520" "525" [61] "520" "501" "525" "520" "515" "520" "520" "525" "515" "515" "520" "520" [73] "520" "515" "515" "501" "525" "525" "501" "515" "525" "520" "515" "520" [85] "525" "525" "501" "501" "525" "515" "501" "525" "520" "501" "501" "501" [97] "501" "525" "501" "520" "520" "515" "501" "515" "515" "501" "520" "501" [109] "525" "525" "520" "515" "501" "520" "520" "515" "515" "501" "501" "520" [121] "515" "525" "501" "515" "501" "515" "515" "501" "520" "515" "501" "520" [133] "515" "520" "520" "515" "525" "515" "525" "515" "525" "520" "520" "515" [145] "515" "520" "501" "515" "525" "520"Example> x4 x4Output[1] 501 515 515 501 515 525 501 515 515 520 525 520 515 501 501 525 520 515 [19] 525 525 525 525 515 515 515 525 520 520 525 501 520 525 520 520 501 515 [37] 525 520 501 501 515 520 515 520 520 520 515 501 515 501 520 501 525 501 [55] 501 501 525 520 520 525 520 501 525 520 515 520 520 525 515 515 520 520 [73] 520 515 515 501 525 525 501 515 525 520 515 520 525 525 501 501 525 515 [91] 501 525 520 501 501 501 501 525 501 520 520 515 501 515 515 501 520 501 [109] 525 525 520 515 501 520 520 515 515 501 501 520 515 525 501 515 501 515 [127] 515 501 520 515 501 520 515 520 520 515 525 515 525 515 525 520 520 515 [145] 515 520 501 515 525 520
Read MoreHow to find the sum of diagonal elements in a table in R?
The sum of diagonal elements could be required in matrix analysis therefore, we can convert the matrix into a table and find the sum of diagonal elements. This can be easily done by using sun function by extracting diagonal elements of the table using diag function. For example, if we have a table T then the sum of diagonal elements of T can be found as sum(diag(T)).ExampleTable1
Read More