Create Boxplot Using Mean and Standard Deviation in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:02:00

9K+ Views

The main statistical parameters that are used to create a boxplot are mean and standard deviation but in general, the boxplot is created with the whole data instead of these values. If we don’t have whole data but mean and standard deviation are available then the boxplot can be created by finding all the limits of a boxplot using mean as a measure of central tendency.ExampleConsider the below data frame:Live Demo> df dfOutputmean sd Category 1 24 1.1 A 2 25 2.1 B 3 27 1.5 C 4 24 1.8 DLoading ggplot2 package and creating the boxplot of each category ... Read More

Create 3D Array from Data Frame in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:00:35

2K+ Views

A 3D-array is a 3-dimensional array and it is actually a collection of 2D arrays. We can create a 3D-array of a data frame in R by using simplify2array function, this function will break the data frame into arrays that will form a 3D-array.Example1Consider the below data frame:Live Demo> set.seed(254) > x y z a b c df1 df1Outputx y z a b c 1 0 4 6 9 5 5 2 0 5 1 4 2 1 3 0 6 1 4 5 6 4 1 6 3 5 4 12 5 1 9 8 6 6 11 6 ... Read More

Create Frequency Table of a String Vector in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:58:44

1K+ Views

To create a frequency table of a string vector, we just need to use table function. For example, if we have a vector x that contains randomly sampled 100 values of first five English alphabets then the table of vector x can be created by using table(x). This will generate a table along with the name of the vector.Example1Live Demo> x1 x1Output[1] "d" "d" "a" "c" "a" "a" "c" "a" "d" "c" "a" "d" "d" "b" "c" "a" "b" "c" "d" [20] "b"Example> table(x1)Outputx1 a b c d 6 3 5 6Example2Live Demo> x2 x2Output[1] "w" "j" "p" "y" "r" ... Read More

Create QR Code in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:55:50

2K+ Views

A QR code is a barcode which is used to read the information about the object on which it is printed. This helps us to detect the useful information relevant to the object so that we can proceed with the next step depending on the operation. In R, we can create QR code by using qrcode_gen function of qrcode package.Example1Loading qrcode package:> library(qrcode)Creating a QR code for tutorialspoint:> qrcode_gen('www.tutorialspoint.com')Output:Example2> qrcode_gen('www.tutorix.com')Output:Example3> qrcode_gen('www.r-project.org')Output:

Create Duplicate Column in R Data Frame with Different Name

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:53:36

2K+ Views

The easiest way to create a duplicate column in an R data frame is setting the new column with $ sign and if we want to have a different name then we can simply pass a new name. For example, if we have a data frame df that contains a column x and we want to have a new column x1 having same values as in x then it can be done as df$x1 set.seed(254) > x y z a b c df dfOutputx y z a b c 1 A 0.8709244 9 0.072625990 5.125432 26.84561 2 B 1.7993156 3 ... Read More

Replace Missing Values with Linear Interpolation in R Vector

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:51:59

2K+ Views

The linear interpolation is a method of fitting a curve using linear polynomials and it helps us to create a new data points but these points lie within the range of the original values for which the linear interpolation is done. Sometimes these values may go a little far from the original values but not too far. In R, if we have some missing values then na.approx function of zoo package can be used to replace the NA with linear interpolation method.Example1Loading zoo package:Live Demo> library(zoo) > x1 x1Output[1] 2 2 2 5 2 2 5 NA 2 5Replacing NA ... Read More

Add a New Column to a Matrix in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:50:01

2K+ Views

A data collection process is one of the initial and very important tasks in a data analysis project and sometimes we miss something. Therefore, we need to collect that data later and add it to the originally collected data. This mistake can be done for matrix data as well, hence we might need to add a new column to original matrix and this can be done by using cbind function.Example1Live Demo> M1 M1Output [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 6 11 16 21 [2, ] 2 7 12 17 22 [3, ... Read More

Add Picture to Plot in Base R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:44:05

5K+ Views

To add a picture to a plot in base R, we first need to read the picture in the appropriate format and then rasterImage function can be used. The most commonly used format for picture in R is PNG. A picture in PNG format can be added to a plot by supplying the values in the plot where we want to add the picture.ExampleLoading png package:> library(png)Reading png file:> Picture plot(1:10,ty="n")Output:Adding the picture in png file to the above plot:Example> rasterImage(Picture,3,3,7,7)Output:Example> plot(1:10,ty="n") > rasterImage(Picture,5,5,7,7)Output:

Find Sum Based on Categorical Variable in R Data Frame

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:41:20

2K+ Views

Finding group-wise mean is a common thing but if we go for step-by-step analysis then sum of values are also required when we have a categorical variable in our data set. This can be easily done with the help of group_by and summarise_each function of dplyr package.ExampleConsider the below data frame:Live Demo> Group Salary Emp EmpOutputGroup Salary 1 D 28256 2 B 31092 3 A 23147 4 C 28209 5 B 37676 6 C 33374 7 D 44864 8 B 40152 9 A 25843 10 A 40946 11 D 23321 12 A 42854 13 C 36960 14 A 35285 15 ... Read More

Find Mean of List Elements in R Without Unlisting

Nizamuddin Siddiqui
Updated on 19-Nov-2020 07:38:02

322 Views

Most of the times, unlisting is used to find the mean of list elements but we can also use double-square brackets for the same purpose. The double-square brackets are basically used to access the values in the elements of the list, thus mean function works with those values directly. Look at the below example to understand how it works.ExampleConsider the below list:Live Demo> x xOutput[1] 3 3 3 5 3 1 4 7 5 4 5 9 9 7 4 3 6 2 4 3 3 4 7 4 4 [26] 4 5 3 4 4 3 5 7 2 ... Read More

Advertisements