Programming Articles

Page 2271 of 2547

How to create a bar chart using ggplot2 with dots drawn at the center of top edge of the bars in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 07-Nov-2020 670 Views

Aesthetics is one of the most important aspect of a chart, hence we should try to use the best possible aesthetic properties in a plot. In a bar chart, we can represent the center of bars in many ways and one such way is using dots at the center of the top edge of the bars. We can use geom_point function by defining colour argument to put points at the center of top edge of the bars in a bar chart created by using ggplot2.ExampleConsider the below data frame:> freq df dfOutputx freq 1 Mango 212 2 Guava 220 3 ...

Read More

How to display xtable values in scientific form in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 394 Views

The xtable function of xtable package creates a Latex table. We can use digits argument with negative sign to convert the values in the original table into scientific form. For example, if we have a data frame defined as df then we can read it with xtable as xtable(df,digits=-10).Loading xtable package −library(xtable)Example1data1

Read More

How to create sets using vector values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 2K+ Views

A set in mathematics is defined as the collection of unique elements and the order of the elements does not matter. In R, we can create sets using set_power function of sets package. For example, if we have a vector x that contains A, B, C then the sets using the vector x can be created by using set_power(x).Loading sets package −library(sets)Examplesx1

Read More

How to create a random sample of values between 0 and 1 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 4K+ Views

The continuous uniform distribution can take values between 0 and 1 in R if the range is not defined. To create a random sample of continuous uniform distribution we can use runif function, if we will not pass the minimum and maximum values the default will be 0 and 1 and we can also use different range of values.Examplesrunif(5) [1] 0.8667731 0.7109824 0.4466423 0.1644701 0.5611908 runif(10) [1] 0.5923782 0.8793613 0.6912947 0.2963916 0.6076762 0.7683766 0.1143595 [8] 0.4782710 0.1143383 0.4540217 runif(50) [1] 0.841674685 0.325249762 0.640847906 0.203868249 0.495230429 0.897175830 [7] 0.744447459 0.490173680 0.254711280 0.144844443 0.867749180 0.004405166 [13] 0.539785687 0.739637398 0.062214554 0.648021581 0.768686809 0.305543906 [19] 0.757496413 0.527085302 0.633331579 0.700118363 0.857950259 0.929350618 [25] 0.167015719 0.775870043 0.430343200 0.528408273 0.600575697 0.612206968 [31] 0.065904791 0.061135682 0.082027863 0.193586800 0.013956337 0.156875620 [37] 0.837501421 0.971202297 0.930835689 0.292126061 0.599263353 0.826630821 [43] 0.509235736 0.741715013 0.224485511 0.113099235 0.395143355 0.375654137 [49] 0.973050494 0.107550270 round(runif(50),2) [1] 0.51 0.70 0.90 0.45 0.41 0.74 0.31 0.40 0.10 0.05 0.18 0.05 0.63 0.34 0.57 [16] 0.06 0.73 0.37 0.79 0.85 0.82 0.41 0.32 0.34 0.37 0.14 0.21 0.11 0.43 0.86 [31] 0.83 0.09 0.88 0.04 0.62 0.64 0.15 0.75 0.78 0.16 0.67 0.97 0.79 0.64 0.56 [46] 0.40 0.07 0.69 0.82 0.63 round(runif(50),4) [1] 0.2951 0.2916 0.9049 0.2669 0.7613 0.2080 0.4739 0.1110 0.6155 0.5429 [11] 0.4490 0.2941 0.8262 0.7719 0.7896 0.7634 0.6260 0.7812 0.7600 0.6852 [21] 0.9142 0.0165 0.2324 0.0821 0.0814 0.4009 0.3315 0.8843 0.9684 0.1966 [31] 0.4841 0.5795 0.7898 0.1865 0.6929 0.8599 0.0492 0.8275 0.7431 0.3122 [41] 0.8480 0.3327 0.4872 0.0503 0.1887 0.0296 0.6011 0.1162 0.7776 0.6874 round(runif(50),5) [1] 0.40368 0.33585 0.03557 0.06047 0.95041 0.18260 0.70011 0.75148 0.12414 [10] 0.01310 0.42343 0.05846 0.21341 0.05454 0.77823 0.66151 0.61406 0.59459 [19] 0.50299 0.96780 0.43033 0.64652 0.39697 0.05897 0.47169 0.79828 0.74154 [28] 0.56074 0.97303 0.35301 0.36110 0.67452 0.14553 0.45195 0.05780 0.90489 [37] 0.96745 0.28014 0.02089 0.77789 0.04797 0.03550 0.40495 0.08924 0.59908 [46] 0.89074 0.48498 0.47335 0.59422 0.00719 round(runif(100),2) [1] 0.10 0.06 0.51 0.89 0.80 0.68 0.97 0.58 0.60 0.79 0.96 0.48 0.29 0.16 0.42 [16] 0.35 0.46 0.18 0.46 0.34 0.48 0.35 0.72 0.10 0.50 0.93 0.30 0.54 0.85 0.19 [31] 0.12 0.10 0.47 0.66 0.43 0.09 0.44 0.86 0.99 0.31 0.10 0.61 0.20 0.15 0.02 [46] 0.25 0.33 0.75 0.98 0.23 0.21 0.70 0.42 0.24 0.87 0.84 0.99 0.06 0.75 0.48 [61] 0.84 0.35 0.48 0.62 0.40 0.25 0.07 0.08 0.75 0.40 0.83 0.95 0.00 0.87 0.27 [76] 0.53 0.21 0.41 0.28 0.83 0.90 0.26 0.50 0.19 0.70 0.93 0.24 0.45 0.33 0.84 [91] 0.15 0.81 0.62 0.17 0.08 0.76 0.74 0.11 0.20 0.49 round(runif(150),1) [1] 0.6 0.3 0.3 0.3 0.9 0.7 0.1 0.1 0.1 0.9 0.4 0.6 1.0 0.0 0.4 1.0 0.1 1.0 [19] 0.8 0.0 0.9 0.9 0.7 0.7 0.7 0.7 0.3 0.7 0.1 0.1 0.9 0.0 0.1 1.0 0.9 1.0 [37] 0.9 0.6 0.0 0.4 0.4 1.0 0.2 0.4 0.2 0.8 0.3 0.9 0.8 0.6 0.3 0.3 0.4 0.7 [55] 0.2 0.9 1.0 0.9 0.8 0.7 0.9 1.0 0.5 0.8 0.6 0.8 0.6 0.8 0.3 0.3 1.0 0.6 [73] 0.9 0.3 0.0 1.0 0.5 0.6 0.7 0.7 0.6 0.3 0.4 0.0 0.3 0.1 0.6 0.2 0.1 0.7 [91] 0.9 0.8 0.3 0.2 0.5 0.6 0.6 0.1 0.0 0.9 0.4 0.6 0.3 0.2 0.9 0.6 0.0 0.2 [109] 0.3 0.3 0.3 0.7 0.4 0.8 0.5 0.9 0.6 0.5 0.3 1.0 0.6 0.7 0.9 0.1 0.8 1.0 [127] 0.3 1.0 0.2 0.9 0.2 0.3 0.5 0.4 0.1 0.6 0.6 0.0 0.3 0.3 0.0 0.3 0.3 1.0 [145] 0.6 0.5 0.1 0.7 0.6 0.4 round(runif(75),1) [1] 0.7 0.3 0.7 0.9 0.8 0.1 0.4 0.2 0.5 0.4 0.1 0.7 0.1 0.6 1.0 0.3 0.4 0.7 0.2 [20] 0.2 0.3 0.4 0.4 0.0 0.1 0.2 0.3 0.5 0.1 1.0 0.3 0.5 0.3 0.7 0.1 0.6 0.6 0.6 [39] 0.5 0.7 0.5 0.8 0.1 1.0 0.7 0.4 0.6 0.1 0.5 0.5 0.9 0.3 0.8 0.9 0.3 0.9 0.7 [58] 0.6 0.8 0.4 0.4 0.7 0.4 0.1 0.2 0.6 0.6 0.9 0.3 0.6 0.5 0.9 0.2 0.3 0.2 round(runif(75),3) [1] 0.712 0.355 0.130 0.768 0.134 0.681 0.273 0.663 0.849 0.851 0.842 0.430 [13] 0.371 0.903 0.148 0.879 0.812 0.330 0.567 0.646 0.199 0.159 0.056 0.448 [25] 0.637 0.204 0.101 0.389 0.797 0.030 0.021 0.167 0.440 0.359 0.670 0.435 [37] 0.807 0.669 0.738 0.546 0.535 0.969 0.055 0.201 0.436 0.336 0.841 0.548 [49] 0.901 0.850 0.369 0.770 0.678 0.922 0.252 0.132 0.635 0.544 0.291 0.715 [61] 0.601 0.399 0.585 0.161 0.423 0.244 0.451 0.397 0.951 0.382 0.123 0.959 [73] 0.252 0.330 0.238

Read More

How to set the alignment of labels in horizontal bar plot to left in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 1K+ Views

When we create a horizontal bar plot using ggplot2 package, the labels of the categorical variable are aligned to the right-side of the axis and if the size of these labels are different then it looks a little ambiguous. Therefore, we might want to set the alignment of the labels to left-side and this can be done by using theme function of ggplot2 package.ExampleConsider the below data frame:> df dfOutput    x y 1 India  14 2 UK     15 3 Russia 12 4 United States of America 18Loading ggplot2 package and creating a horizontal ...

Read More

How to check the difference between plot generation time in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 146 Views

One of the mostly used time measurement function in R is microbenchmark function of microbenchmark package. We can pass the function to create the plot inside microbenchmark function and this will result in the processing time for each of the plots then a comparison can be done for the difference.Example1Loading microbenchmark package:> library(microbenchmark)Finding the plot generation time:> x1 x2 x3 X XUnit: milliseconds expr min lq mean median uq max neval plot(x1) 12.7488 14.88815 15.65040 15.2515 15.90765 23.9348 100 plot(x2) 20.9810 21.67780 23.92976 22.2116 23.29665 137.2474 100 plot(x3) 93.6965 95.03440 96.67086 95.6717 97.12290 125.3670 100Plots:Example> plot(x1)Output:Example> plot(x2)Output:Example> plot(x3)Output:

Read More

How to generate passwords with varying lengths in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 06-Nov-2020 271 Views

To generate passwords, we can use stri_rand_strings function of stringi package. If we want to have passwords of varying length then we need to create the passwords using the particular size separately. For example, for a size or length of the password equals to 8, we can use the argument length in the stri_rand_strings function.Loading stringi package:> library(stringi)Example1> stri_rand_strings(n=5, length=8, pattern="[0-9a-zA-Z]") [1] "YkIEDYQz" "t42JCzYO" "rOE9YN8U" "2lu9AonY" "6lDUxScX"Example2> stri_rand_strings(n=20, length=8, pattern="[0-9a-zA-Z]") [1] "glH3ysoX" "X0Sgvg3F" "P3YOePTa" "45GOb2hA" "tLCwszus" "CerCi1ks" [7] "UtFwzrSc" "pG8AJCQX" "NTCdMRHj" "5thI1wKb" "Ic8Rol1Y" "JakWa1Wd" [13] "9AfeXo7T" "SFJVn9XV" "lIRhLbJ9" "DNFyAbkJ" "jV4jJRZk" "IthkzfEU" [19] "talj9nBq" "Nak9Tidh"Example3> ...

Read More

Count of distinct sums that can be obtained by adding prime numbers from given arrays in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 31-Oct-2020 271 Views

We are given two arrays containing prime and non prime numbers. The goal is to find the count of distinct sums of pairs of prime numbers in each array. We will do this by making a pair of two primes from each array, take their sum and add them to set sums. In the end the size of the set is the number of distinct sums of primes. Let's understand with examples. Input Arr1[] = { 1, 2, 3 } Arr2[] = { 2, 3, 4} Output Distinct Sums of primes :3 Explanation Prime pairs ...

Read More

Count numbers whose difference with N is equal to XOR with N in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 31-Oct-2020 194 Views

We are a number N. The goal is to find numbers between 0 and N whose difference with N is equal to XOR with N.We will do this by traversing no. from i=0 to i

Read More

Count pieces of the circle after N cuts in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 31-Oct-2020 177 Views

We are given an integer N which represents the number of cuts applied on a 2D-circle. Each circle divides the circle in two halves. Goal is to find the pieces of the circle after N cuts.Number of pieces= 2 * no. of cutsLet’s understand with examples.Input − N=1Output − Pieces of circle: 2Explanation −Input − N=3Output − Pieces of circle: 6Explanation −Approach used in the below program is as followsWe take N for a number of cuts.Take pieces=1*N.Print the result..Example#include using namespace std; int main(){    int N=2;    Int pieces=2*N;    cout

Read More
Showing 22701–22710 of 25,466 articles
Advertisements