Multiply Matrix Columns and Rows with Same Matrix in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:33:00

2K+ Views

To multiply a rows or columns of a matrix, we need to use %*% symbol that perform the multiplication for matrices in R. If we have a matrix M with 5 rows and 5 columns then row 1 of M can be multiplied with column 1 of M using M[1,]%*%M[,1], similarly, we can multiply other rows and columns.Example Live DemoM

Create a Dashed Line Passing Through y=1 in Base R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:31:36

2K+ Views

Usually, plots are created with solid lines but sometimes we need to use dashed line so that the points can represent a threshold or something critical, the main objective here is to get the attention of the reader on these points. In base R, the plots are created with plot function and we can use abline function with lty =2 to draw dashed lines.Example1 Live Demox

Create Dendrogram without X-Axis Labels in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:27:49

587 Views

A dendrogram display the hierarchical relationship between objects and it is created by using hierarchical clustering. In base R, we can use hclust function to create the clusters and the plot function can be used to create the dendrogram. For example, if we want to create the dendrogram for mtcars data without X−axis labels then it can be done as shown below −hc=hclust(dist(mtcars)) plot(hc, xlab="", sub="")Example Live Demohead(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 ... Read More

Create Random Sample of Values Between 0 and 1 in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:23:52

4K+ Views

The continuous uniform distribution can take values between 0 and 1 in R if the range is not defined. To create a random sample of continuous uniform distribution we can use runif function, if we will not pass the minimum and maximum values the default will be 0 and 1 and we can also use different range of values.Examplesrunif(5) [1] 0.8667731 0.7109824 0.4466423 0.1644701 0.5611908 runif(10) [1] 0.5923782 0.8793613 0.6912947 0.2963916 0.6076762 0.7683766 0.1143595 [8] 0.4782710 0.1143383 0.4540217 runif(50) [1] 0.841674685 0.325249762 0.640847906 0.203868249 0.495230429 0.897175830 [7] 0.744447459 0.490173680 0.254711280 0.144844443 0.867749180 0.004405166 [13] 0.539785687 0.739637398 0.062214554 0.648021581 0.768686809 0.305543906 [19] 0.757496413 0.527085302 0.633331579 0.700118363 0.857950259 0.929350618 [25] 0.167015719 0.775870043 0.430343200 0.528408273 0.600575697 0.612206968 [31] 0.065904791 0.061135682 0.082027863 0.193586800 0.013956337 0.156875620 [37] 0.837501421 0.971202297 0.930835689 0.292126061 0.599263353 0.826630821 [43] 0.509235736 0.741715013 0.224485511 0.113099235 0.395143355 0.375654137 [49] 0.973050494 0.107550270 round(runif(50),2) [1] 0.51 0.70 0.90 0.45 0.41 0.74 0.31 0.40 0.10 0.05 0.18 0.05 0.63 0.34 0.57 [16] 0.06 0.73 0.37 0.79 0.85 0.82 0.41 0.32 0.34 0.37 0.14 0.21 0.11 0.43 0.86 [31] 0.83 0.09 0.88 0.04 0.62 0.64 0.15 0.75 0.78 0.16 0.67 0.97 0.79 0.64 0.56 [46] 0.40 0.07 0.69 0.82 0.63 round(runif(50),4) [1] 0.2951 0.2916 0.9049 0.2669 0.7613 0.2080 0.4739 0.1110 0.6155 0.5429 [11] 0.4490 0.2941 0.8262 0.7719 0.7896 0.7634 0.6260 0.7812 0.7600 0.6852 [21] 0.9142 0.0165 0.2324 0.0821 0.0814 0.4009 0.3315 0.8843 0.9684 0.1966 [31] 0.4841 0.5795 0.7898 0.1865 0.6929 0.8599 0.0492 0.8275 0.7431 0.3122 [41] 0.8480 0.3327 0.4872 0.0503 0.1887 0.0296 0.6011 0.1162 0.7776 0.6874 round(runif(50),5) [1] 0.40368 0.33585 0.03557 0.06047 0.95041 0.18260 0.70011 0.75148 0.12414 [10] 0.01310 0.42343 0.05846 0.21341 0.05454 0.77823 0.66151 0.61406 0.59459 [19] 0.50299 0.96780 0.43033 0.64652 0.39697 0.05897 0.47169 0.79828 0.74154 [28] 0.56074 0.97303 0.35301 0.36110 0.67452 0.14553 0.45195 0.05780 0.90489 [37] 0.96745 0.28014 0.02089 0.77789 0.04797 0.03550 0.40495 0.08924 0.59908 [46] 0.89074 0.48498 0.47335 0.59422 0.00719 round(runif(100),2) [1] 0.10 0.06 0.51 0.89 0.80 0.68 0.97 0.58 0.60 0.79 0.96 0.48 0.29 0.16 0.42 [16] 0.35 0.46 0.18 0.46 0.34 0.48 0.35 0.72 0.10 0.50 0.93 0.30 0.54 0.85 0.19 [31] 0.12 0.10 0.47 0.66 0.43 0.09 0.44 0.86 0.99 0.31 0.10 0.61 0.20 0.15 0.02 [46] 0.25 0.33 0.75 0.98 0.23 0.21 0.70 0.42 0.24 0.87 0.84 0.99 0.06 0.75 0.48 [61] 0.84 0.35 0.48 0.62 0.40 0.25 0.07 0.08 0.75 0.40 0.83 0.95 0.00 0.87 0.27 [76] 0.53 0.21 0.41 0.28 0.83 0.90 0.26 0.50 0.19 0.70 0.93 0.24 0.45 0.33 0.84 [91] 0.15 0.81 0.62 0.17 0.08 0.76 0.74 0.11 0.20 0.49 round(runif(150),1) [1] 0.6 0.3 0.3 0.3 0.9 0.7 0.1 0.1 0.1 0.9 0.4 0.6 1.0 0.0 0.4 1.0 0.1 1.0 [19] 0.8 0.0 0.9 0.9 0.7 0.7 0.7 0.7 0.3 0.7 0.1 0.1 0.9 0.0 0.1 1.0 0.9 1.0 [37] 0.9 0.6 0.0 0.4 0.4 1.0 0.2 0.4 0.2 0.8 0.3 0.9 0.8 0.6 0.3 0.3 0.4 0.7 [55] 0.2 0.9 1.0 0.9 0.8 0.7 0.9 1.0 0.5 0.8 0.6 0.8 0.6 0.8 0.3 0.3 1.0 0.6 [73] 0.9 0.3 0.0 1.0 0.5 0.6 0.7 0.7 0.6 0.3 0.4 0.0 0.3 0.1 0.6 0.2 0.1 0.7 [91] 0.9 0.8 0.3 0.2 0.5 0.6 0.6 0.1 0.0 0.9 0.4 0.6 0.3 0.2 0.9 0.6 0.0 0.2 [109] 0.3 0.3 0.3 0.7 0.4 0.8 0.5 0.9 0.6 0.5 0.3 1.0 0.6 0.7 0.9 0.1 0.8 1.0 [127] 0.3 1.0 0.2 0.9 0.2 0.3 0.5 0.4 0.1 0.6 0.6 0.0 0.3 0.3 0.0 0.3 0.3 1.0 [145] 0.6 0.5 0.1 0.7 0.6 0.4 round(runif(75),1) [1] 0.7 0.3 0.7 0.9 0.8 0.1 0.4 0.2 0.5 0.4 0.1 0.7 0.1 0.6 1.0 0.3 0.4 0.7 0.2 [20] 0.2 0.3 0.4 0.4 0.0 0.1 0.2 0.3 0.5 0.1 1.0 0.3 0.5 0.3 0.7 0.1 0.6 0.6 0.6 [39] 0.5 0.7 0.5 0.8 0.1 1.0 0.7 0.4 0.6 0.1 0.5 0.5 0.9 0.3 0.8 0.9 0.3 0.9 0.7 [58] 0.6 0.8 0.4 0.4 0.7 0.4 0.1 0.2 0.6 0.6 0.9 0.3 0.6 0.5 0.9 0.2 0.3 0.2 round(runif(75),3) [1] 0.712 0.355 0.130 0.768 0.134 0.681 0.273 0.663 0.849 0.851 0.842 0.430 [13] 0.371 0.903 0.148 0.879 0.812 0.330 0.567 0.646 0.199 0.159 0.056 0.448 [25] 0.637 0.204 0.101 0.389 0.797 0.030 0.021 0.167 0.440 0.359 0.670 0.435 [37] 0.807 0.669 0.738 0.546 0.535 0.969 0.055 0.201 0.436 0.336 0.841 0.548 [49] 0.901 0.850 0.369 0.770 0.678 0.922 0.252 0.132 0.635 0.544 0.291 0.715 [61] 0.601 0.399 0.585 0.161 0.423 0.244 0.451 0.397 0.951 0.382 0.123 0.959 [73] 0.252 0.330 0.238

Find Combination of Multiplication of Integers Up to a Certain Value in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:23:03

201 Views

Suppose we want to find the total number of combinations of two numbers, say, 1 and 2 and then multiply each of the combination values. This will result in the following combinations −1 1 1 2 2 1 2 2And the multiplication will also have a third column as shown below −Multiplication 1 1 1 1 2 2 2 1 2 2 2 4Example1 Live Demofor (i in 1:2) for (j in 1:2) cat(i, j, i*j, "") Output1 1 1 1 2 2 2 1 2 2 2 4Example2 Live Demofor (i in 1:5) for (j in 1:5) cat(i, j, i*j, "") ... Read More

Convert MANOVA Data Frame for Two Dependent Variables into Count Table in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:21:54

236 Views

MANOVA refers to multivariate analysis of variance, in this method we have more than one dependent variable and multiple independent variables. We want to compare each level of the independent variable combination for each of the dependent variables. To convert MANOVA data frame for two-dependent variables into a count table, we can use cast function of reshape package but we need to melt the data frame first so that the casting can be done appropriately.Example Live DemoConsider the below data frame −Gender

Replace & in a String with and in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:06:51

184 Views

We know that the word “and” can be written as “&”. If we have vectors that contain string values separated with word “and” then we can replace it with “&”. To do this, we can use stri_replace_last function of stringi package. For example, if we have a string vector that contain only one element defined as x

Create a Random Sample of Months in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:03:02

405 Views

Random samples can be created by using sample function and months in a year can be generated with the help of month.name function. Therefore, if we want to create a random sample of months then month.name can be used with sample function as sample(month.name) and if the size of the sample is larger than 12 then replace=TRUE argument should be used.Examples Live Demox1

Subset Data Frame by Excluding Specific Text Value in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:02:02

817 Views

To create a subset based on text value we can use rowSums function by defining the sums for the text equal to zero, this will help us to drop all the rows that contains that specific text value. For example, if we have a data frame df that contains A in many columns then all the rows of df excluding A can be selected as−df[rowSums(df=="A")==0,,drop=FALSE]Example Live DemoConsider the below data frame −set.seed(951) x1

Highlight Text Inside a Plot in ggplot2 Using a Box in R

Nizamuddin Siddiqui
Updated on 06-Nov-2020 12:56:07

252 Views

There might be many ways to highlight text inside a plot but the easiest one would be using geom_label function of ggplot2 package, with the help of this function we can put the required text and the aesthetics of that text by using a single line of code. It is highly recommended that we should use geom_label function with desired specifications.Example Live DemoConsider the below data frame −set.seed(222) x

Advertisements