Articles on Trending Technologies

Technical articles with clear explanations and examples

How to find the difference in number of days between two date columns of an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 1K+ Views

When dealing with date data, we often want to find the difference between dates if the data contains two or more date values. Same thing can be done for the two columns of an R data frame that contains dates but first we need to read those date columns in date format in case they are not recorded as date in R. The finding of difference in number of days can be done by using difftime function.ExampleConsider the below data −date1

Read More

How to transform numbers between 1 and 12 to abbreviated month in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 441 Views

Sometimes date vector for months is recorded in numeric form and it becomes difficult to treat or visualize it as a date vector. For example, if a vector for months has numbers 1 that represents January, 2 that represents February and so on then it is considered as a numeric vector instead of the vector to represent the month. To transform such type of vectors into abbreviated month as Jan, Feb, etc. we can use month.abb function.ExamplesMonth1

Read More

How to remove some last elements of a vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 5K+ Views

A vector in R can have infinite number of elements but we might want to remove some of them. To remove the last elements of a vector, we can use head function with negative sign of the number of values we do not want. For example, if we have a vector of length 200 but we don’t want last fifty elements then we can use head(vector_name,-50).Examplesx1

Read More

How to sort a matrix based on one column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 10K+ Views

Since a matrix contain only numeric values, sorting can be also done for matrices. There might be multiple reasons to sort a matrix such as we want to convert the matrix to a data frame, the data stored in matrix needs to be sorted prior to matrix calculations so that the view of the result after calculations becomes clearer, etc. To sort a matrix based on one column, we can use order function.Examplesset.seed(123) M1

Read More

How to generate random samples from different statistical distributions using parameters as a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 234 Views

To generate random samples from statistical distributions, we use functions like rnorm, rbinom, rexp, rpois for the corresponding distribution based on their names. Using these functions, we can pass their parameters as an argument inside the function. But if we have the parameters saved as a list then generation of random sample is not straight forward, for this we need to use do.call function.Examplesparameters1

Read More

How to change the position of the title of a plot which is created using plot function in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 1K+ Views

When we create a plot using plot function, the title of the plot appears on top of the plot while using main argument. If we use title function to create the title of the plot then we can adjust its position in many different ways such as any position between below and top border of the plot.Examplesx

Read More

How to rescale a continuous variable so that the range of the rescale becomes 0 to 1 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 24-Aug-2020 526 Views

Rescaling a continuous means that we want to standardize it with some properties and if we are using 0 to 1 as a range that represents that property. Most of the times, the objective behind rescaling is we want to nullify the effect of measurement units of the variable under consideration. To rescale so that the range becomes 0 to 1, we can use rescale function of scales package.ExampleLoading scales package −Examplelibrary(scales) x1

Read More

How to write partial title of X-axis in italics using ggplot2 of R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 7K+ Views

Of course, writing axes titles help viewers to understand the plot in a better way because they add more information to the plot. In general, the axes titles have simple font but we can change partial or complete title to italics to get the viewers attraction. This is needed when we want to highlight the title by making it different. In ggplot2, we can do this by using expression.ExampleConsider the below data frame −set.seed(1) x

Read More

How to select top rows of an R data frame based on groups of factor column?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 284 Views

We use head function to take a look at some top values in an R data frame but it shows the top values for the whole data frame without considering the groups of factor column. Therefore, if we have a large number of values in a particular group then head function does not seem to be helpful alone, we must use something to extract the top values for each of the groups. This can be done through using by function with single square brackets and head function.Examplesdata(iris) str(iris) 'data.frame': 150 obs. of 5 variables: $ Sepal.Length: num 5.1 4.9 4.7 ...

Read More

How to delete rows of an R data frame based on string match?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 21-Aug-2020 1K+ Views

Often, we need to subset our data frame and sometimes this subsetting is based on strings. If we have a character column or a factor column then we might be having its values as a string and we can subset the whole data frame by deleting rows that contain a value or part of a value, for example, we can get rid of all rows that contain set or setosa word in Species column.ExampleConsider the below data frame −Character

Read More
Showing 51471–51480 of 61,297 articles
Advertisements