Find Matches of Single Array in Multidimensional Array Using JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:47:48

170 Views

We are required to write a JavaScript function that takes in an array of arrays of Numbers as the first argument and an array of Numbers as the second argument. The function should pick a subarray from each array of the first array, (subarray that contains item common to both the second array and the corresponding array of first array.)For example −If the inputs are −Exampleconst arr1 = [ [1, 2, 5, 6], [5, 13, 7, 8], [9, 11, 13, 15], [13, 14, 15, 16], [1, 9, 11, 12] ]; const arr2 = [9, 11, 13, 15, 1, 2, 5, ... Read More

Create a Vector of Lists in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:46:58

568 Views

If we have many lists but we want to use the values in the lists as a vector then we first need to combine those lists and create a vector. This can be done by using unlist function along with the combine function c to create the vector. For example, if we have two lists defined as List1 and List2 and we want to create a vector V using these lists then it can be created as:V x1 x1Output$a [1] -0.6972237 -1.5013768 -0.2451809 -0.2365569 -1.6304919 -1.1704378 [7] 1.1617054 -0.2349498 -1.2582229 0.4112065 $b [1] 2 0 2 6 0 0 ... Read More

Find Longest Repeating Series of Numbers in Array with JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:45:29

615 Views

We are required to write a JavaScript function that takes in an array of Numbers that may contain some repeating elements. The function should return the length of the longest repeating number sequence from the array.For example −If the input array is −const arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1];Then the output should be 3 because the number 2 is repeated 3 times consecutively in the array (and that's the highest number).Exampleconst arr = [2, 1, 1, 2, 3, 3, 2, 2, 2, 1]; const findLongestSequence = (arr = []) => {    const res ... Read More

Sort Array Based on Another Array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:44:03

447 Views

Suppose, we have two arrays like these −const input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"];We are required to write a JavaScript function that takes in two such arrays as first and second argument respectively.The function should sort the elements of the first array according to their position in the second array.The code for this will be −Exampleconst input = ['S-1', 'S-2', 'S-3', 'S-4', 'S-5', 'S-6', 'S-7', 'S-8']; const sortingArray = ["S-1", "S-5", "S-2", "S-6", "S-3", "S-7", "S-4", "S-8"]; const sortByReference = (arr1 = [], arr2 = ... Read More

Create Histogram with Main Title in Outer Margin of Plot Window in Base R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:43:53

2K+ Views

The main title of a histogram in base R can be inserted by using title function and if we want to have it in the outer margin then outer argument must be set to TRUE. If the outer argument does not fulfil our requirement then we can use par function to adjust the outer margin area and create the histogram. Check out the below example to understand how it works.Example> x hist(x) > title('Normal Distribution',outer=TRUE)OutputExample> par(oma=c(0,0,2,0)) > hist(x) > title('Normal Distribution',outer=TRUE)Output

Convert JSON to Another JSON Format with Recursion in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:42:20

2K+ Views

Suppose, we have the following JSON object −const obj = {    "context": {       "device": {          "localeCountryCode": "AX",          "datetime": "3047-09-29T07:09:52.498Z"       },       "currentLocation": {          "country": "KM",          "lon": -78789486,       }    } };We are required to write a JavaScript recursive function that initially takes in one such array.The function should split the above object into a "label" - "children" format.Therefore, the output for the above object should look like −const output = {   ... Read More

Find Group Wise Median in R Data Table

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:42:17

811 Views

When the assumptions of parametric analysis are not satisfied then we move on to non-parametric analysis and non-parametric analysis often deals with the calculation of median because the data is not normally distributed. If we want to find the group-wise median and the data is stored in a data.table object then lapply function can be used as shown in the below examples.ExampleLoading data.table package:> library(data.table)Consider the below data.table object:Example> Group x1 x2 x3 x4 dt1 dt1OutputGroup x1 x2 x3 x4 1: B 0.515370827 6.174187 542.9350 50.28300 2: B 0.522858146 6.976872 510.5568 49.71331 3: A 1.055456751 3.192242 476.7693 48.88280 4: A ... Read More

Convert a Matrix into a Single Column Matrix in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:40:53

1K+ Views

If we have a matrix then we might want to convert it to matrix with single column for some analytical purpose such as multiplying with a vector that has the length equal to the total number of elements as in the matrix. Thus, the matrix can be converted to a single column matrix by using matrix function itself but for this we would need to nullify the column names and row names.Example1Live Demo> M1 M1Output [, 1] [, 2] [, ... Read More

Deep Search JSON Object in JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:38:35

6K+ Views

Suppose we have the following nested JSON object −const obj = {    id: 1,    title: 'hello world',    child: {       id: null,       title: 'foobar',       child: {          id: null,          title: 'i should be in results array '       }    },    foo: {       id: null,       title: 'i should be in results array too!' },       deep: [       {          id: null,         ... Read More

Create Smooth Density Curves Without Filling Densities in R

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:36:48

529 Views

The density curves can be created by using stat_density function of ggplot2 package but it fills the curve with density hence it becomes difficult to recognize the curves. We can remove these densities by using geom="line" inside the stat_density function so that only the density curves will be plotted.ExampleConsider the below data frame:Live Demo> G Response df dfOutputG Response 1 C 1.0229016 2 C 1.0058160 3 B 0.8831558 4 B 0.7729167 5 C 0.9130468 6 D 0.8431893 7 B 1.5003581 8 A 0.9687335 9 B 1.1139661 10 A 0.9211660 11 A 1.1790619 12 D 0.6349671 13 A 1.2616918 14 A ... Read More

Advertisements