Programming Articles

Page 1257 of 2547

How to remove a column from a data frame that contains same value in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 461 Views

If we have only one value in all of the rows of an R data frame then we might want to remove the whole column because the effect of that column will not make any sense in the data analysis objectives. Thus, instead of removing the column we can extract the columns that contains different values.Exampleset.seed(1001) x1

Read More

Program to find the left side view of a binary tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 175 Views

Suppose we have a binary tree, if we see the tree from left side, then we can see some elements of it. we have to display those elements. So if the tree is like −The output will be [1, 2, 5]To solve this, we will follow these steps −Define an array retDefine a function dfs(), this will take node, c initialize it with 1, if node is null, then −returnif c > lvl, then −lvl := cinsert value of node into retdfs(left of node, c + 1)dfs(right of node, c + 1)From the main method, do the following −lvl := ...

Read More

How to find the median of all columns in an R data frame?

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

The median is the value in a vector that divide the data into two equal parts. To find the median of all columns, we can use apply function. For example, if we have a data frame df that contains numerical columns then the median for all the columns can be calculated as apply(df,2,median).ExampleConsider the below data frame −set.seed(7) x1

Read More

How to find the maximum value for each column of a matrix in R?

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

To find the maximum value for each column of a matrix, we need to use apply function. For example, if we have a matrix M that contains 2 rows and 2 columns with values 1, 2 in the first row and 3, 4 in the second row then the maximum for each of the columns in that matrix can be found by using the syntax; apply(M,2,max), hence the result will be 3, 4.ExampleM1−-matrix(1:36,ncol=6) M1Output   [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1    7    13   19   25   31 [2,] 2    8    14   20   26   32 [3,] 3    9    15   21   27   33 [4,] 4    10   16   22   28   34 [5,] 5    11   17   23   29   35 [6,] 6    12   18   24   30   36Exampleapply(M1,2,max)Output[1] 6 12 18 24 30 36ExampleM2

Read More

How to find the frequency of NA values per row in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 559 Views

Since column represent variables, we often find missing values in the columns of a data frame but we may want to find missing values(NA) for cases as well so that we can replace them based on case characteristic instead of the distribution of the variable. In R, we can use rowSums with apply function.ExampleConsider the below data frame −set.seed(8) x1

Read More

How to create histogram of all columns in an R data frame?

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

To create histogram of all columns in an R data frame, we can use hist.data.frame function of Hmisc package. For example, if we have a data frame df that contains five columns then the histogram for all the columns can be created by using a single line code as hist.data.frame(df).ExampleConsider the below data frame −set.seed(9) x1

Read More

Program to perform level order traversal of binary tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 324 Views

Suppose we have a binary tree. We have to traverse this tree using level order traversal fashion. So if the tree is likeThe traversal sequence will be like: [1, 2, 3, 5, 4]To solve this, we will follow these steps −define queue que to store nodesinsert root into the que.while que is not empty, doitem := item present at front position of queueprint the value of itemif left of the item is not null, then insert left of item into queif right of the item is not null, then insert right of item into quedelete front element from queLet us ...

Read More

How to sort a list that contains single sub-elements in decreasing order in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 193 Views

Just like a list can have multiple elements, the elements of the list can have multiple sub-elements and the size of those elements may vary as well hence a list with single sub-elements is also possible. If we have such type of list then we can sort that list in decreasing order by using order function but we also need to unlist those elements.ExampleConsider the below list −x1

Read More

Program to find tree level that has minimum sum in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 355 Views

Suppose we have a binary tree, the level of its root is 1, the level of its children is 2, and so on.We have to find the smallest level X such that the sum of all the values of nodes at level X is minimum. So if the tree is like −Output will be 2 as the sum is 4 – 10 = -6, which is minimum.To solve this, we will follow these steps −level := 1, sum := value of r, ansLevel := level, ansSum := sumdefine a queue q, insert given node r into qwhile q is not ...

Read More

How to find the frequency of repeated and unique values in a vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 414 Views

If we unique values in a vector in R and they are repeated then we can find the frequency of those unique values, this will help us to understand the distribution of the values in the vector. On the basis of that distribution analysis, we can proceed with the further analysis that could be used. This can be done with the help of rle function.Examplex1

Read More
Showing 12561–12570 of 25,466 articles
Advertisements