Found 2038 Articles for R Programming

How to create a combination of pairs from a vector in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:09:34

2K+ Views

To create a combination of pairs, we can use the combn function. This function will be helpful to use the vector length and the value 2 that represents a pair to create the combinations. Although, this is enough but we can transpose the output to get a better−looking output.Example1 Live Demox1

How to check for duplicates in data.table object in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:09:55

457 Views

The checking for duplicates in a data.table object can be easily done with the help of delta ($) operator that is used to access the columns and the duplicated function. For example, if a data.table object DT contains a column x then to check the duplicates in x, we can use the command duplicated(DT$x).Example1Loading data.table object and creating an object −library(data.table) set.seed(141) x

How to multiply only one column in an R data frame with a number?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 11:06:03

4K+ Views

To multiply only one column with a number, we can simply use the multiplication operator * but need to replace the original column with the new values. For example, if we have a data frame called df that contains three columns x1, x2, and x3, and we want to multiply the second column x2 with 2 then it can be done as df$x2

How to extract axes labels for the plot drawn using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:38:19

412 Views

When we create a plot using ggplot2, the axes labels are automatically generated for both the axes. We might want to use those axes labels for report writing or some other purpose, therefore, extraction of those labels for a plot created by using ggplot2 package can be found by using the ggplot_build function as shown in the below example but we need to save the plot in an object.Consider the below data frame −Example Live Demox

How to change the default type of points for scatterplot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:35:49

187 Views

To change the defaults for ggplot2 geoms, we need to use update_geom_defaults function. If we want to change the shape of points for scatterplot, we can use the below syntax −update_geom_defaults("point",list(shape=”point_shape_number”))The point shape number ranges from 0 to 25. We can change that according to our need.Consider the below data frame −Example Live Demox

How to create boxplot for list elements using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:33:40

571 Views

If list elements are of equal size having numerical values then the boxplot for each element can be created by using ggplot2 but before creating the boxplot we would need to convert the list to a data frame. The conversion of list elements to a data frame will be done by unlisting of elements and creating two columns, one for categories and one for response variable. Check out the below example to understand how it works.Consider the below list −Example Live DemoList

How to create boxplot for a list object in base R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:31:26

1K+ Views

A list object can contain multiple elements of data, also the size of the data may vary. If a list object has numerical vectors then the boxplot for each of the elements can be created simply by using boxplot function. For example, if we have a list object called LIST that contains five numerical vectors then the boxplot for each of the vectors can be created by using the command boxplot(LIST)Example Live DemoList

How to change the first value for each group in data.table object in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:29:14

520 Views

To change the first value for each group in data.table object, we can use single square brackets for accessing and changing the value to desired value. For example, if we have a data.table object called DT that contains a group column defined by Class and a numerical column defined by Response then the first value of Response for each Class can be set to say 5 by using the command DT[,Response:=c(2,Response[-]),by=Class]Consider the below data.table object −Examplelibrary(data.table) Group

How to display text in base R plot with 180 degrees rotation?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:25:39

1K+ Views

To display text in base R plot with 180 degrees rotation, we can use text function. We would need to define both the axes values for the position where we need the text to be rotated inside the plot. For the rotation of the text, srt argument will be used and set to the value equals to 270. Check out the below example to understand how it works.Examplex

How to create a plot of binomial distribution in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 06:24:09

1K+ Views

A binomial distribution is based on the distribution of success and failure, the other two parameters of binomial distribution are the sample size and the probability of success. To create a plot of binomial distribution, we first need to define the density of the binomial distribution using dbinom function. The plotting can be done by using plot function with success and the density as shown in the below examples.Examplex

Advertisements