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

483 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

736 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

243 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

286 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

Determine All Possible Ways to Remove Values from a Sequence in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:08:22

123 Views

We are required to write a JavaScript function that determines how many different ways we can remove a group of values from a sequence, leaving the original sequence in order (stable), and making sure to remove only one instance value each from the original sequence.For example − If the sequence array is −const arr = [1, 2, 1, 3, 1, 4, 4];And the array to be removed is −const arr2 = [1, 4, 4];Then there are three possible ways of doing this without disrupting the order of elements −1 --> [2, 1, 3, 1] 2 --> [1, 2, 3, 1] ... Read More

Find Mean of Very Small Numbers in Scientific Notation in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:07:55

206 Views

If we find the mean of scientific numbers then the result will be also in scientific notation. We can get rid of this problem by using options(scipen=999), once we will use this code in R console all the inputs that are in scientific notation will be converted to normal numeric form, including any calculation and if we want to go back to the scientific notation then options(scipen=0) can be used.ExampleLive Demo> x1 mean(x1)Output[1] 4.436267e-22Example> options(scipen=999) > mean(x1)Output[1] 0.0000000000000000000004436267ExampleLive Demo> x2 x2Output[1] 0.000000000000000000000000000000000000001010964 [2] 0.000000000000000000000000000068291679999999998 [3] 0.000000000000000000000000006026013000000000181 [4] 0.000000000000000000000000002702241000000000107 [5] 0.000000000000000000000042258669999999998179163 [6] 0.000000000000000000000000000000091949710000000 [7] 0.000000000000000000000000000000000000107406400 [8] 0.000000000000000000000000000000091949710000000 [9] 0.000000000000000000000003463124999999999951636 [10] 0.000000000000000000004305051000000000103323794 ... Read More

Filter Nested Object by Keys Using JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:06:07

1K+ Views

Suppose, we have an array of objects like this −const arr = [{ 'title': 'Hey',    'foo': 2,    'bar': 3 }, {    'title': 'Sup',    'foo': 3,    'bar': 4 }, {    'title': 'Remove',    'foo': 3,    'bar': 4 }];We are required to write a JavaScript function that takes in one such array as the first input and an array of string literals as the second input.Our function should then prepare a new array that contains all those objects whose title property is partially or fully included in the second input array of literals.ExampleThe code for ... Read More

Convert Array with Duplicate Values to Object in JavaScript

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

744 Views

Suppose, we have an array string that contains some duplicate entries like this −const arr = ['California', 'Texas', 'Texas', 'Texas', 'New York', 'Missouri', 'New Mexico', 'California'];We are required to write a JavaScript function that takes in one such array. Our function should then construct an array of objects that contains an object for each unique entry and a property "count" that contains its count in the original array.Therefore, the final output for the above array should look something like this −const output = [    {'name':'California', 'count':2},    {'name':'Texas', 'count':3},    {'name':'New York', 'count':1},    {'name':'Missouri', 'count':1},    {'name':'New Mexico', ... Read More

Find Absolute Distance Between Columns of an R Data Frame

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:04:02

846 Views

The absolute distance can be found by calculating the difference between column values. And if we want the distance to be absolute then we would be need to use abs function. For example, suppose we have a data frame df that contain columns x and y then the absolute distance can be found by using df$Absolute_Distance set.seed(274) > x1 y1 df1 df1Output  x1 y1 1 6 11 2 1 4 3 4 2 4 7 12 5 4 5 6 6 10 7 6 14 8 6 8 9 2 11 10 3 8 11 3 8 12 2 6 ... Read More

Advertisements