Change Title of a Graph to Italic in R Using Plot Function

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:59:46

315 Views

If a graph is created by specifying main title of the plot using the plot function then the default font is plain text. We might want to change the style of the font to italic so that the title gets a little more attraction of the viewers. This can be done by using font.main argument with plot function. The value 4 of font.main refers to the bold italic font and if we want to make it bold then we can use the value 3.Consider the below vectors and create the scatterplot between the two with title of the plot −ExamplexRead More

Round Correlation Values in R to Zero Decimal Places

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:57:20

4K+ Views

To find the correlation matrix, we simply need to use cor function with the data frame object name. For example, if we have a data frame named as df then the correlation matrix can be found by using cor(df). But the result will have too many decimal places to represent the correlation. If we want to avoid the values after decimal places, we can use round function.Consider the mtcars data in base R −Example Live Demodata(mtcars) cor(mtcars)Output      mpg           cyl        disp        hp         drat     ... Read More

Find Root Mean Square of a Vector in R

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:40:27

844 Views

To find the root mean square of a vector we can find the mean of the squared values then take the square root of the resulting vector. This can be done in a single and very short line of code. For example, if we have a vector x and we want to find the root mean square of this vector then it can be done as sqrt(mean(x^2)).Example Live Demox1

Create Blue or Red Colored Boxplots in R using ggplot2

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:38:22

253 Views

The default color of boxplot area in R using ggplot2 is white but we might want to change that color to something more attracting, for example blue or red. To do this purpose, we can use geom_boxplot function of ggplot2 package with fill argument by passing the color names.Consider the below data frame −Example Live Demoset.seed(1321) v1

Fill NA with Last Observation in R Data Frame

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:33:35

502 Views

There are multiple ways to fill missing values in data analysis and one of the ways is filling them with the previous value in the same column of the data frame. For example, if we have a column x in data frame df and this columns x contains some NA values then we can fill them with the values in the upper row. This can be done with the help of na.locf function of zoo package.Consider the below data frame −Example Live Demoset.seed(477) x

Create Subset of R Data Frame with Complete Cases of a Particular Column

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:25:49

374 Views

If we have missing values in a data frame then all the values cannot be considered complete cases and we might want to extract only values that are complete. We might want extract the complete cases for a particular column only. Therefore, we can use negation of is.na for the column of the data frame that we want to subset.Consider the below data frame −Example Live Demoset.seed(123) x

Create Random Sample of Rows for a Specific Column Value in R

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:10:04

975 Views

Random sampling is an important part of data analysis, mostly we need to create a random sample based on rows instead of columns because rows represent the cases. To create a random sample of some percentage of rows for a particular value of a column from an R data frame we can use sample function with which function.Consider the below data frame −Example Live Demoset.seed(887) grp

Create Barplot with One Bar Having Different Color in R

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:06:57

578 Views

A bar plot represents discrete data and the bars in the bar plot are usually of same color but we might want to highlight a particular bar based on the characteristics of the data or the objective of the analysis project. For example, if a particular bar represents highly severe situation or highly unimportant situation then we can change the color that particular bar so that people can easily point out that bar.Consider the below data frame −Example Live Demox

Add String Before Each Numeric Value in R Data Frame Column

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:03:10

5K+ Views

Sometimes the unique identity column of a data frame is not recorded as intended, it contains only numeric values that does not solve the data characteristic purpose. Therefore, we might want to add a string before those numeric values to make the data more sensible for viewers and analysts. This can be easily done with the help of gsub function.Consider the below data frame −Example Live Demoset.seed(111) x1

Excluding Extreme Elements from Average Calculation in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:21:22

240 Views

We are required to write a JavaScript function that takes in an array of Number. Then the function should return the average of its elements excluding the smallest and largest Number.ExampleThe code for this will be −const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const findExcludedAverage = arr => {    const creds = arr.reduce((acc, val) => {       let { min, max, sum } = acc;       sum += val;       if(val > max){          max = val;       };       if(val < min){          min = val;       };       return { min, max, sum };    }, {       min: Infinity,       max: -Infinity,       sum: 0    });    const { max, min, sum } = creds;    return (sum - min - max) / (arr.length / 2); }; console.log(findExcludedAverage(arr));OutputThe output in the console −8.666666666666666

Advertisements