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
R Programming Articles
Page 69 of 174
How to find the maximum value for each column of a matrix in R?
To find the maximum value for each column of a matrix, we need to use apply function. For example, if we have a matrix M that contains 2 rows and 2 columns with values 1, 2 in the first row and 3, 4 in the second row then the maximum for each of the columns in that matrix can be found by using the syntax; apply(M,2,max), hence the result will be 3, 4.ExampleM1−-matrix(1:36,ncol=6) M1Output [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 7 13 19 25 31 [2,] 2 8 14 20 26 32 [3,] 3 9 15 21 27 33 [4,] 4 10 16 22 28 34 [5,] 5 11 17 23 29 35 [6,] 6 12 18 24 30 36Exampleapply(M1,2,max)Output[1] 6 12 18 24 30 36ExampleM2
Read MoreHow to create a sequence of time in minutes with date in R?
To create a sequence of time in minutes with date we can use seq function and define the date and time with as.POSIXct. For example, if we want to generate a sequence of time between 2 pm on tenth October 2020 to 4 pm on the same date then we can use the following code −seq(from=as.POSIXct("2020-10-10 14:00"), to=as.POSIXct("2020-10-10 16:00"), by="min")Exampleseq(from=as.POSIXct("2020-01-01 00:01"), to=as.POSIXct("2020-01-01 01:00"), by="min")Output[1] "2020-01-01 00:01:00 IST" "2020-01-01 00:02:00 IST" [3] "2020-01-01 00:03:00 IST" "2020-01-01 00:04:00 IST" [5] "2020-01-01 00:05:00 IST" "2020-01-01 00:06:00 IST" [7] "2020-01-01 00:07:00 IST" "2020-01-01 00:08:00 IST" [9] "2020-01-01 00:09:00 IST" "2020-01-01 00:10:00 IST" [11] "2020-01-01 ...
Read MoreHow to generate random samples rounded to 4 decimal places in R?
Random samples can be generated in many ways such as using discrete and continuous distributions, using integer vectors, using numerical vectors, using character vectors and/or factor vectors, also with columns of a data set. If we have the sample that is continuous in nature then the values are likely to contain many values after decimal point and we can limit those values to 4 or use any other limit using round function.Examplex1
Read MoreHow to combine array of vectors in R?
To combine array of vectors we can use rbind function. For example, if we have multiple vectors say x, y, z of same size or different sizes but the total number of elements are even then we can use rbind(x,y,z) to combine those vectors. Check out the examples to understand how it works.Example1x1
Read MoreHow to replicate a vector to create matrix in R?
The matrix can be created by using matrix function in R and if we want to create a matrix by replicating a vector then we just need to focus on the replication. For example, if we have a vector V and we want to create matrix by replicating V two times then the matrix can be created as matrix(replicate(2,V),nrow=2).Example1V1
Read MoreHow to find the groupwise mean and save it in a data frame object in R?
We often need groupwise mean in data analysis, especially in situations where analysis of variance techniques is used because these techniques helps us to compare different groups based on their measures of central tendencies and measures of variations. It can be done by using aggregate function so that the output can be saved in a data frame object. In the below examples, we can see how it can be done and also check the final object type.ExampleConsider the below data frame −set.seed(109) Salary
Read MoreHow to create a data frame with combinations of values in R?
Suppose we have two values 0 and 1 then how many combinations of these values are possible, the answer is 8 and these combinations are (0,0), (1,0), (0,1), (1,1). In R, we can use expand.grid function to create these combinations but to save it in a data frame, we would need to use as.data.frame function.Exampledf1
Read MoreHow to change the size of correlation coefficient value in correlation matrix plot using corrplot in R?
The correlation coefficient value size in correlation matrix plot created by using corrplot function ranges from 0 to 1, 0 referring to the smallest and 1 referring to the largest, by default it is 1. To change this size, we need to use number.cex argument. For example, if we want to decrease the size to half then we can use number.cex = 0.5.ExampleConsider the below matrix −set.seed(99) M corrplot(cor(M), addCoef.col="black")OutputChanging the size of correlation coefficient value to 0.75 −corrplot(cor(M), addCoef.col="black", number.cex=0.75)OutputChanging the size of correlation coefficient value to 0.30 −> corrplot(cor(M), addCoef.col="black", number.cex=0.30)Output
Read MoreHow to create a contingency table with sum on the margins from an R data frame?
The sum of rows and columns on the margins in a contingency table are always useful because they are used for different type of calculations such as odds ratio, probability etc. If an R data frame has factor columns then we can create a contingency table for that data frame and it can be done by using addmargins function.ExampleConsider the below data frame −x1
Read MoreHow to find the absolute pairwise difference among values of a vector in R?
If a vector contains five values then there will be ten pairwise differences. For example, suppose we have five numbers starting from 1, then the pairwise combinations for these values will be (1,2), (1,3), (1,4), (1,5), (2,3), (2,4), (2,5), (3,4), (3,5), (4,5). Now to find the absolute pairwise differences, we would be need to find the differences between each of these combinations and take the absolute value of the answer hence the result will be 1, 2, 3, 4, 1, 2, 3, 1, 2, 1.Examplex1
Read More