Create a Plot Using RGB Colors in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:18:50

623 Views

The rgb colors are referred to red green and blue. This combination helps us to create many different colors. In R, we can use rgb function to create a plot using with different colors along with the image function. If we want to have a plot with rgb colors without any axes title or axes labels then the appropriate arguments should be used inside the image function as shown in the below example.ExampleConsider the below data frame:Live Demo> set.seed(9991) > x1 x2 x3 df dfOutput x1 x2 ... Read More

Merge Two Different Arrays of Objects Using JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:16:52

527 Views

Suppose, we have two different array of objects that contains information about the questions answered by some people −const arr1=[    { PersonalID: '11', qusetionNumber: '1', value: 'Something' },    { PersonalID: '12', qusetionNumber: '2', value: 'whatever' },    { PersonalID: '13', qusetionNumber: '3', value: 'anything' },    { PersonalID: '14', qusetionNumber: '4', value: 'null' } ]; const arr2=[    { qusetionNumber: '2', chID: '111', cValue: 'red' },    { qusetionNumber: '2', chID: '112', cValue: 'green'},    { qusetionNumber: '2', chID: '113', cValue: 'blue' },    {qusetionNumber: '3', choiceID: '114', cValue: 'yellow'},    {qusetionNumber: '4', choiceID: '115', cValue: 'red'} ];We ... Read More

Create Random Vector in R That Sums to 1

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:16:44

607 Views

To create a random vector that sums to 1, we can use uniform distribution. The main thing that needs to be done cautiously is we should include 0 in the vector with randomly generating uniform distribution values. Check out the below examples to understand how it can be done.Example1Live Demo> x1 x1Output[1] 0.45490995 0.23826247 -0.07338489 -0.33361362 0.26125094 -0.45243689 [7] 0.05967125 0.43007076 0.04069027 0.37457976Example> sum(x1)Output[1] 1Example2Live Demo> x2 x2Output[1] 1.84330339 -0.11622911 -0.15001654 0.07803346 -0.17353612 0.23651847 [7] -0.21121933 -0.30938763 0.44503222 -0.64249881Example> sum(x2)Output[1] 1Example3Live Demo> x3 x3Output[1] 2.63249755 1.17230387 -0.28068787 0.58040911 -1.48530836 -0.04894802 [7] 0.66718009 0.13504265 -0.18253891 -0.49757615 1.63580429 -2.31002917 [13] 2.66256899 -2.40636756 ... Read More

Create a Plot in Base R with Mixed Font of Plot Title

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:14:51

218 Views

We sometimes want to highlight the main title of a plot and one of the ways to do it is changing the font of the title to a unique or using a mixed font for the title. If we want to used mixed font then we need to use the appropriate font style for the title inside as shown in the below examples.Example1> plot(rpois(10,5),main=substitute(paste(italic("Point Chart"),": Poisson Distribution")))Output:Example2> plot(rpois(10,2),main=substitute(paste(bold("Point Chart"),": Poisson Distribution")))Output:

Multidimensional Array Intersection Using JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:13:40

404 Views

We are required to write a JavaScript function that takes in a multidimensional array of arrays of literal values. Our function should return the intersecting array of all the subarrays present in the input array.ExampleThe code for this will be −const arr = [    ["garden","canons","philips","universal"],    ["universal","ola","uber","bangalore"] ]; const findMultiIntersection = (arr = []) => {    const res = [];    arr.forEach(el => {       const thisObj = this;       el.forEach(element => {          if(!thisObj[element]){             thisObj[element] = true;          }          else{             res.push(element)          };       });    }, {});    return res; }; console.log(findMultiIntersection(arr));OutputAnd the output in the console will be −[ 'universal' ]

Find the Range of a Vector in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:13:08

4K+ Views

The range function in R provides the minimum and maximum values instead of the difference between the two. Hence, we can find the minimum and maximum by using range function then diff function can be used to find the actual range. For example, if we have a vector x then the range can be found by using diff(range(x)).ExampleLive Demo> x1 x1Output[1] 4 2 3 0 2 3 1 3 4 2Example> diff(range(x1))Output[1] 4 ExampleLive Demo> x2 x2Output[1] 4 5 3 10 2 4 2 4 8 7 3 1 5 6 7 3 7 3 4 5 3 7 ... Read More

Dynamically Combine All Provided Arrays Using JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:12:33

471 Views

Suppose we have two arrays of literals like these −const arr1= ['a', 'b', 'c']; const arr2= ['d', 'e', 'f'];We are required to write a JavaScript function that takes in two such arrays and build all possible combinations from the arrays.So, for these two arrays, the output should look like −const output = [ad, ae, af, bd, be, bf, cd, ce, cf];ExampleThe code for this will be −const arr1= ['a', 'b', 'c']; const arr2= ['d', 'e', 'f']; const combineArrays = (...arr) => {    const res = [];    const combinePart = (part, index) => {       arr[index].forEach(el => ... Read More

Sort Array by Month and Year in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:11:10

714 Views

Suppose, we have an array that contains dates in MM-YYYY format like this −const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"];We are required to write a JavaScript function that takes in one such array and sorts it such that the dates in the array are arranged in oldest to newest order.ExampleThe code for this will be −const arr = ["1-2016", "7-2015", "7-2016", "3-2016", "8-2016", "2-2016", "6-2016", "8-2015", "5-2016", "4-2016", "9-2015", "10-2015", "11-2015", "12-2015"]; const padToString = (num) => {    return String("0" + num).slice(-2); }; const sortByDate = (first, second) => ... Read More

Find Minimum and Maximum of Column Values in R Data Frame

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:10:14

219 Views

The range function in R provides the minimum and maximum values instead of the difference between the two. Hence, we can find the minimum and maximum by using range function but for a data frame we cannot use it directly. Check out the below examples to understand how it works.Example1Live Demo> set.seed(974) > x1 x2 x3 df1 df1Output x1 x2 x3 1 0 6 10 2 0 7 10 3 3 3 11 4 2 7 9 5 3 2 5 6 3 4 7 7 2 7 7 8 2 8 5 9 0 4 9 10 2 2 ... Read More

Find Most Frequent Number in Array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:09:48

271 Views

We are required to write a JavaScript function that takes in an array of literals and finds the most frequent number in the array and how many times it is repeated.ExampleThe code for this will be −const arr = ['13', '4', '1', '1', '4', '2', '3', '4', '4', '1', '2', '4', '9', '3']; const findFrequency = (arr = []) => {    const count = {};    const max = arr.reduce((acc, val, ind) => {       count[val] = (count[val] || 0) + 1;       if (!ind || count[val] > count[acc[0]]) {          return ... Read More

Advertisements