Found 2038 Articles for R Programming

How to create a dummy variable in R?

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

946 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

How to convert binary variable to 0/1 format in an R data frame?

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

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

How to add an extra point to scatterplot using ggplot2 in R?

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

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

How to 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

How to find the rate of return for a vector values in R?

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

95 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

How to remove spaces at the end in string vectors in R?

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

357 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

How to create an integer64 vector in R?

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

743 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

How to use pnorm function on data frame columns in R?

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

563 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

How to define the breaks for a histogram using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:17:18

6K+ Views

To manually define the breaks for a histogram using ggplot2, we can use breaks argument in the geom_histogram function. While creating the number of breaks we must be careful about the starting point and the difference between values for breaks. This will define the number of bars for histogram so it should be taken seriously and should be according to the distribution of the data.Consider the below data frame −Example Live Demox

How to fill NA values with previous values in an R data frame column?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:10:49

1K+ Views

To fill NA values with next and previous values, we can use na.locf function of zoo package with fromLast = TRUE. This is the situation of a column as shown below −x 0 NA NA 1 1 NA 0 1The output after filling NA values with next and previous values will be −x 0 0 0 1 1 1 0 1Consider the below data frame −Example Live Demox1

Advertisements