R Programming Articles

Page 151 of 174

How to create a black and white word cloud in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 14-Oct-2020 237 Views

According to Google, a word cloud is an image composed of words used in a particular text or subject, in which the size of each word indicates its frequency or importance. In R, we can create word cloud by using wordcloud function of wordcloud package. So, we have the same name of the function as the package, thus we should not get confused by it.Loading wordcloud package and creating a wordcloud −library("wordcloud") x

Read More

How to check whether a year or a vector of years is leap year or not in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 14-Oct-2020 524 Views

Almost everyone knows that a leap has 366 instead of 365 days and it occurs once in four years. If we want to check whether a particular year is a leap year or in a range of years which years correspond to leap year then we can use leap_year function of leap year. The length function can be used with the year value and if the output is 1 then it will be a leap year otherwise the output will be 0 which refers to the non-leap year.Loading lubridate package −Examplelibrary("lubridate") year1

Read More

How to change the title of a graph to italic created by using plot function in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 14-Oct-2020 338 Views

If a graph is created by specifying main title of the plot using the plot function then the default font is plain text. We might want to change the style of the font to italic so that the title gets a little more attraction of the viewers. This can be done by using font.main argument with plot function. The value 4 of font.main refers to the bold italic font and if we want to make it bold then we can use the value 3.Consider the below vectors and create the scatterplot between the two with title of the plot −Examplex

Read More

How to create a histogram using weights in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Oct-2020 1K+ Views

A histogram using weights represent the weighted distribution of the values. In R, we can use weighted.hist function of plotrix package to create this type of histogram and we just need the values and weights corresponding to each value. Since plotrix is not frequently used, we must make sure that we install this package using install.packages("plotrix") then load it in R environment.Loading plotrix package −library("plotrix")Consider the below vector and the weight associated with that vector −Examplex

Read More

How to find prime numbers between two values or up to a value in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Oct-2020 1K+ Views

We know that the prime numbers are the numbers that are not divisible by any number except by themselves or 1 and 1 is not considered as a prime number. In R, we can find the prime numbers up to a number or between two numbers by using Prime function of numbers package. If we want to find the total number of prime numbers between 1 and 10, then we just need to pass 10 inside the Prime function, otherwise range will be required.Loading numbers package −library("numbers")Primes(10)[1] 2 3 5 7Primes(100)[1] 2 3 5 7 11 13 17 19 23 ...

Read More

How to create side-by-side boxplot in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 10-Oct-2020 480 Views

Often, we need to compare continuous variables using boxplots and thus side-by-side boxplots are required. Creating side-by-side boxplot in base R can be done with the help of creating space for graphs with the help of par(mfrow=). In this function, we can define the number of graphs and the sequence of these graphs, thus creation of side-by-side boxplot will become easy.Consider the below vectors −set.seed(100) x

Read More

How to create two plots using ggplot2 arranged in a vertical manner in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Oct-2020 717 Views

The two plots created by using ggplot2 can be arranged in a vertical manner with the help gridExtra package, we simply needs to use grid.arrange function to do so. For example, if we have two plots created by using ggplot2 and saved in objects p1 and p2 then they can be vertically arranged as grid.arrange(p1,p2)Consider the below data frame −Exampleset.seed(151) x

Read More

How to simulate normal distribution for a fixed limit in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Oct-2020 728 Views

To simulate the normal distribution, we can use rnorm function in R but we cannot put a limit on the range of values for the simulation. If we want simulate this distribution for a fixed limit then truncnorm function of truncnorm package can be used. In this function, we can pass the limits with and without mean and standard deviation.Loading and installing truncnorm package −>install.packages("truncnorm") >library(truncnorm)Examplertruncnorm(n=10, a=0, b=10)[1] 0.76595522 0.33315633 1.29565988 0.67154230 0.04957334 0.38338705 [7] 0.75753005 0.65265304 0.63616552 0.45710877rtruncnorm(n=50, a=0, b=100)[1] 0.904997947 0.035692016 0.402963452 1.001102057 1.445190636 0.109245234 [7] 0.205630845 0.312428027 0.465876772 0.424647787 0.309222394 0.442172805 [13] 0.365503292 1.277570451 0.235747661 1.128447123 ...

Read More

How to create a transparent histogram using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Oct-2020 3K+ Views

When we create a histogram using ggplot2 package, the area covered by the histogram is filled with grey color but we can remove that color to make the histogram look transparent. This can be done by using fill="transparent" and color="black" arguments in geom_histogram, we need to use color argument because if we don’t use then the borders of the histogram bars will also be removed and this color is not restricted to black color only.ExampleConsider the below data frame −set.seed(987) x

Read More

How to select values less than or greater than a specific percentile from an R data frame column?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Oct-2020 740 Views

The percentiles divide a set of numeric values into hundred groups or individual values if the size of the values is 100. We can find percentiles for a numeric column of an R data frame, therefore, it is also possible to select values of a column based on these percentiles. For this purpose, we can use quantile function.ExampleConsider the below data frame −set.seed(111) x

Read More
Showing 1501–1510 of 1,740 articles
« Prev 1 149 150 151 152 153 174 Next »
Advertisements