Programming Articles - Page 1430 of 3366

How to create combinations of 0 and 1 with fixed number of 1s in each row in an R data frame?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:48:13

474 Views

To create the combinations of 0 and 1 data frame, we can use expand.grid function along with the rep function. If we want to create combination of 0 and 1 with fixed number of 1’s in each row then rowSums functions can be used with the appropriate sum value. For example, to have rows containing less than three 1’s, the rowSums will be extracted from grid for the same.Example1 Live DemoFirst

How to replace NA with 0 and other values to 1 in an R data frame column?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:45:30

806 Views

Sometimes we want to convert a column of an R data frame to binary column using 0 and 1, it is especially done in situations where we have some NAs in the column of the data frame and the other values can be converted to 1 due to some characteristics. To replace NA with 0 and other values to 1, we can use ifelse function.Example1 Live DemoConsider the below data frame −x1

How to convert a sparse matrix into a matrix in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:45:46

3K+ Views

A sparse matrix is a type of matrix that has most of the elements equal to zero but there is no restriction for the number of zero elements. As a general criterion the number of non-zero elements are expected to be equal to the number of rows or number of columns. To convert a sparse matrix into a matrix R, we can use as.matrix function with the sparse matrix object name.Example1 Live Demolibrary(Matrix) i

How to create a sparse matrix in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:45:57

5K+ Views

A sparse matrix is a type of matrix that has most of the elements equal to zero but there is no restriction for the number of zero elements. As a general criterion the number of non−zero elements are expected to be equal to the number of rows or number of columns. To create a sparse matrix in R, we can use sparseMatrix function of Matrix package.Example1 Live DemoLoading Matrix package and creating a sparse matrix −library(Matrix) i

How to create the plots arranged in a list that were generated using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:45:22

521 Views

If we have two plots generated by using ggplot2 and arranged in a list then we can create them using ggarrange function. For example, if we have two objects p1 and p2 that are stored in the list called named as LIST then those plots can be created in the plot window by using the command ggarrange(plotlist=LIST,widths=c(2,1),labels=c("Scatter","Hist"))ExampleConsider the below data frame − Live Demoset.seed(21) x

What is the use of set.seed in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:43:35

914 Views

The set.seed helps to create the replicate of the random generation. If the name of the object changes that does not mean the replication will be changed but if we change the position then it will. Here, in the below example x4 in the first random generation and the x_4 in the second random generation with the same set.seed are same but x4 and x4 in both are different.Example Live Demoset.seed(101) x1

How to check the least number of arguments a function expects in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:43:21

194 Views

To find the least number of arguments a function expects while using in R can be done with the help of the syntax: length(formals(“function_name”)). For example, to find the number of arguments expected by mutate function of dplyr package can be calculated by using the command length(formals(mutate)) but we need to make sure that the package is loaded.Examples Live Demolibrary(ggplot2) length(formals(ggplot))Output[1] 4 length(formals(boxplot)) [1] 2 length(formals(qnorm)) [1] 5 length(formals(rnorm)) [1] 3 length(formals(rpois)) [1] 2 length(formals(runif)) [1] 3 length(formals(punif)) [1] 5 length(formals(plot)) [1] 3 length(formals(pbinom)) [1] 5 length(formals(qbinom)) [1] 5 length(formals(hist)) [1] 2 length(formals(data)) [1] 7 length(formals(matrix)) [1] 5 length(formals(list)) [1] 0 ... Read More

How to find the extremes of a data frame column in R if infinity exists in the column?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:43:10

284 Views

To find the extremes of a data frame column that is numeric can be done with the help of min and max function but if we want to get the same in single line code then range function can be used. If there are some infinity values in the column then range.default function will be used as shown in the below example.Example Live DemoConsider the below data frame −set.seed(214) x1

How to display star for significance in base R boxplot?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:42:57

3K+ Views

To display the star for significance in a base R boxplot, we can use text function. The text function will be helpful in defining the star sign (that is asterisk or *). If the significance is high then three stars are used and the significance is low then a single star is used. We need to use the appropriate position using x and y values.Example Live DemoConsider the below data frame −x

How to create the stacked bar plot using ggplot2 in R with labels on the plot?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:42:38

3K+ Views

The creation of stacked bar plot using ggplot2 can be done with the help of position="stack" argument inside geom_bar function. If we want to create the stacked bar plot then geom_text function will be used with the same position argument and the aes to define the labels as shown in the below example.Example Live DemoConsider the mtcars data −head(mtcars)Outputmpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1Example Live Demodf

Advertisements