Count Depth Level of Nested JavaScript Objects

AmitDiwan
Updated on 25-Aug-2020 06:25:25

3K+ Views

We have an array of objects, which further have nested objects like this −const arr = [{    id: 0, children: [] }, {       id: 1, children: [{       id: 2, children: [] }, {       id: 3, children: [{          id: 4, children: []       }]    }] }];Our job is to write a recursive function, say assignDepth() that takes in this array and assigns depth property to each nested object. Like the object with id 0 will have depth 0, id 1 will have depth 1 ... Read More

Butterfly Shuffle in JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:20:50

396 Views

A butterfly shuffled array in JavaScript is an array of Numbers that is sorted such that the numbers decrease as we approach the center of array and increase as we approach the end of array. The biggest number is placed at the very first index.Another variation of butterfly shuffled array is where the numbers increase towards the center and decrease towards the end. In this case the smallest number is placed at the very first index.For people who come from a Mathematics background, it’s somewhat relatable to the Guassian distribution.ExampleSuppose we have this array −const arr = [8, 2, 6, ... Read More

Equivalent of Ruby's each_cons in JavaScript

AmitDiwan
Updated on 25-Aug-2020 06:18:21

354 Views

each_cons() - RubyThe each_cons() method of enumerable is an inbuilt method in Ruby that iterates for consecutive N elements starting from each element every time. If no block is given, it returns the enumerator.JS equivalent of each_cons()Suppose we have an array of Number literals (JS equivalent of Ruby’s enumerable in this case), the each_cons function is supposed to be an Array function that executes for each element of the array and accepts a number N (N

Repeat a Simulation to a Fixed Number of Times in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:10:14

1K+ Views

Often, we simulate random values from different distributions in R. The base R provides some inbuilt functions for the same and if we want to repeat the simulation a fixed number of times then we write these inbuilt functions again and again. But we can do multiple simulations using a single line of code with the help of replicate function, that means if we want to simulate ten uniform random variables ten times then it can be done by using replicate function.Examplesreplicate(10, runif(5, 2, 5)) [, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [, 7] [, ... Read More

Create Column in R Data Frame with Cumulative Sum

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:08:58

621 Views

The cumulative sum is used to determine the total sum of a variable or group and helps us to understand the changes in the values of that variable or group over time. While creating the cumulative, we must be sure that the total sum and the cumulative sum of the last value (depending on the direction of sum) are same. We can use mutate function of dplyr package to find the cumulative and create a column for it.ExampleConsider the below data frame −x1

Change Starting and Ending Points of Axes Labels Using Plot Function in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:07:17

829 Views

When we create a plot using plot function, the axes labels are automatically created based on the values of the variables that is being plotted. It is possible to set a limit to the labels for both the axes, X-axis and Y-axis and we can do this by using xlim and ylim options. For example, if we have the variable limits from 0 to 50 for variable that is going to be plotted on X-axis then it can be done as xlim = c(0,50).Exampleset.seed(99) x

Create Rank Variable Using mutate Function of dplyr Package in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:05:29

570 Views

A rank variable is created to convert a numerical variable into ordinal variable. This is useful for non-parametric analysis because if the distribution of the numerical variable is not normal or there are assumptions of parametric analysis that cannot be followed by the numerical variable then the raw variable values are not analyzed directly. To create a rank variable using mutate function, we can use dense_rank argument.ExampleConsider the below data frame −set.seed(7) x1

Extract First Two Characters from a String in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:04:35

2K+ Views

A string can be short or long, also we can have a vector or list of strings as well in R. Extraction of partial string is common when we want to use the strings for single or multiple comparisons. If we want to extract first two characters from a string, we can use substr function and the syntax is substr(“String_object Or String”,start=1,stop=2)Examplesx1

Create Boxplot with Horizontal Lines on Minimum and Maximum in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:01:22

1K+ Views

A boxplot shows the minimum, first quartile, median, third quartile, and maximum. When we create a boxplot with ggplot2 it shows the boxplot without horizontal lines on the minimum and maximum, if we want to create the horizontal lines we can use stat_boxplot(geom= 'errorbar') with ggplot function of ggplot2.ExampleConsider the below data frame −set.seed(101) Gender

Create Scatterplot with Group Colors in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:56:05

729 Views

A scatterplot is the plot that has one dependent variable plotted on Y-axis and one independent variable plotted on X-axis. Sometimes the pair of dependent and independent variable are grouped with some characteristics, thus, we might want to create the scatterplot with different colors of the group based on characteristics. For this purpose, we can use colour argument in ggplot function.ExampleConsider the below data frame −set.seed(123) x

Advertisements