R Programming Articles

Found 1,740 articles

How to replace blanks in a vector with the previous element in R?

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

Filling of blanks is not an easy task in data analysis, especially if the vector contains numerical or integer values. Suppose we have a vector x that contains 1, , 2, 3, 4, 5 and we want to put 1 in place of blank after first value then cummax function along with seq_along function can be used as x[cummax(seq_along(x)*(x!=""))].Example1x1

Read More

How to create a vector with all dates in a particular year in R?

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

We know that some years are leap years and some are normal years. The leap years have 366 days and the normal years have 365 days. To create a vector with all dates in a particular year we can use first date and the last date of the year by reading them with as.Date and creating a sequence with seq function. Check out the below examples to understand how it is done.Example1Creating a vector with dates in year 2020 −seq(as.Date("2020-01-01"), as.Date("2020-12-31"), by="1 day")Output[1] "2020−01−01" "2020−01−02" "2020−01−03" "2020−01−04" "2020−01−05" [6] "2020−01−06" "2020−01−07" "2020−01−08" "2020−01−09" "2020−01−10" [11] "2020−01−11" "2020−01−12" "2020−01−13" "2020−01−14" "2020−01−15" ...

Read More

How to concatenate two or more vectors in R?

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

The concatenation of vectors can be done by using combination function c. For example, if we have three vectors x, y, z then the concatenation of these vectors can be done as c(x,y,z). Also, we can concatenate different types of vectors at the same time using the same same function.Example1set.seed(999) x1

Read More

What is the difference between creating a matrix by using matrix function or as.matrix function in R?

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

The difference between as.matrix and matrix function is that nrow argument or ncol argument are not helpful with as.matrix function but with matrix function we can use them. Therefore, we can actual define a matrix with matrix function but if we have a data frame or data table then it can be converted to matrix by using as.matrix function.Examples of creating matrix with as.matrix and matrix functionExample1M

Read More

How to calculate mahalanobis distance in R?

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

The Mahalanobis distance is the relative distance between two cases and the centroid, where centroid can be thought of as an overall mean for multivariate data. We can say that the centroid is the multivariate equivalent of mean. If the mahalanobis distance is zero that means both the cases are very same and positive value of mahalanobis distance represents that the distance between the two variables is large. In R, we can use mahalanobis function to find the malanobis distance.Example1y1

Read More

How to check if a matrix is invertible or not in R?

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

If the matrix is singular then it is not invertible and if it is non−singular then it is invertible. Therefore, we can check if a matrix is singular or not. We can use is.singular.matrix function of matrixcalc for this purpose. For example, if we have a matrix called M then to check whether it is invertible or not, we can use is.singular.matrix(M).Example1Loading matrixcalc package and creating a matrix −library(matrixcalc) M1

Read More

How to print a large number of values in R without index?

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

Whenever we print any vector then the left-hand side of the R window shows indexing, even if we have one value in the vector the index will be there. For example, if we print a vector that has value equal to 2 then the print output will be [1] 2, here [1] represents the index. If we want to print the vector without index then cat function can be used as shown in the below examples.Example1x1

Read More

How to change the repeated row names and column names to a sequence in a matrix in R?

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

To change the repeated row names and column names to a sequence, we first need to read those names in a vector then set them to row names and column names with make.unique function. For example, if a matrix has row names defined as A, B, A, B, A then it can be converted into A, B, A.1, B.1, A.2.Example1M1

Read More

What is the difference between ordered factors and unordered factors in R?

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

To understand the difference ordered factors and unordered factors, it is better to understand them by creating the factor vectors by using ordered argument with TRUE and FALSE options. For example, if we have a vector x then it can be ordered or unordered as factor(x,ordered=TRUE) and factor(x,ordered=FALSE).Example1x1

Read More

Why do we get warning 'newdata' had 1 row but variables found have X rows while predicting a linear model in R?

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

The reason we get newdata had 1 row warning is the newdata is not correctly defined. We should give the name of the explanatory variable or independent variable to the newdata so that the model can identify that we are passing the mean of the explanatory variable, otherwise it considers all the values of the explanatory hence the result of the predict function yields the predicted values for the sample size.Examplepredict(M, newdata=data.frame(1.2), interval="confidence") fit lwr upr 1 4.645695 3.690676 5.600715 2 4.459543 3.635161 5.283925 ...

Read More
Showing 1–10 of 1,740 articles
« Prev 1 2 3 4 5 174 Next »
Advertisements