Programming Articles - Page 1771 of 3363

How to collapse factor levels in an R data frame?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 10:33:36

706 Views

Sometimes the levels of a factor are not correctly recorded, for example, recording male with M in some places and with Mal in some places hence there are two levels for level male. Therefore, the number of levels increases if the factor levels are incorrectly recorded and we need to fix this issue because the analysis using these factor levels will be wrong. To convert the incorrect factor levels into the appropriate ones, we can use list function to define those levels.Example 1 Live DemoF

How to convert two columns of an R data frame to a named vector?

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

522 Views

If two columns are of a form such that one column contains the name of the vector values and another column having the values of a vector then we might want to convert them into a vector. To do this, we can simply read the vectors with their data type and structure them with structure function.Example 1 Live Demox1

How to find the maximum using aggregate and get the output with all the columns in R?

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

601 Views

When we use aggregate function to find maximum or any other value, the output of the aggregation does not provide all the columns that corresponds to the maximum value. Therefore, we need to merge the data frame obtained by using aggregate with the original data frame. In this way, we will get only those rows that are common between the new data frame and the original one.ExampleConsider the below data frame − Live Demoset.seed(99) x1

How to join the points of a point chart if X-axis values are categorical in R using ggplot2?

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

760 Views

A point chart is usually drawn to see the relationship between two continuous variables and it is also called scatterplot but if the independent variable is categorical then we simply call it a point chart. Often, we want to join or connect the points of a point chart to visually represent the variation of categories of the independent variable and make it a line chart. This can be done by setting stat_summary argument geom to line and setting group = 1 in aes.ExampleConsider the below data frame − Live DemoClass

How to remove NULL values from a list in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:45:44

6K+ Views

The value NULL is used to represent an object especially a list of length zero. If a list contains NULL then we might want to replace it with another value or remove it from the list if we do not have any replacement for it. To remove the NULL value from a list, we can use the negation of sapply with is.NULL.Examples Live Demox

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

526 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

521 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

136 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

805 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

846 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

Advertisements