Found 2038 Articles for R Programming

How to remove all text from a string before a particular character in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:06:57

2K+ Views

Sometimes we want to extract a sub-string from a big string and that sub-string lies after a particular character. For example, a string could be “Learning.Computer.Science.is.not.difficult-Author” and we want to extract the word Author from it. This can be done with the help of gsub function.Examplesx1

How to rename the factor levels of a factor variable by using mutate of dplyr package in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:04:07

1K+ Views

We know that a factor variable has many levels but it might be possible that the factor levels we have are not in the form as needed. For example, if we want to have capital letters as a factor level but the original data has small letters of English alphabets. In this situation we can rename those factor levels by using mutate of dplyr package.ExampleConsider the below data frame −City

How to create a new column for factor variable with changed factor levels by using mutate of dplyr package in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:02:59

868 Views

We know that a factor variable has many levels but it might be possible that the factor levels we have are not in the form as needed. For example, if we want to have capital letters as a factor level but the original data has small letters of English alphabets. In this situation we can convert those factor levels by using mutate of dplyr package.ExampleConsider the below data frame −x

How to add a new column at the front of an existing R data frame?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 12:00:55

2K+ Views

Generally, when we add a new column to an existing R data frame that column is added at the end of the columns but we might need it at the front. This totally depends on our ease of use, familiarity with variables, and their need. We can add a new column at the front of an existing R data frame by using cbind function.ExampleConsider the below data frame −ID

How to replace the values in a matrix with another value based on a condition in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:58:07

3K+ Views

A matrix has only numeric values and sometimes these values are either incorrectly entered or we might want to replace some of the values in a matrix based on some conditions. For example, if we have few fives in a matrix then we might want to replace all fives to an another number which is greater than 5 or less than 5.ExampleConsider the below matrix −set.seed(123) M

How to extract a single column of an R data frame as a data frame?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:55:00

248 Views

Generally, we extract columns as a vector from an R data frame but sometimes we might need a column as a data frame, therefore, we can use as.data.frame to extract columns that we want to extract as a data frame with single square brackets. The purpose behind this could be merging the column with another data frame.ExampleConsider the below data frame −set.seed(9) x1

How to stop par(mfrow) to create multiple plots in one plot window and create only one plot in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:53:25

1K+ Views

When we use par(mfrow), we define the number of plots we want to draw on the plot window and when we draw all the necessary plots then starts again with the first plot. For example, if we set par(mfrow) to (2,2) then we will have four plots on the plot window but if we want to create one plot on the plot window then it does not work, it will show a small plot on the upper left side. To deal with the problem, we can set par(mfrow) to (1,1).Examplepar(mfrow=c(2,2)) x

How to convert a data frame to a matrix if the data frame contains factor variable as strings in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:50:16

437 Views

A matrix contains only numeric values, therefore, if we will convert a data frame that has factor variables as strings then the factor levels will be converted to numbers. These numbering is based on the first character of the factor level, for example, if the string starts with an A then it will get 1, and so on. To convert a data frame to a matrix if the data frame contains factor variable as strings, we need to read the data frame as matrix.ExampleConsider the below data frame −x1

How to change number formatting from scientific to numbers of axes labels in a scatterplot using ggplot2 package in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:48:47

351 Views

When we create a scatterplot or any other plot and the values are presented in scientific form in the original data then the axes values of the plot are also plotted in scientific form. This makes the plot ambiguous, therefore, reading the plot or interpreting it becomes difficult. Hence, we need to convert the scientific form of the axes labels to numbers and it can be done by using scale_x_continuous(labels =comma) and scale_y_continuous(labels=comma) for both the axes.ExampleConsider the below data frame −set.seed(101) x

How to manage top and bottom spaces of a bar plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 11:47:07

2K+ Views

A bar plot is one of the most commonly used plots for categorical data and it can be easily done in R with the help of ggplot2. When we create a bar plot using ggplot2, there exists some space between bars and the X-axis and the largest bar and top area of the plot. This can be reduced or increased by using scale_y_continuous function.ExampleConsider the below data frame −x

Advertisements