Set Plot Area to Assign Plots Manually in Base R

Nizamuddin Siddiqui
Updated on 08-Feb-2021 13:38:43

110 Views

We can split the screen to assign the plots manually in the plot area. The split.screen function can be used for this purpose. For example, if we want to create 4 plots in the plot window then we can use split.screen(c(2, 2)). Now to create the plot in first screen the command will be screen(1) then plot will be created. If we again create the plot then the original plot in screen(1) will be replaced with the new one. To create the plot in 3rd screen that is screen(3), we first need to use the command screen(3) and then create ... Read More

Convert Multiple Columns into Single Column in R Data Frame

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

10K+ Views

To convert multiple columns into single column in an R data frame, we can use unlist function. For example, if we have data frame defined as df and contains four columns then the columns of df can be converted into a single by using data.frame(x=unlist(df)).Example1 Live DemoConsider the below data frame −x1

Replace Outliers with 5th and 95th Percentile Values in R

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

376 Views

There are many ways to define an outlying value and it can be manually set by the researchers as well as technicians. Also, we can use 5th percentile for the lower outlier and the 95th percentile for the upper outlier. For this purpose, we can use squish function of scales package as shown in the below examples.Example1library(scales) x1

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

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

386 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

437 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

874 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

475 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

Advertisements