Found 2038 Articles for R Programming

How to create a bar chart using plotly in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:19:13

297 Views

Plotly in R is a package specifically designed to create highly-interactive and publication-quality charts. The chart can be created by using plot_ly function of the package and there are three main arguments of plot_ly defined as x, y, and type, where x refers to the X-axis, y refers to the Y-axis and type refers to the chart type but the axes values are stored in a data frame or itself a shared.ExampleLoading plotly package:> library(plotly)Consider the below data frame:Live Demo> x count df dfOutputx count 1 A 321 2 B 324 3 C 320 4 D 328Creating the bar plot ... Read More

How to create a covariance matrix in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:15:11

549 Views

To create a covariance matrix, we first need to find the correlation matrix and a vector of standard deviations is also required. The correlation matrix can be found by using cor function with matrix object. For example, if we have matrix M then the correlation matrix can be found as cor(M). Now we can use this matrix to find the covariance matrix but we should make sure that we have the vector of standard deviations.Example1Live Demo> M1 M1Output [, 1] [, 2] [, 3] ... Read More

Why scale_fill_manual does not fill the bar with colors created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 05:05:40

2K+ Views

We can manually fill the color of bars in a bar plot which is created by using ggplot2 but for this we would be needing color aesthetics already used for the bars. That means we need to use filling of bars inside aes of ggplot2 and then the colors can be mentioned inside the scale_fill_manual function.ExampleConsider the below data frame:Live Demo> x count df dfOutputx count 1 A 321 2 B 324 3 C 320 4 D 328Loading ggplot2 package and creating bar plot:Example> library(ggplot2) > ggplot(df, aes(x, count))+geom_bar(stat="identity")Output:Creating the bar plot by using scale_fill_manual:Example> ggplot(df, aes(x, count))+geom_bar(stat="identity")+scale_fill_manual(values=c("blue", "green", "grey", ... Read More

How to find the position of a data frame’s column value based on a column value of another data frame in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:55:24

543 Views

In data analysis, we encounter many problems with a lot of variations among them. One such problem is we have some information in a place that needs to be checked through a different place and these places can be data frames. Therefore, we need to find the position of a data frame’s column value based on a column value of another data frame. In R, we can easily do it with the help of which function.Example Live DemoConsider the below data frame −set.seed(12121) x1

How to find the distance among matrix values in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:54:21

76 Views

Finding the distance among matrix values means that we want to find the distance matrix and it can be directly found by using dist function with the matrix name. For example, suppose we have a matrix of size 5x5 named as M then the distance matrix can be calculated as dist(M).Example1M1

How to plot the X-axis labels on the upper-side of the plot in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:52:48

669 Views

By default, the X−axis labels are plotted on the bottom−axis of the plot and that is referred to as the first axis of axis 1, the second axis is the axis on the left−side, the third axis is the axis on the upper side of the plot, and the fourth axis is on the right−side of the plot. If we want to plot the X−axis labels on the upper−side of the plot then we can use xaxt="n" inside the plot function then define axis for upper−side using axis(3) with semi−colon.Example1xRead More

How to add a data frame inside a list in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:48:56

2K+ Views

A list may contain many objects such as vector, matrix, data frame, list etc. In this way, we can have access to all the necessary objects at the same time. If we want to add a data frame inside a list then we can use the length of the list. For example, if we have a list defined as List and we want to add a data frame df to the List then it can be added as follows −List[[length(List)+1]]

How to perform group-wise linear regression for a data frame in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:47:20

5K+ Views

The group−wise linear regression means creating regression model for group levels. For example, if we have a dependent variable y and the independent variable x also a grouping variable G that divides the combination of x and y into multiple groups then we can create a linear regression model for each of the group. In R, we can convert data frame to data.table object, this will help us to create the regression models easily.Example Live DemoConsider the below data frame −G1

How to create a vector of data frame values by rows in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:45:42

5K+ Views

To create a vector of data frame values by rows we can use c function after transposing the data frame with t. For example, if we have a data frame df that contains many columns then the df values can be transformed into a vector by using c(t(df)), this will print the values of the data frame row by row.Example1 Live Demoset.seed(798) x1

How to create a character vector from data frame values in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:44:12

3K+ Views

To create a character vector in R we can enclose the vector values in double quotation marks but if we want to use a data frame values to create a character vector then as.character function can be used. For example, if we have a data frame df then all the values in the df can form a character vector using as.character(df[]).Example1 Live Demox1

Advertisements