Change Partial Plot Background Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:08:00

1K+ Views

To change the partial plot background, we can create a rectangle using geom_rect function by defining both the axes values and alpha for transparency, the color will be changed by using fill argument. The value of alpha will completely hide the grey background and we can play with its value based on our need.Example Live DemoConsider the below data frame −x

Convert NaN to NA in an R Data Frame

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:54:19

11K+ Views

To convert NaN to NA in an R data frame, we can set the NaN values to NA values by using single square brackets. Firstly, we would need to access the column that contains NaN values then NaN values will be accessed using is.nan then we can set those values to NA as shown in the below examples.Consider the below data frame −Example Live Demox1

Create a Dummy Variable in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:48:02

1K+ Views

A dummy variable is a type of variable that takes a value of 1 if the value for which the dummy variable is created exists in the data frame, otherwise it takes 0. Therefore, if we have a one binary variable in a data frame then there will be two dummy variables for the same. To create a dummy variable, we can use model.matrix function as shown in the below examples.Consider the below data frame −Example Live DemoTemp

Convert Binary Variable to 0-1 Format in R Data Frame

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:42:56

8K+ Views

A binary variable is a type of variable that can take only two possible values like gender that has two categories male and female, citizenship of a country with two categories as yes and no, etc. If the binary variable is not in 0/1 format then it can be converted with the help of ifelse function. Check out the below examples to understand how it works.Consider the below data frame −Example Live DemoTemp

Add Extra Point to Scatterplot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:40:20

14K+ Views

To add an extra point to scatterplot using ggplot2, we can still use geom_point function. We just need to use aes function for quoting with new values for the variables, also we can change the color of this point using colour argument. The display of an extra point will help us to distinguish between a threshold/new value and remaining values.Consider the below data frame −Example Live Demox

Extract Words from a String Vector in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:38:56

3K+ Views

To extract words from a string vector, we can use word function of stringr package. For example, if we have a vector called x that contains 100 words then first 20 words can be extracted by using the command word(x,start=1,end=20,sep=fixed(" ")). If we want to start at any other word then starting value will be changed accordingly.Example Live Demox

Find Rate of Return for Vector Values in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:36:15

156 Views

To find the rate of return for a vector values, we can use the formula for rate of return. For example, if we have a vector called x then the rate of return can be calculated by using the syntax diff(x)/x[-length(x)]. The output will be in decimal form and if we want to convert it to percentage then the output needs to be multiplied with 100, we can also input the same within the formula as (diff(x)/x[-length(x)])*100.Example Live Demox1

Remove Spaces at the End in String Vectors in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:27:44

453 Views

Sometimes values in a string vector has an extra space at the end, this might happen while typing the values or due to some other manual errors. To remove spaces at the end in string vectors, we can use gsub function. For example, if we have a vector called x that contains string values with spaces at the end then the removal of values can be done by using the command gsub(" $","",x,perl=T)Example Live Demox1

Create Integer64 Vector in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:25:44

1K+ Views

The integer64 vector contains vector values that represents signed integers with values that range from negative 9,223,372,036,854,775,808 to positive 9,223,372,036,854,775,807. To create an integer64 vector, we can use as.integer64 function of bit64 package. The difference between integer64 vector and others is that a large number of values can be stored in the vector.Examplelibrary(bit64) x1

Use pnorm Function on Data Frame Columns in R

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:23:19

697 Views

The pnorm function is used to find the probability for a normally distributed random variable. Probabilities such as less than mean, greater than mean, or probability between left- and right-hand side of the mean. If we want to use pnorm function on data frame columns then apply function can help us.Consider the below data frame −Example Live Demox1

Advertisements