Server Side Programming Articles

Page 1148 of 2109

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 581 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 −set.seed(21) x

Read More

How to create a sparse matrix in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 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.Example1i

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 883 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.Example1y1

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 537 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.Example1First

Read More

How to remove rows for categorical columns that has three or less combination of duplicates in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 428 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.Example1y1

Read More

How to find the percentage of missing values in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 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)))*100Example1y1

Read More

How to convert multiple columns into single column in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 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)).Example1y1

Read More

How to count the number of duplicate rows in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To count the number of duplicate rows in an R data frame, we would first need to convert the data frame into a data.table object by using setDT and then count the duplicates with Count function. For example, if we have a data frame called df then the duplicate rows will be counted by using the command − setDT(df)[,list(Count=.N),names(df)].Example1y1

Read More

How to create a categorical variable using a data frame column in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

If a variable is numerical then it can be converted into a categorical variable by defining the lower and upper limits. For example, age starting from 21 and ending at 25 can be converted into a category say 21−25. To convert an R data frame column into a categorical variable, we can use cut function.Example1y1

Read More

How to find the counts of categories in categorical columns in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 6K+ Views

If we have two categorical columns in an R data frame then we can find the frequency/count of each category with respect to each category in the other column. This will help us to compare the frequencies for all categories. To find the counts of categories, we can use table function as shown in the below examples.Example1y1

Read More
Showing 11471–11480 of 21,090 articles
Advertisements