R Programming Articles

Page 27 of 174

How to find the percentage for frequencies stored in a vector with two decimal places in R?

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

To find the percentage for frequencies stored in a vector with two decimal places can be done with the help of sum function and round function. For example, if we have a vector of frequencies say x then the percentage of these frequencies can be found by using the command round((x/sum(x))*100,2). Check out the below examples to understand how it works.Example1Frequency1

Read More

How to find the index of the nearest smallest number in an R data frame column?

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

To find the index of the nearest smallest number in an R data frame column, we can use which function along with subsetting for the value for which we want to find the index of the nearest smallest number. To understand how it can be done check out the below examples.Example1Consider the below data frame −ID

Read More

How to create a data frame column with letters of both size in R?

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

To create a data frame column with letters of both sizes, we can simply use the letters function and LETTERS function, the first one corresponds to lowercase letters and the latter corresponds to uppercase letters with single square brackets as shown in the below examples.Example1df1

Read More

How to check if a data frame column contains duplicate values in R?

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

To check if a data frame column contains duplicate values, we can use duplicated function along with any. For example, if we have a data frame called df that contains a column ID then we can check whether ID contains duplicate values or not by using the command −any(duplicated(df$ID))Example1Consider the below data frame −ID

Read More

How to find the mean squared error for linear model in R?

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

To find the mean squared error for linear model, we can use predicted values of the model and find the error from dependent variable then take its square and the mean of the whole output. For example, if we have a linear model called M for a data frame df then we can find the mean squared error using the command mean((df$y-predict(M))^2).Example1Consider the below data frame −x1

Read More

How to change the tick size using ggplot2 in R?

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

To change the tick size using ggplot2, we can use theme function with argument axis.ticks.length. For example, if we have a data frame called df that contains two columns say x and y then the scatterplot between x and y with larger size of tick marks can be created by using the below command −ggplot(df,aes(x,y))+geom_point()+theme(axis.ticks.length=unit(0.8,"inch"))ExampleConsider the below data frame −x

Read More

How to change the code "Yes" to 1 in an R data frame column?

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

To change the code “Yes” to 1, we can use ifelse function and set the Yes to 1 and others to 0. For example, if we have a data frame called df that contains a character column x which has Yes and No values then we can convert those values to 1 and 0 using the command ifelse(df$x=="Yes",1,0).Example1Consider the below data frame −Agree

Read More

How to create horizontal lines for each bar in a bar plot of base R?

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

To create horizontal lines for each bar in a bar plot of base R, we can use abline function and pass the same values as in the original barplot with h argument that represents horizontal with different color to make the plot a little better in terms of visualization.Examplex

Read More

How to sort each row of an R data frame in increasing order?

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

To sort each row of an R data frame in increasing order, we can use apply function for sorting the columns and then transpose the output. For example, if we have a data frame called df that contains 5 columns then each row of df can be sorted in increasing order by using the command t(apply(df,1,sort)).Example1Consider the below data frame −x1

Read More

How to minus one column from another in an R matrix?

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

To minus one column from another in an R matrix, we first need to read the matrix as a data frame using as.data.frame then find minus the columns using minus sign and accessing the column of the data frame. To understand how it can be done look at the steps in below examples.ExampleConsider the below data frame −M1

Read More
Showing 261–270 of 1,740 articles
« Prev 1 25 26 27 28 29 174 Next »
Advertisements