Truncate Numerical Vector to Specified Decimal Places in R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:12:15

4K+ Views

The truncation means removing the number of decimal places but not rounding. For example, if we have a value 5.1742145 then truncating to one decimal place will be 5.1 and rounding will be 5.2. In R, we can do this by using trunc function as shown in the below examples.Example Live Demox1

Find Maximum of Each Row in an R Data Frame

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:09:58

7K+ Views

Sometimes we need maximum values, it helps us to identify which case or subject occurs at the greatest point hence we can understand the limit for the sample or population under study. If we want to find the maximum of values two or more columns for each row in an R data frame then pmax function can be used.ExampleConsider the below data frame − Live Demoset.seed(1997) x1

Create Matrix Without Column and Row Indices in R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:07:17

536 Views

To create a matrix without column and row indices, we first need to create the matrix and then prmatrix function can be used to convert that matrix without column and row indices but we would need to provide the number of rows inside the function. For example, if we have a matrix M that contains 5 rows and 5 columns then it can be converted to a matrix without column and row indices using prmatrix(M,rowlab=rep("",5),collab=rep("",5)).Example Live DemoM1

Create a Plot of Poisson Distribution in R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:04:31

3K+ Views

The Poisson distribution is a discrete distribution that has only one parameter named as lambda and it is the rate parameter. The rate parameter is defined as the number of events that occur in a fixed time interval. To create a plot of Poisson distribution in R, we can use the plot function with the density of the Poisson distribution using dpois function.Example Live Demoplot(dpois(x=1:50,lambda=3))OutputExample Live Demoplot(dpois(x=1:50,lambda=3),type="l")OutputExample Live Demoplot(dpois(x=1:50,lambda=3),type="b")Output

Create a Plot in Base R Without Margins

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:02:02

645 Views

To create a plot without margins, we first need to define that margin in a way that the plot created after that will not have margins and this can be done by using par function. We would need to pass mar function within par function as par(mar=c(0,0,0,0)).Example Live Demopar(mar=c(0,0,0,0)) plot(1:10)OutputExample Live Demobarplot(1:10)Output

Use of pheatmap Function in R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:00:16

128 Views

The pheatmap function is used to create clustered heatmaps but we can change the aesthetics of the plot by using color argument which is one of the main functionalities of pheatmap function. There are many other arguments that differentiate pheatmap from heatmap function.Examplelibrary(pheatmap) M1

Create Table of Sums for Two Categorical Variables in R Data Frame

Nizamuddin Siddiqui
Updated on 07-Dec-2020 04:57:36

508 Views

If we want to create a table of sums of a discrete variable for two categorical variables then xtabs function can be used. The output will be a contingency table or cross tabulation table which looks like a matrix. For example, if we have a data frame df with two categorical column x and y and a count column freq then the table of sums for freq can be created by using xtabs(freq~x+y,data=df1).ExampleConsider the below data frame − Live Demox1

Create Rectangle Inside Boxplot in Base R

Nizamuddin Siddiqui
Updated on 07-Dec-2020 04:52:59

264 Views

To create a rectangle inside boxplot in base R, we can use rect function after creating the boxplot. The rect function has many arguments but for the creation of a rectangle only first four are necessary and these are defined as xleft - a vector (or scalar) of left x positions, ybottom - a vector (or scalar) of bottom y positions, xright - a vector (or scalar) of right x positions and ytop - a vector (or scalar) of top y positions.Example Live Demox

Find Mean of Three-Dimensional Array in R

Nizamuddin Siddiqui
Updated on 05-Dec-2020 13:27:56

1K+ Views

A three-dimensional array can have matrices of different size and they are not necessarily to be square or rectangular. Also, all the elements in an array are of same data type. If we want to find the mean of a three-dimensional array then apply function can be used where we need to refer the columns and rows of the array elements using combination function.Example Live DemoA1 apply(A1,c(1,2),mean) [,1] [,2] [1,] 5 7 [2,] 6 8ExampleA2

Create Serial Number Column in R Data Frame

Nizamuddin Siddiqui
Updated on 05-Dec-2020 13:25:08

1K+ Views

A group column in an R data frame have duplicate values and we might want to create a column with the serial number based on the values such as first value of the first group gets 1, the same value gets 2 when occurred second time in the same column and so on. This can be done by using ave function as shown in the below examples.ExampleConsider the below data frame − Live DemoS.No

Advertisements