Found 2038 Articles for R Programming

How to split a string vector that contain strings of equal sizes and have spaces between values then extract only few values in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:48:52

95 Views

A string vector can contain any value including spaces. Sometimes a string vector is read with some spaces as well and we want to split the vector then extract few values. For example, if a string has “ABC 123” then we might want to extract the number 123 so that we can use it in analysis. If the string vector has strings of equal sizes then it can be easily done with the help of substr function.Examples> x1 x1 [1] "1 00" "1 01" "1 02" "1 03" "1 03" "1 04" > Numeric_Last_two_x1 Numeric_Last_two_x1 [1] "00" "01" "02" "03" ... Read More

How to find the total of frequency based on the values of a factor column in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:46:54

355 Views

Often, we have duplicate values in a factor column that means a factor column has many levels and each of these levels occur many times. In this situation, if we have a frequency column then we want to find the total of that frequency based on the values of a factor column and this can be done by using aggregate function.Example Live DemoConsider the below data frame −> set.seed(109) > Class Frequency df1 df1Output    Class Frequency 1     E       9 2     D       5 3     B       10 ... Read More

How to convert unit of measurements of a value or a vector in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:40:58

506 Views

There are many units of measurements for a single object or item. For example, weight can be measured in milligrams, grams, kilograms, tons, oz, lbs, etc. Now suppose we have two variables that belong to the same unit of measurement as weight of a coca cola cans and weight of apple juice, if the weights given for both of these variables have different units like one having grams and the other having oz then we might want to convert one of them. This will help us to compare both the variables easily without conflicting the scale of measurements. Therefore, we ... Read More

How to display the legend of a bar plot in a colored box in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:38:07

279 Views

When we create a bar plot or any other plot with legend, the background of the legend is white but it can be changed to any color with the help of scales package. We can make changes in the legend of a plot using alpha in legend.background argument of theme function. This will help us to change the background color of the legend.Example Live Demo> x y df dfOutput   x  y 1   0 25 2 100 28 3 150 32 4 200 25Creating a bar plot with legend −> library(ggplot2) > ggplot(df, aes(x, y, fill=x))+geom_bar(stat="identity")OutputChanging the background color of the ... Read More

How to represent X-axis label of a bar plot with greater than equal to or less than equal to sign using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:27:27

627 Views

The values of the categorical variable can be represented by numbers, by characters, by a combination of numbers and characters, by special characters, by numerical signs or any other method. But when we create the bar plot, if the size of a label name is large then we might want to reduce it by representing it with a different word or character or sign that gives the same meaning and it can be done by using expression argument inside scale_x_discrete.ExampleConsider the below data frame − Live Demo> x y df dfOutput   x  y 1 0   25 2 100 28 3 ... Read More

How to create a vector with zero values in R Program?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:23:48

102 Views

In data analysis, sometimes we need to use zeros for certain calculations, either to nullify the effect of a variable or for some other purpose based on the objective of the analysis. To deal with such type of situations, we need a zero value or a vector of many ways to create a vector with zeros in R. The important thing is the length of the vector.Examples> x1 x1 [1] 0 0 0 0 0 0 0 0 0 0 > x2 x2 [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ... Read More

How to find the standardized coefficients of a linear regression model in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:22:23

1K+ Views

The standardized coefficients in regression are also called beta coefficients and they are obtained by standardizing the dependent and independent variables. Standardization of the dependent and independent variables means that converting the values of these variables in a way that the mean and the standard deviation becomes 0 and 1 respectively. We can find the standardized coefficients of a linear regression model by using scale function while creating the model.ExampleConsider the below data frame − Live Demo> set.seed(99) > x y df1 df1Output      x       y 1 1.7139625 1.2542310 2 1.9796581 2.9215504 3 1.5878287 2.7500544 4 ... Read More

How to select columns of an R data frame that are not in a vector?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:17:38

333 Views

An R data frame can have so many columns and we might want to select them except a few. In this situation, it is better to extract columns by deselecting the columns that are not needed instead of selecting the columns that we need because the number of columns needed are more than the columns that are not needed. This can be done easily with the help of ! sign and single square brackets.ExampleConsider the below data frame − Live Demo> Age Gender Salary ID Education Experience df dfOutput ID    Gender    Age     Salary    Experience    Education 1 ... Read More

How to add a new column in an R data frame by combining two columns with a special character?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 08:03:44

585 Views

A data frame can have multiple types of column and some of them could be combined to make a single column based on their characteristics. For example, if a column has characters and the other has numbers then we might want to join them by separating with a special character to showcase them as an identity.ExampleConsider the below data frame − Live Demo> ID Frequency set.seed(111) > ID Frequency df dfOutput   ID Frequency 1 A    78 2 B    84 3 C    83 4 D    47 5 E    25 6 F    59 7 G    69 ... Read More

How to find the mean of corresponding elements of multiple matrices in R?

Nizamuddin Siddiqui
Updated on 04-Sep-2020 07:54:13

686 Views

If the elements of multiple matrices represent the same type of characteristic then we might want to find the mean of those elements. For example, if we have matrices M1, M2, M3, and M4 stored in a list and the first element represent the rate of a particular thing, say Rate of decay of rusty iron during rainy season, then we might want to find the mean of first element of matrix M1, M2, M3, and M4. This mean can be found by using Reduce function.ExampleConsider the below matrices and their list − Live Demo> M1 M1Output   [, 1] [, 2] ... Read More

Advertisements