Found 2038 Articles for R Programming

How to generate the permutation of x values in y positions with fixed row sums in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:22:27

84 Views

To generate a permutation of x values in y positions, we can use expand.grid function. For example, if we want to generate three columns for the range of values 0 to 5 then it can be done in R by using the below command − Live Demox

How to calculate mahalanobis distance in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:18:08

583 Views

The Mahalanobis distance is the relative distance between two cases and the centroid, where centroid can be thought of as an overall mean for multivariate data. We can say that the centroid is the multivariate equivalent of mean. If the mahalanobis distance is zero that means both the cases are very same and positive value of mahalanobis distance represents that the distance between the two variables is large. In R, we can use mahalanobis function to find the malanobis distance.Example1 Live DemoConsider the below data frame −set.seed(981) x1

How to find the frequency of a particular string in a column based on another column in an R data frame using dplyr package?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:16:40

417 Views

When we have two or more categorical columns in an R data frame with strings as level of the categories or numbers as strings/integers then we can find the frequency of one based on another. This will help us to identify the cross-column frequencies and we can understand the distribution of one categorical based on another column. To do this with dplyr package, we can use filter function.Example Live DemoConsider the below data frame −Group%filter(Standard=="II")%>%count(Group) OutputGroup n 1 1 1 2 2 1 3 3 2 4 4 1Exampledf1%>%filter(Standard=="III")%>%count(Group) OutputGroup n 1 1 1 2 3 2 3 4 6 4 ... Read More

How to create geometric progression series in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:15:18

2K+ Views

A geometric progression series is a sequence of numbers in which all the numbers after the first can be found by multiplying the previous one by a fixed number. To generate a geometric progression series in R, we can use seq function. For example, to generate a geometric progression series of 2 by having the difference of multiplication value equal to 1 up to 5 can be found as 2^seq(0, 5, by=1) and the output would be 1, 2, 4, 8, 16, 32.Examples2^seq(0, 5, by=1) [1] 1 2 4 8 16 32 2^seq(0, 5, by=2) [1] 1 4 16 2^seq(0, ... Read More

How to create a sample from an R data frame if weights are assigned to the row values?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:14:33

883 Views

To create a random sample in R, we can use sample function but if the weight of the values is provided then we need to assign the probability of the values based on the weights. For example, if we have a data frame df that contains a column X with some values and another column Weight with the corresponding weights then a random sample of size 10 can be generated as follows −df[sample(seq_len(nrow(df)),10,prob=df$Weight_x),]Example Live DemoConsider the below data frame −set.seed(1256) x

How to convert a data frame column to date that contains integer values in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:13:01

796 Views

If we have an integer column that actually contains date values, for example having 29th September 2020 as 20200929 then we can convert it to date by using transform function by reading the dates with as.Date function but as.character will also be needed so that as.Date function can read the date values.Example1 Live DemoConsider the below data frame −ID

How to create scatterplot for factor levels in an R data frame?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:05:22

453 Views

To create a scatterplot for factor levels, we can use facet_grid function of ggplot2 package. For example, suppose we have a factor column in a data frame df defined as F and numerical columns defined as x and y then the scatterplot for the factor levels can be created as −ggplot(df,aes(x,y))+geom_point()+facet_grid(~Factor)Example Live DemoConsider the below data frame −set.seed(1251) Factor

How to change the font size of legend in base R plot?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 11:03:26

2K+ Views

In base R, we can use legend function to add a legend to the plot. For example, if we want to create a histogram with legend on top-right position then we can use legend("topright",legend="Normal Distribution") and if we want to change the font size then we need to as cex argument as shown below:legend("topright",legend="Normal Distribution",cex=2)Example Live DemoConsider the below histogram −x

How to find the union of two vectors in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 08:04:12

2K+ Views

The union of vectors return all the unique values in both the vectors. For example, if we have a vector x that contains 1, 2, 3, 4, 2, 3, 4, 1, 1, 4 and another vector that contains 2, 1, 2, 4, 5, 7, 5, 1, 2, 3, 7, 6, 5, 7, 4, 2, 4, 1, 5, 8, 1, 3 then the union of these two vectors will be 1, 2, 3, 4, 5, 6, 7, 8. In R, we can do this by using union function.ExampleLive Demo> x1 x1Output[1] 2 2 0 1 1 1 4 1 2 2ExampleLive ... Read More

How to round to the nearest hundred in R?

Nizamuddin Siddiqui
Updated on 07-Nov-2020 08:02:24

2K+ Views

To round a value in R, we can use round function and if we want to round to the nearest hundred then -2 value should be used for the rounding. For example, if we have a vector say x that contains 25, 78, 32, 38, 79, 91, 82, 20, 56 then the output of round(x,-2) will be as follows:0, 100, 0, 0, 100, 100, 100, 0, 100Example1Live Demo> x1 x1Output[1] 89.61275 141.54670 130.25924 142.81744 38.60795 47.40821 69.30543 [8] 12.15515 174.11419 20.69817Example> round(x1,-2)Output[1] 100 100 100 100 0 0 100 0 200 0Example2Live Demo> x2 x2Output[1] 200.095061 206.720294 138.471412 128.208184 151.900210 63.717254 [7] 156.931319 209.009279 176.416783 105.085543 93.546862 128.853476 [13] 148.053905 174.839196 203.737574 146.825702 105.457096 173.874628 [19] 169.803994 60.572176 178.237244 203.810326 80.719865 94.174711 [25] 162.453897 106.240436 127.433929 208.214542 131.028068 92.870231 [31] 140.720894 42.651514 165.289149 13.375160 94.329940 71.876679 [37] 107.241246 96.262988 219.807754 145.722219 108.398741 205.967403 [43] 145.948586 85.032097 227.794284 204.046132 74.620053 115.689188 [49] 169.348128 59.402132 212.328543 161.365704 140.384076 218.065167 [55] 100.907675 161.661667 34.387952 148.238412 151.183663 104.967732 [61] 136.176786 80.512360 95.035262 153.149381 4.655616 167.156364 [67] 14.473762 109.639397 107.070616 47.568150 180.272436 124.611189 [73] 158.350524 171.339213 121.324641 60.166787 65.728890 140.234364 [79] 137.634627 54.564704 101.383017 118.009068 147.360182 99.928836 [85] 5.523774 36.624999 10.115692 237.099438 122.702824 71.494478 [91] 113.543730 162.988237 74.957041 155.995607 96.629868 196.688934 [97] 166.616179 149.185278 95.640113 236.727589Example> round(x2,-2)Output[1] 200 200 100 100 200 100 200 200 200 100 100 100 100 200 200 100 100 200 [19] 200 100 200 200 100 100 200 100 100 200 100 100 100 0 200 0 100 100 [37] 100 100 200 100 100 200 100 100 200 200 100 100 200 100 200 200 100 200 [55] 100 200 0 100 200 100 100 100 100 200 0 200 0 100 100 0 200 100 [73] 200 200 100 100 100 100 100 100 100 100 100 100 0 0 0 200 100 100 [91] 100 200 100 200 100 200 200 100 100 200Example3Live Demo> x3 x3Output[1] 190.27252 155.32879 175.01110 100.87456 152.60590 74.71596 121.35612 [8] 55.87325 121.24243 81.55583 108.66598 100.59884 165.46113 129.17487 [15] 84.19696 112.37490 162.77709 100.26368 104.26728 57.81007 122.87770 [22] 175.91888 60.23992 98.50004 130.80482 170.23349 193.14007 173.45364 [29] 174.91854 131.74613 85.70544 101.73342 62.61430 105.24194 69.58468 [36] 113.18957 196.87726 188.19204 61.03517 76.17451 145.93752 153.57438 [43] 93.97600 106.59288 114.67775 196.36251 55.22318 160.60880 179.77599 [50] 55.62690 95.40597 119.23054 176.63038 94.59722 138.94008 101.09087 [57] 189.16560 65.93981 138.85905 174.91417 173.81721 167.15242 84.75690 [64] 197.05821 164.97665 52.40786 161.50446 92.95925 91.16180 131.55187 [71] 97.13889 96.88271 129.55102 177.42351 160.40981 191.51033 156.36852 [78] 55.18035 160.83181 175.20245Example> round(x3,-2)Output[1] 200 200 200 100 200 100 100 100 100 100 100 100 200 100 100 100 200 100 100 [20] 100 100 200 100 100 100 200 200 200 200 100 100 100 100 100 100 100 200 200 [39] 100 100 100 200 100 100 100 200 100 200 200 100 100 100 200 100 100 100 200 [58] 100 100 200 200 200 100 200 200 100 200 100 100 100 100 100 100 200 200 200 [77] 200 100 200 200 Example4Live Demo> x4 x4Output[1] 585.7340 472.3567 469.3459 375.4255 444.9098 478.0684 634.6411 379.8119 [9] 602.1881 563.3953 673.9045 486.2639 367.9377 315.7793 346.8235 593.0923 [17] 383.9356 613.3875 566.1341 582.9207 239.6635 444.1799 690.2431 330.5347 [25] 431.7668 598.7132 652.1041 684.2897 454.4928 506.6183 653.4909 450.6463 [33] 579.3951 663.9724 476.6189 451.4790 586.9429 543.9879 518.5954 504.7755 [41] 414.4607 430.8862 537.9323 356.7645 384.5718 533.1949 452.6198 446.6593 [49] 469.5048 500.3851 593.5534 244.6215 395.8466 545.7889 685.6367 268.5527 [57] 414.8149 500.2674 483.4512 444.0095 460.9468 382.2085 511.1722 477.4728 [65] 543.5912 739.8601 744.3788 443.7733 424.7943 500.5329 497.0514 601.4509 [73] 685.6504 594.2415 529.4130 742.5796 572.8989 602.9205 625.1378 543.2260 [81] 403.1793 373.7638 659.8974 468.5121 358.3145 582.3178 374.0248 639.3185 [89] 447.4567 270.1555 610.0241 356.8236 464.9692 615.0988 775.8457 531.8723 [97] 261.7674 617.1434 553.2038 454.2908Example> round(x4,-2)Output[1] 600 500 500 400 400 500 600 400 600 600 700 500 400 300 300 600 400 600 [19] 600 600 200 400 700 300 400 600 700 700 500 500 700 500 600 700 500 500 [37] 600 500 500 500 400 400 500 400 400 500 500 400 500 500 600 200 400 500 [55] 700 300 400 500 500 400 500 400 500 500 500 700 700 400 400 500 500 600 [73] 700 600 500 700 600 600 600 500 400 400 700 500 400 600 400 600 400 300 [91] 600 400 500 600 800 500 300 600 600 500

Advertisements