Found 2038 Articles for R Programming

How to find the correlation matrix of groups for a data.table object in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:36:27

282 Views

To find the correlation of groups, we can use cor function but it cannot be directly used.For this purpose, we first need to set they key for group column of data table object. For example, if we have a data.table DT with one numerical column defined as x and one group column defined as Group having 4 groups as a, b, c, and d then the correlation of numerical values for groups a and b can be found as −setkey(DT, Group) cor(DT["a"]$x, DT["b"]$x)Loading data.table package −library(data.table)ExampleConsider the below data.table object −xRead More

How to replace upper triangular matrix with lower triangular matrix and vice versa in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:35:21

697 Views

The upper triangular matrix can be replaced with lower triangular matrix by transposing the whole matrix and extracting upper triangular matrix from it then storing it in the original matrix. For example, if we have a matrix M then upper triangular matrix of M can be replaced with lower triangular matrix by using the below code −M1[upper.tri(M1)]

How to remove duplicate rows and sort based on a numerical column an R data frame?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:21:59

569 Views

If we have duplicate rows in an R data frame then we can remove them by using unique function with data frame object name. And if we want to order the data frame with duplicate rows based on a numerical column then firstly unique rows should be found then order function can be used for sorting as shown in the below examples.ExampleConsider the below data frame − Live Demox1

How to find the table of ordered frequencies of vector elements in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:20:21

330 Views

We can create table of frequencies of a vector element by using table function and the ordering can be done by using sort function. If we want to order the frequencies in decreasing order then decreasing argument can be used. For example, if we have a vector x then the table of ordered frequencies can be created as sort(table(x)).Example Live Demox1

How to create a classification model using svm for multiple categories in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:18:50

286 Views

SVM is a supervised machine learning algorithm which can be used for both classification or regression challenges but mostly we use it for classification. The classification using svm can be done for two or more categories as well. In R, we can use simply use svm function of e1071 package.ExampleConsider the iris data − Live Demostr(iris)Output'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 ... Read More

How to truncate a numerical vector to a specified number of decimal places in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:12:15

3K+ Views

The truncation means removing the number of decimal places but not rounding. For example, if we have a value 5.1742145 then truncating to one decimal place will be 5.1 and rounding will be 5.2. In R, we can do this by using trunc function as shown in the below examples.Example Live Demox1

How to find the maximum of each row in an R data frame?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:09:58

6K+ Views

Sometimes we need maximum values, it helps us to identify which case or subject occurs at the greatest point hence we can understand the limit for the sample or population under study. If we want to find the maximum of values two or more columns for each row in an R data frame then pmax function can be used.ExampleConsider the below data frame − Live Demoset.seed(1997) x1

How to create a matrix without column and row indices in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:07:17

382 Views

To create a matrix without column and row indices, we first need to create the matrix and then prmatrix function can be used to convert that matrix without column and row indices but we would need to provide the number of rows inside the function. For example, if we have a matrix M that contains 5 rows and 5 columns then it can be converted to a matrix without column and row indices using prmatrix(M,rowlab=rep("",5),collab=rep("",5)).Example Live DemoM1

How to create a plot of Poisson distribution in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:04:31

2K+ Views

The Poisson distribution is a discrete distribution that has only one parameter named as lambda and it is the rate parameter. The rate parameter is defined as the number of events that occur in a fixed time interval. To create a plot of Poisson distribution in R, we can use the plot function with the density of the Poisson distribution using dpois function.Example Live Demoplot(dpois(x=1:50,lambda=3))OutputExample Live Demoplot(dpois(x=1:50,lambda=3),type="l")OutputExample Live Demoplot(dpois(x=1:50,lambda=3),type="b")Output

How to create a plot in base R without margins?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:02:02

453 Views

To create a plot without margins, we first need to define that margin in a way that the plot created after that will not have margins and this can be done by using par function. We would need to pass mar function within par function as par(mar=c(0,0,0,0)).Example Live Demopar(mar=c(0,0,0,0)) plot(1:10)OutputExample Live Demobarplot(1:10)Output

Advertisements