Articles on Trending Technologies

Technical articles with clear explanations and examples

How to convert a numerical column into factor column in R?

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

Often, we find that the values that represent factor levels are recorded as numerical values, therefore, we need to convert those numerical values to factor. In this way, we can use the factor column properly in our analysis otherwise R program will treat the factors as numerical values and the analysis output will be incorrect.Exampledata(mtcars) str(mtcars)Output'data.frame': 32 obs. of 11 variables: $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ... $ cyl : num 6 6 4 6 8 6 8 4 4 6 ... $ disp: num 160 160 108 258 360 ... $ hp : num 110 110 93 110 175 105 245 62 95 123 ... $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ... $ wt : num 2.62 2.88 2.32 3.21 3.44 ... $ qsec: num 16.5 17 18.6 19.4 17 ... $ vs : num 0 0 1 1 0 1 0 1 1 1 ... $ am : num 1 1 1 0 0 0 0 0 0 0 ... $ gear: num 4 4 4 3 3 3 3 4 4 4 ... $ carb: num 4 4 1 1 2 1 4 2 2 4 ... mtcars$cyl

Read More

How to find unique permutations if a vector contains repeated elements in R?

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

We can use permn function from combinat package to find the permutations but if we have repeated elements in the vector then the result will not have unique permutations, therefore, we need to use unique function along with the permn function. For example, if we have a vector 1, 2, 1 then the permutations will be (1 2 1), (1 1 2), (1 1 2), (1 2 1), (2 1 1), (2 1 1) and the unique permutations will be (1 2 1), (1 1 2), (2 1 1).Examplex1

Read More

Program to find longest consecutive run of 1 in binary form of a number in C++

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

Suppose we have a number n, we have to find the length of the longest consecutive run of 1s in its binary representation.So, if the input is like n = 312, then the output will be 3, as 312 is 100111000 in binary and there are 3 consecutive 1s.To solve this, we will follow these steps −ret := 0, len := 0for initialize i := 0, when i < 32, update (increase i by 1), do:if n/2 is odd, then(increase len by 1)Otherwiselen := 0ret := maximum of ret and lenreturn retLet us see the following implementation to get better ...

Read More

How to create a horizontal bar chart using ggplot2 with labels at inside end of the bars in R?

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

To create a horizontal bar chart using ggplot2 package, we need to use coord_flip() function along with the geom_bar and to add the labels geom_text function is used. These two functions of ggplot2 provides enough aesthetic characteristics to create the horizontal bar chart and put the labels at inside end of the bars.Examplex

Read More

How to add a string before each numeric value in an R data frame column?

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

Sometimes the unique identity column of a data frame is not recorded as intended, it contains only numeric values that does not solve the data characteristic purpose. Therefore, we might want to add a string before those numeric values to make the data more sensible for viewers and analysts. This can be easily done with the help of gsub function.Consider the below data frame −Exampleset.seed(111) x1

Read More

How to find the sum of consecutive values considering two values each time in R?

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

Finding the sum of consecutive value while considering the sum of two values each time means the sum of first two values, then the sum of second value and the third value, then the sum of third value and the fourth value, then the sum of fourth value and the fifth value, and so on. For this purpose, we can use rollapply function from zoo package.Loading zoo packageibrary(zoo)Examplex1

Read More

How to create a barplot with one of the bars having different color in R?

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

A bar plot represents discrete data and the bars in the bar plot are usually of same color but we might want to highlight a particular bar based on the characteristics of the data or the objective of the analysis project. For example, if a particular bar represents highly severe situation or highly unimportant situation then we can change the color that particular bar so that people can easily point out that bar.Consider the below data frame −Examplex

Read More

How to find different elements between two string vectors in R?

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

Just like numerical vectors, we can find the different elements between two string vectors if there exists any. For this purpose, we can use setdiff function. For example, if we have a vector V1 that contains a, b, c, d, e, f and the other vector V2 that contains a, e, h, k, l, p, r, u, v, w then the different elements between these two vectors can be found as setdiff(V1,V2).Examplex1

Read More

How to create a random sample of some percentage of rows for a particular value of a column from an R data frame?

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

Random sampling is an important part of data analysis, mostly we need to create a random sample based on rows instead of columns because rows represent the cases. To create a random sample of some percentage of rows for a particular value of a column from an R data frame we can use sample function with which function.Consider the below data frame −Exampleset.seed(887) grp

Read More

How to create a subset of an R data frame having complete cases of a particular column?

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

If we have missing values in a data frame then all the values cannot be considered complete cases and we might want to extract only values that are complete. We might want extract the complete cases for a particular column only. Therefore, we can use negation of is.na for the column of the data frame that we want to subset.Consider the below data frame −Exampleset.seed(123) x

Read More
Showing 26511–26520 of 61,298 articles
Advertisements