Programming Articles - Page 1773 of 3366

How to create a line chart for a subset of a data frame using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:17:33

636 Views

Subsetting is not a difficult thing in R but if we make our code short then it is a little tedious task because we will have to introduce code between codes and that creates confusion. Therefore, we must be very careful while writing a code inside another code. To create a line with subsetting the data frame using ggplot function of ggplot2 can be done by using subset function.Example Live DemoConsider the below data frame −set.seed(99) x1

How to create a Venn diagram in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:11:50

458 Views

A Venn diagram helps to identify the common and uncommon elements between two or more than two sets of elements. This is also used in probability theory to visually represent the relationship between two or more events. To create a Venn diagram in R, we can make use of venn function of gplots package.ExampleConsider the below vectorsx

How to find monthly sales using date variable in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 06:05:05

464 Views

Sales analysis requires to find monthly sales mean, total, range, and often standard deviation. This is mostly required by FMCG (Fast-Moving Consumer Goods) companies because they want to track their sales on daily basis as well as monthly basis. If we have daily sales data then we need to create another column for months in an R data frame to find the monthly sales and this can be done with the help of strftime and aggregate function.ExampleConsider the below data frame −date

How to save a plot as SVG created with ggplot2 in R?

Nizamuddin Siddiqui
Updated on 29-Aug-2020 08:05:31

5K+ Views

   There are multiple ways to save a plot created in R. Base R provides, metafile, bitmap, and postscript options to copy and save the plots created in R but we can also save the plots created with ggplot2 as an SVG file with the help of svglite package. The ggsave function of svglite package does this job easily and we can also define the height and width of the plot inside this function.Example Live DemoInstall the svglite package −install.packages("svglite")Consider the ToothGrowth data and create a scatterplot between len and dose −head(ToothGrowth) len supp dose 1 4.2 VC ... Read More

Why do we get warning 'newdata' had 1 row but variables found have X rows while predicting a linear model in R?

Nizamuddin Siddiqui
Updated on 29-Aug-2020 08:01:43

2K+ Views

The reason we get newdata had 1 row warning is the newdata is not correctly defined. We should give the name of the explanatory variable or independent variable to the newdata so that the model can identify that we are passing the mean of the explanatory variable, otherwise it considers all the values of the explanatory hence the result of the predict function yields the predicted values for the sample size.Example Live DemoConsider the below data frame −set.seed(123) x

How to increase the space between bars of a bar plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 29-Aug-2020 08:00:20

739 Views

When a bar plot is created then the distance or space between bars is equal but sometimes the width of the bar is large, therefore, it becomes a little difficult to understand the difference between those bars especially in cases when the data values are not very much different from each other. To overcome this visualization problem, we can create a bar plot with some space between the bars and it can be done with the help of width argument of geom_bar in ggplot2.ExampleConsider the below data frame −x

How to replace space in a string value for some elements in a column of an R data frame?

Nizamuddin Siddiqui
Updated on 29-Aug-2020 07:42:50

475 Views

Most of the times, the string data is in bad shape and we need to make it appropriate so that we can easily proceed with the analysis. There is also a situation in which a string column has some values where an extra space is used which was not required, therefore, it does not match with the rest of the column values. To remove these spaces, we can use lapply and gsub function.ExampleConsider the below data frame −x1

How to add a month to a date in R?

Nizamuddin Siddiqui
Updated on 21-Aug-2020 05:49:26

713 Views

We have to deal with date data in time series analysis, also sometimes we have a time variable in data set that is recorded to perform another type of analysis. Depending on our objective, we need to process the data and the time variable is also converted into appropriate form that we are looking for. If we want to create a sequence of months from date data then we can do it by adding a month to each upcoming month. This can be easily done by using AddMonths function of DescTools package.ExampleInstalling DescTools package −install.packages("DescTools") Loading DescTools package: library(DescTools) AddMonths(as.Date('2020/01/31'), ... Read More

Find the minimum sum of distance to A and B from any integer point in a ring of size N in Python

Arnab Chakraborty
Updated on 20-Aug-2020 08:40:14

149 Views

Suppose we have a ring, which is made of few numbers from 1 to N. We also have tow numbers A and B. Now, we can stand at any place (say x) and perform the count operation with respect of the total sum of the distance (say Z = distance from X to A + distance from X to B). We have to select X such that Z is minimized. At the end return the value of Z. We have to keep in mind that X will not be same as A and B.So, if the input is like N ... Read More

Find the minimum positive integer such that it is divisible by A and sum of its digits is equal to B in Python

Arnab Chakraborty
Updated on 20-Aug-2020 08:37:36

283 Views

Suppose we have two numbers A and B, we have to find the minimum positive number M so that M is divisible by A and the sum of the digits of M is same as B. So, if there is no such result, then return -1.So, if the input is like A = 50, B = 2, then the output will be 200 as this is divisible by 50 and sum of its digit = 2 + 0 + 0 = 2.To solve this, we will follow these steps −Define one element type container, that contains two numbers a and ... Read More

Advertisements