Found 2038 Articles for R Programming

How to subtract number of days from a date to get the previous date in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:43:32

356 Views

In our daily life, we might want to know what was the date before some number of days. This is also required in professional life, especially in those professions where we work on projects and have tight deadlines. To find the date before a certain number of days we can just use subtraction sign after reading the date with as.Date.Examplesas.Date("2001-01-01")-30 [1] "2000-12-02" as.Date("2020-06-30")-30 [1] "2020-05-31" as.Date("2020-06-30")-50 [1] "2020-05-11" as.Date("2020-06-30")-100 [1] "2020-03-22" as.Date("2020-06-30")-120 [1] "2020-03-02" as.Date("2020-06-30")-15 [1] "2020-06-15" as.Date("2020-06-30")-45 [1] "2020-05-16" as.Date("2020-06-30")-40 [1] "2020-05-21" as.Date("2020-12-25")-20 [1] "2020-12-05" as.Date("2020-12-25")-300 [1] "2020-02-29" as.Date("2020-12-25")-125 [1] "2020-08-22" as.Date("2020-12-25")-80 [1] "2020-10-06"We can also use / to ... Read More

How to create permutations as a list in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:42:32

368 Views

The permutation is the combination with orders. For example, if we want to create a key for lock with a sequence of numbers then it must be order in some direction, otherwise, it will be difficult to remember and easy to unlock. We can find the permutation of some numbers or characters by using permn function of combinat package.Loading the combinat package −library(combinat)Examples that create list of permutations −permn(LETTERS[1:4]) [[1]] [1] "A" "B" "C" "D" [[2]] [1] "A" "B" "D" "C" [[3]] [1] "A" "D" "B" "C" [[4]] [1] "D" "A" "B" "C" [[5]] [1] "D" "A" "C" "B" [[6]] ... Read More

How to write a long line for the X-label of a scatterplot in R using ggplot2?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:38:43

66 Views

When we create a plot in R, the variable names are automatically plotted as axes labels but sometimes we want to give a brief detail of the X-label or a Y-label. If that brief is not small so that the expression function can contain the length of the label then it becomes difficult but it can be done with the help of atop inside expression.ExampleConsider the below data frame − Live Demoset.seed(123) x

How to reverse a vector in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:34:16

644 Views

Sometimes the vector values are recorded in the reverse order in R, therefore, we need to again reverse those vectors to get the actual order we want. For example, a sequence of numbers might be recorded as 1 to 20 but we wanted it to be from 20 to 1. The reversing of order of the vector values can be easily done with the help of rev function.Examplesx1

How to a split a continuous variable into multiple groups in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:32:27

637 Views

Splitting a continuous variable is required when we want to compare different levels of a categorical variable based on some characteristics of the continuous variable. For example, creating the salary groups from salary and then comparing those groups using analysis of variance or Kruskal-Wallis test. To split a continuous variable into multiple groups we can use cut2 function of Hmisc package −Example Live DemoConsider the below data frame −set.seed(2) ID

How to split a long string into a vector of substrings of equal sizes in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:29:39

319 Views

If a vector is recorded as a single string by mistake or the file that contains the data did not separated the string in an appropriate way then we might need to split in the correct form so that we can proceed with the further analysis. This might happen when the levels of a factor variable that have equal name length are not separated. In this case, we can split the string into a vector that contain substring of equal sizes by using substring function.ExamplesJust look at these examples to understand how substring function can help us to split the ... Read More

How to visualize the normality of a column of an R data frame?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:28:32

103 Views

The first step to analyze a variable is checking whether it is normally distributed or not and to visually do this, we create a histogram. If the histogram depicts a bell then we consider that the variable is normally distributed otherwise, it is not. We can create a histogram for any column of an R data frame by using hist function.ExampleConsider the below data frame −set.seed(9) df

How to create a bar plot in R with label of bars on top of the bars using ggplot2?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:25:03

390 Views

There are multiple ways to represent a chart, specifically a bar plot is represented with so many variations. We can also include bar labels in a bar plot so that the viewer can easily understand the frequency of the categories for bars. To put the labels on top of the bars in a bar plot we can use vjust = 0 with geom_text in ggplot2.Example Live DemoConsider the below data frame −df

How to create a new data frame for the mean of rows of some columns from an R data frame?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:22:16

695 Views

Finding row means help us to identity the average performance of a case if all the variables are of same nature and it is also an easy job. But if some of the columns have different type of data then we have to extract columns for which we want to find the row means. Therefore, we can create a new data frame with row means of the required columns using rowMeans function.Example Live DemoConsider the below data frame −set.seed(88) Group

How to add or multiply each element of a matrix to the corresponding element of another matrix in R, if these matrices are stored as a list?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:20:14

94 Views

Basic mathematical operations such as addition, subtraction, multiplication, and division are common for matrices and we often do that but if the matrices are stored as a list in R then these basic calculations are done differently as they are not direct objects. To add or multiply the matrices in a list, we can use Reduce function with the plus (+) or multiply (*) sign and the list name.Example Live DemoConsider the below list of matrices −Matrices_List

Advertisements