When we create a histogram using ggplot2 package, the area covered by the histogram is filled with grey color but we can remove that color to make the histogram look transparent. This can be done by using fill="transparent" and color="black" arguments in geom_histogram, we need to use color argument because if we don’t use then the borders of the histogram bars will also be removed and this color is not restricted to black color only.ExampleConsider the below data frame −set.seed(987) x
The percentiles divide a set of numeric values into hundred groups or individual values if the size of the values is 100. We can find percentiles for a numeric column of an R data frame, therefore, it is also possible to select values of a column based on these percentiles. For this purpose, we can use quantile function.ExampleConsider the below data frame −set.seed(111) x
If we have numbers then we might want to convert those numbers into words. For example, converting 1 to one. This might be required in cases where we have text data and numbers are part of the text. Therefore, it would be better to represent the numbers in text form to make the uniformity in the text. This can be done by using replace_number function qdap package.Installing and loading qdap package−install.packages("qdap") library("qdap")Examplereplace_number("1") [1] "one" replace_number("10") [1] "ten" replace_number("100") [1] "one hundred" replace_number("1000") [1] "one thousand" replace_number("1001") [1] "one thousand one" replace_number("12000") [1] "twelve thousand" replace_number("12214") [1] "twelve thousand two hundred ... Read More
Dealing with NA values is one of the boring and almost day to day task for an analyst and hence we need to replace it with the appropriate value. If in an R data frame, we have a Boolean column that represents TRUE and FALSE values, and we have only FALSE values then we might want to replace NA’s with TRUE. In this case, we can use single square bracket and is.na to set all NA’s to TRUE.Exampleset.seed(999) S.No.
Sometimes we have missing values that can be replaced with the values on the above row values, it often happens in situations when the data is recorded manually and the person responsible for it just mention the unique values because he or she understand the data characteristics. But if this data needs to be re-used by someone else then it does not make sense and we have to connect with the concerned person. If the concerned person tells us that the first value in each row can be filled for every NA in the same column then it can be ... Read More
The value of mean is an important characteristic of the data to be represented by a histogram, therefore, one might want to plot it with the histogram. If the histogram is created by using hist function then we can create a vertical line on the histogram with the help of abline function by defining mean of the data for vertical argument v.Exampleset.seed(101) x
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).Example Live Demox1
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)Example Live Demox1
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
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.Example Live Demox