Programming Articles - Page 1145 of 3363

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

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

421 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

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

245 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

How to divide data.table object rows by row median in R?

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

213 Views

To divide the row values by row median 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 median.Create the data.table objectLet's create a data frame as shown below −> library(data.table) > x y z DT DTOn executing, the above script generates the below output(this output will vary on your system due to randomization) −    x y  z 1:  5 3  2 2:  1 3  7 3:  6 0 13 4:  6 2 10 5:  8 1  9 6:  9 ... Read More

How to divide data.table object rows by maximum value in each row excluding 0 in R?

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

194 Views

To divide the row values by row maximum excluding 0 in R’s data.table object, we can follow the below steps −First of all, create a data.table object.Then, use apply function and if else function to divide the data.table object row values by row maximum excluding 0.Create the data.table objectLet’s create a data.table object as shown below −> library(data.table) > x y z DT DTOn executing, the above script generates the below output(this output will vary on your system due to randomization) −    x y z 1:  2 0 3 2:  2 3 2 3:  0 1 3 4:  0 ... Read More

How to replace zero with first non-zero occurring at the next position in an R data frame column?

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

302 Views

To replace zero with first non-zero occurring at the next position in an R data frame column, we can follow the below steps −First of all, create a data frame.Then, use na.locf function of zoo package to replace zero with first non-zero occurring at the next position in the data frame column.Create the data frameLet's create a data frame as shown below − Live Demo> x df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −   x 1  5 2  4 3  2 4  2 5  5 6  4 7  1 ... Read More

How to remove scientific notation form base R plot?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 10:11:14

7K+ Views

To remove scientific notation form base R plot, we can follow the below steps −First of all, create a vector and its plot using plot function.Then, use options(scipen=999) to remove scientific notation from the plot.Create the vector and plotUsing sample function to create a vector and plot the vector with plot function − Live Demo> x plot(x)OutputRemove the scientific notation from the plotUse options(scipen=999) function and again create the same plot − Live Demo> x options(scipen=999) > plot(x)Output

How to find the sum of numerical columns based on the combination of values incategorical columns in R data frame?

Nizamuddin Siddiqui
Updated on 03-Aug-2021 09:19:39

191 Views

To find the sum of numerical columns based on the combination of values in categorical columns in R data frame, we can follow the below steps −First of all, create a data frame.Then, find the sum of numerical columns based on the combination of values in categorical columns by using recast function of reshape2 package with sum function.Create the data frameExampleLet's create a data frame as shown below − Live Demo> x1 x2 x3 x4 f1 f2 df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −Output  x1 x2 x3 x4 ... Read More

Advertisements