Found 2038 Articles for R Programming

How to create plotly bar chart with values on top of bars in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:42:02

856 Views

To create plotly bar chart with values on top of bars in R, we can follow the below steps −First of all, create two vectors one having categories and one counts and create the bar chart using plotly.Then, create the same bar chart with add_text function of plotly package.Create the vectors and plotly bar chartUsing c function to create the vectors and plotly function with layout function to create the bar chart with axes titles −x

How to find the sum of numbers stored in character vector separated with a special character in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:38:59

360 Views

To find the sum of numbers stored in character vector separated with a special character in R, we can follow the below steps −First of all, create a vector having numbers stored in character vector separated with a special character.Then, use sapply and strsplit function to find the sum of numbers.Example1Create a vector with numbers stored in character vector separated with hyphen − Live Demo> x1 x1On executing, the above script generates the below output(this output will vary on your system due to randomization) − [1] "11-2-15-19-10"   "11-2-15-19-10"    "13-12-75-29-18"  "15-25-52-91-10"  [5] "15-25-52-91-10"  "17-27-55-91-100"  "13-12-75-29-18"  "15-25-52-91-10"  [9] "13-12-75-29-18"  "10-4-5-19-10"     ... Read More

How to divide the row values by row mean in R data frame?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:36:41

973 Views

To divide the row values by row mean in R data frame, we can follow the below steps −First of all, create a data frame.Then, use apply function to divide the data frame row values by row mean.Create the data frameLet's create a data frame as shown below − Live Demo> x y df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −x y 1 42 23 2 2 37 3 6 42 4 34 28 5 50 19 6 41 46 7 35 38 8 33 7 9 32 9 ... Read More

How to create a horizontal line in ggplot2 graph with larger width in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:34:47

1K+ Views

To create a horizontal line in ggplot2 graph with larger width in R, we can follow the below steps −First of all, create a data frame.Then, create a plot using ggplot2.After that, create the same plot with geom_hline function having horizontal line defined with yintercept and its width defined with size argumentCreate the data frameLet's create a data frame as shown below − Live Demo> x y df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −    x y 1   1 43 2  21 17 3  36 9 4 ... Read More

How to create a dashed horizontal line in a ggplot2 graph in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:32:59

8K+ Views

To create a dashed horizontal line in a ggplot2 graph in R, we can follow the below steps −First of all, create a data frame.Then, create a plot using ggplot2.After that, create the same plot with geom_hline function having horizontal line defined with y intercept and its type defined with line type argument.Create the data frameLet's create a data frame as shown below − Live Demo> x y df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −         x       y 1 -0.2622735 0.050784727 2 -0.9453493 ... Read More

How to create a histogram with Y-axis values as count using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:31:24

3K+ Views

To create a histogram with Y-axis values as count using ggplot2 in R, we can follow the below steps −First of all, create a data frame.Then, use geom_histogram function of ggplot2 package with aes to create the histogram with Y-axis values as count.Create the data frameLet's create a data frame as shown below − Live Demo> df head(df, 20)On executing, the above script generates the below output(this output will vary on your system due to randomization) −          x 1    -0.008015477 2    -0.981227322 3     1.144050354 4     0.207177231 5     0.179782914 6 ... Read More

How to check whether the difference between previous and current value in a column of an R data frame is 1?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:30:16

337 Views

To check whether the difference between previous and current value in a column of an R data frame is 1, we can follow the below steps −First of all, create a data frame.Then, create a custom function for the difference between previous and current value.Now, use the function to check the difference.Example1Create the data frameLet's create a data frame as shown below − Live Demo> x df1 df1On executing, the above script generates the below output(this output will vary on your system due to randomization) −    x 1   1 2   2 3   3 4   4 5 ... Read More

How to create an ID column for the combination of values in multiple columns in R data frame?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:27:52

989 Views

To create an ID column for the combination of values in multiple columns in R data frame, we can follow the below steps −First of all, create a data frame.Then, create an ID column using as.numeric, as.factor and with function for the combination of values in columns of the data frame.Create the data frameLet's create a data frame as shown below − Live Demo> x1 x2 df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −  x1 x2 1  0  1 2  0  1 3  0  1 4  2  2 5 ... Read More

How to divide the data.table object rows by row standard deviation in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:26:17

150 Views

To divide the row values by row standard deviation in R’s data.table object, we can follow the below steps −First of all, create a data.table object.Then, use apply function to divide the data.table object row values by row standard deviation.Create the data.table objectLet’s create a data.table object as shown below −> library(data.table) > x y DT DTOn executing, the above script generates the below output(this output will vary on your system due to randomization) −      x y 1:   45 18 2:    3 99 3:   74 96 4:   67 58 5:   82 24 6: ... Read More

How to add a regression line to a plot in base R if intercept and slope are given?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:24:46

2K+ Views

To add a regression line to a plot in base R if intercept and slope are given, we can follow the below steps −First of all, create two vectors and the scatterplot between them.Then, use abline function to create the regression line with intercept and slope given by a and b respectively.Create the vectors and scatterplotUse plot functions to create scatterplot between two random vectors x and y − Live Demo> x y plot(x, y)OutputAdd regression line with given intercept and slopeExampleUsing abline function to add the regression line to the scatterplot with given intercept a = 0.51 and slope = ... Read More

Advertisements