Suppose, we have an array of objects like this −const arr = [ {area: 'NY', name: 'Bla', ads: true}, {area: 'DF', name: 'SFS', ads: false}, {area: 'TT', name: 'SDSD', ads: true}, {area: 'SD', name: 'Engine', ads: false}, {area: 'NSK', name: 'Toyota', ads: false}, ];We are required to write a JavaScript function that takes in one such array as the first argument and an array of string literals as the second argument.Our function should then filter the input array of objects to contain only those objects whose "area" property is included in the array of literals ... Read More
Suppose, we have some data regarding some images in an array like this −const arr = [{ 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }, { 'image': "jv2bcutaxrms4i_img.png", 'gallery_image': true }, { 'image': "abs.png", 'gallery_image': true }, { 'image': "acd.png", 'gallery_image': false }];We are required to write a JavaScript function that takes in one such array.Our function should remove the objects from the array that have duplicate values for the 'image' property.ExampleThe code for this will be −const arr ... Read More
The central limit theorem says that as the sample size increases the distribution of the sample means approaches normal distribution. Therefore, irrespective of the actual population distribution if we take samples of larger size and find the mean of these samples then the distribution of these sample means will be approximately normal. We can display this in R, by creating the histogram of such type of means.Example1> x y
Suppose, we have an array of values like this −const arr = [ { value1:[1, 2], value2:[{type:'A'}, {type:'B'}] }, { value1:[3, 5], value2:[{type:'B'}, {type:'B'}] } ];We are required to write a JavaScript function that takes in one such array. Our function should then prepare an array where the data is grouped according to the "type" property of the objects.Therefore, for the above array, the output should look like −const output = [ {type:'A', value: [1, 2]}, {type:'B', value: [3, 5]} ];ExampleThe code ... Read More
In a linear model, a residual is the difference between the observed value and the fitted value and it is not different for a general linear model. The difference between linear model and the general linear model is that we use a probability distribution to create a general linear model. If we want to find the residual for a general linear model then resid function can be used just like it is used with the linear model.Example1Consider the below data frame:Live Demo> x1 y1 df1 df1Output x1 y1 1 4 2 2 3 3 3 5 3 4 4 2 ... Read More
Addition of a column with consecutive might have different objectives such as getting the sequence of numbers, representing serial numbers, representing ids, identification of each row, or a variable. We can use the sequence starting from any number up to the number of rows if we know the number of rows for this purpose.Example1Consider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 6.137898 5.203712 2 5.283467 5.057344 3 5.873749 4.907388 4 7.628762 5.012650 5 4.134700 4.988379 6 5.340686 4.684900 7 5.126999 4.821752 8 3.722762 4.974044 9 ... Read More
Suppose, we have two arrays like these −const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [ ["eggs", "yogurt", "toast"], ["falafel", "mushrooms", "fries"], ["pasta", "cheese"] ];We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array.Therefore, the output for the above arrays should look like −const output = { "breakfast" : ["eggs", "yogurt", "toast"], "lunch": ["falafel", "mushrooms", "fries"], "dinner": ["pasta", "cheese"] };ExampleThe code for this will be −const meals = ["breakfast", "lunch", "dinner"]; const ingredients ... Read More
Sometimes we want to find the conditional cumulative sums and these conditions can be resetting the cumulative if a particular value occurs. For example, finding the cumulative sum of a variable frame but restarting the sum if 1 occurs. In R, we can do this with the help of with, ave and cumusum function as shown in the below examples.Example1Consider the below data frame:Live Demo> ID Ratings df1 df1Output ID Ratings 1 1 0 2 2 2 3 3 0 4 4 0 5 5 0 6 6 ... Read More
We are required to write a JavaScript function that takes in two arrays of the same length.Our function should then combine corresponding elements of the arrays, to form the corresponding subarray of the output array, and then finally return the output array.If the two arrays are −const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3];Then the output should be −const output = [ ['a', 1], ['b', 2], ['c', 3] ];ExampleThe code for this will be −const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3]; const combineCorresponding = (arr1 = [], arr2 ... Read More