R Programming Articles

Page 73 of 174

How to find row minimum for an R data frame?

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

Data analysis is a difficult task because it has so much variation in terms of the smaller objectives of a big project. One of the smallest tasks could be finding the minimum value in each row contained in a data frame. For this purpose, we cam use apply function and pass the FUN argument as min so that we can get minimum values.Consider the below data frame −Exampleset.seed(101) x1

Read More

How to add a string before each numeric value in an R data frame column?

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

Sometimes the unique identity column of a data frame is not recorded as intended, it contains only numeric values that does not solve the data characteristic purpose. Therefore, we might want to add a string before those numeric values to make the data more sensible for viewers and analysts. This can be easily done with the help of gsub function.Consider the below data frame −Exampleset.seed(111) x1

Read More

How to find critical value for one-sided and two-sided t test in R?

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

To find the critical value for t test in R, we need to use qt function. This function requires level of significance and the sample size and returns the tabulated or critical value of t distribution. Below examples shows the calculation of critical value for different situations such as left-side test, right-side test or two-sided test.Left side critical value with sample size 30 and 95% confidence level −Exampleqt(0.05, 30)Output[1] -1.697261Right side critical value with sample size 30 and 95% confidence level −Exampleabs(qt(0.05, 30))Output[1] 1.697261 Exampleqt(0.05, 50)Output[1] -1.675905Exampleabs(qt(0.05, 50))Output[1] 1.675905 Exampleqt(0.01, 50)Output[1] -2.403272Exampleabs(qt(0.01, 50))Output[1] 2.403272 Exampleqt(0.01, 51)Output[1] -2.401718Exampleabs(qt(0.01, 51))Output[1] 2.401718 Exampleqt(0.01, ...

Read More

How to create a barplot with one of the bars having different color in R?

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

A bar plot represents discrete data and the bars in the bar plot are usually of same color but we might want to highlight a particular bar based on the characteristics of the data or the objective of the analysis project. For example, if a particular bar represents highly severe situation or highly unimportant situation then we can change the color that particular bar so that people can easily point out that bar.Consider the below data frame −Examplex

Read More

How to create train, test and validation samples from an R data frame?

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

To create predictive models, it is necessary to create three subsets of a data set for the purpose of training the model, testing the model and checking the validation of the model. These subsets are usually called train, test and validation. For this purpose, we can use different type of sampling methods and the most common is random sampling. In the below example, you can see how it can be done.Consider the mtcars data set in base R −Exampledata(mtcars) str(mtcars)Output'data.frame':32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ ...

Read More

How to create a random sample of some percentage of rows for a particular value of a column from an R data frame?

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

Random sampling is an important part of data analysis, mostly we need to create a random sample based on rows instead of columns because rows represent the cases. To create a random sample of some percentage of rows for a particular value of a column from an R data frame we can use sample function with which function.Consider the below data frame −Exampleset.seed(887) grp

Read More

How to find the indexes of minimum values in a vector if there are ties in R?

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

If we have repeated values in a vector that means we have ties in the vector, therefore, the indexes of values will help us to identify the positions of a particular value in the vector. We can use which function with min function to find the positions of minimum values in a vector, if there exists more than one minimum then the output will show all the relevant positions.Examplex1

Read More

How to select only one column from an R data frame and return it as a data frame instead of vector?

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

Generally, if we extract a single column from an R data frame then it is extracted as a vector but we might want it in data frame form so that we can apply operations of a data frame on it. Therefore, we can use single square brackets for the extraction with T (TRUE) or (FALSE) values and drop = FALSE so that the output becomes a data frame.Consider the below data frame −Exampleset.seed(999) x1

Read More

How to create blue or red colored boxplots in R using ggplot2?

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

The default color of boxplot area in R using ggplot2 is white but we might want to change that color to something more attracting, for example blue or red. To do this purpose, we can use geom_boxplot function of ggplot2 package with fill argument by passing the color names.Consider the below data frame −Exampleset.seed(1321) v1

Read More

How to calculate population variance in R?

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

There is no function in R to calculate the population variance but we can use the population size and sample variance to find it. We know that the divisor in population variance is the population size and if we multiply the output of var(it calculates sample variance) function with (population size – 1)/population size then the output will be population variance.Exampleset.seed(141) x1

Read More
Showing 721–730 of 1,740 articles
« Prev 1 71 72 73 74 75 174 Next »
Advertisements