Nizamuddin Siddiqui

Nizamuddin Siddiqui

1,958 Articles Published

Articles by Nizamuddin Siddiqui

Page 66 of 196

How to find the mean of a square matrix elements by excluding diagonal elements in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 306 Views

There are many ways to find the mean of a matrix elements by excluding diagonal elements, this mean is actually the mean of lower triangular matrix and the upper triangular matrix. We can simply use mean function by creating a vector of lower and upper triangular matrix as shown in the below examples.Example1> M1 M1Output   [, 1] [, 2] [, 3] [, 4] [1, ] 1    6    3    6 [2, ] 8    5    3    4 [3, ] 5    4    4    6 [4, ] 5    5    3    4 [5, ...

Read More

How to convert a string vector into an integer vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

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 More

How to create a replicated list of a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

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 More

How to find the column means of a column based on another column values that represent factor in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

If we have a column that represent factor then we might want to find the mean of values in other column(s) for the factor levels. This is helpful in comparing the levels of the factor. In R, we can find the mean for such type of data by using aggregate function. Check out the below examples to understand how it can be done.Example1Consider the below data frame:> x1 y1 df1 df1Output x1 y1 1 D 5.801197 2 B 3.432060 3 B 6.154168 4 A 5.466655 5 D 5.171689 6 C 5.175170 7 B 5.353469 8 D 4.840470 ...

Read More

How to convert a date or date vector to POSIXct in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

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 More

How to check if some specific columns of an R data frame are equal to a column or not?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 446 Views

If we have a large amount of data in a data frame and we suspect that some of the data columns are repeated or some of them are equal to a particular column then we can use sapply function in base R to figure it out. In this way, we can remove duplicated columns that does not suppose to help in our data analysis objective.Example1Consider the below data frame:> set.seed(354) > x1 x2 x3 x4 x5 df1 df1Outputx1 x2 x3 x4 x5 1 4 5 4 4 6 2 6 4 8 7 5 3 5 6 4 7 6 ...

Read More

How to create smooth density curves without filling densities in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 566 Views

The density curves can be created by using stat_density function of ggplot2 package but it fills the curve with density hence it becomes difficult to recognize the curves. We can remove these densities by using geom="line" inside the stat_density function so that only the density curves will be plotted.ExampleConsider the below data frame:> G Response df dfOutputG Response 1 C 1.0229016 2 C 1.0058160 3 B 0.8831558 4 B 0.7729167 5 C 0.9130468 6 D 0.8431893 7 B 1.5003581 8 A 0.9687335 9 B 1.1139661 10 A 0.9211660 11 A 1.1790619 12 D 0.6349671 13 A 1.2616918 14 A 1.6021078 ...

Read More

How to subset rows of an R data frame using grepl function?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 5K+ Views

The grepl function in R search for matches to argument pattern within each element of a character vector or column of an R data frame. If we want to subset rows of an R data frame using grepl then subsetting with single-square brackets and grepl can be used by accessing the column that contains character values.Example1Consider the below data frame:> x1 y1 z1 df1 df1Output x1 y1 z1 1 A 0.8833979 5 2 B 0.5400075 1 3 C 0.6923827 3 4 B 1.5069186 2 5 B 0.8190962 2 6 B 0.8296171 1 7 B ...

Read More

How to find the group-wise correlation coefficient in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 973 Views

If we have two continuous and one categorical column in an R data frame then we can find the correlation coefficient between continuous values for the categories in the categorical column. For this purpose, we can use by function and pass the cor function with the spearman method as shown in the below examples.Example1Consider the below data frame:> x1 y1 z1 df1 df1Output x1 y1 z1 1 A 1.1155324 2 2 C 0.9801564 3 3 B 0.9116162 1 4 A 0.8406772 3 5 C 0.8009355 2 6 A 0.9331637 2 7 B 1.0642089 1 ...

Read More

How to convert a matrix into a matrix with single column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

If we have a matrix then we might want to convert it to matrix with single column for some analytical purpose such as multiplying with a vector that has the length equal to the total number of elements as in the matrix. Thus, the matrix can be converted to a single column matrix by using matrix function itself but for this we would need to nullify the column names and row names.Example1> M1 M1Output [, 1] [, 2] [, 3] ...

Read More
Showing 651–660 of 1,958 articles
« Prev 1 64 65 66 67 68 196 Next »
Advertisements