Found 2038 Articles for R Programming

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

506 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

350 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

349 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

4K+ 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

554 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

353 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

545 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

How to change the column names in R within aggregate function?

Nizamuddin Siddiqui
Updated on 29-Aug-2020 07:36:13

374 Views

The column names in an R data frame are an important part of the data because by reading the column names any viewer is likely to understand the theoretical background behind it. If that name is not appropriate then we might want to change it. While using the aggregate function to calculate mean or any other statistical summary, it is possible to change that name with another name by defining the new name with list.ExampleConsider the below data frame −set.seed(1) x1

How to create a vector with zero values in R?

Nizamuddin Siddiqui
Updated on 20-Aug-2020 05:49:42

412 Views

In data analysis, sometimes we need to use zeros for certain calculations, either to nullify the effect of a variable or for some other purpose based on the objective of the analysis. To deal with such type of situations, we need a zero value or a vector of zeros. There are many ways to create a vector with zeros in R. The important thing is the length of the vector.Examples> x1 x1 [1] 0 0 0 0 0 0 0 0 0 0 > x2 x2 [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 ... Read More

Advertisements