Remove Rows for Categorical Columns with Limited Duplicates in R Data Frame

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:55:16

380 Views

In Data Analysis, we sometimes decide the size of the data or sample size based on our thoughts and this might result in removing some part of the data. One such thing could be removing three or less duplicate combinations of categorical columns and it can be done with the help of filter function of dplyr package by grouping with group_by function.Example1 Live DemoConsider the below data frame −set.seed(121) x1

Find Percentage of Missing Values in R Data Frame

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

1K+ Views

To find the percentage of missing values in an R data frame, we can use sum function with the prod function. For example, if we have a data frame called df that contains some missing values then the percentage of missing values can be calculated by using the command: (sum(is.na(df))/prod(dim(df)))*100Example1 Live DemoConsider the below data frame −x1

Create Large Vector with Repetitive Elements of Varying Size in R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:51:52

431 Views

To create a large vector of repetitive elements of varying size we can use the rep function along with the logical vector as an index. The logical vector that contains TRUE or FALSE will define the selection or omission of the values in the vector created with the help of rep function as shown in the below examples. If the vector created by using rep is larger than the logical vector then the logical vector will be recycled.Example1 Live Demox1

Add New Value to Each Element of List in R

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

860 Views

Suppose we have a list that contain two elements and we get a new value for both of these elements then the problem of adding those values to the original list arises. This can be done with the help of mapply function. We can append the new values in the original easily but a vector of the new values needs to be created first.Example1 Live DemoList1

Create Combinations of 0 and 1 with Fixed Number of 1s in R Data Frame

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

461 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

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

Convert Sparse Matrix into 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

Replace NA with 0 and Other Values to 1 in R Data Frame Column

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

794 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

Create Plots Arranged in a List Using ggplot2 in R

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

513 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

Use of set.seed() in R

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

909 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

Advertisements