The bar plot can be easily created with the help of geom_bar. But if we want to have a different border for a particular bar then we first need to create the bar plot and store it in an object. After that we need to add the original plot with the bar for which we want to have a black border. Check out the below example to understand how it can be done.ExampleConsider the below data frame:Live Demo> Group Freq df dfOutput Group Freq 1 G1 18 2 G2 27 3 G3 24Loading ggplot2 ... Read More
The echelon form of a matrix is the matrix that has the following characteristics:1. The first non-zero element in each row, called the leading entry, is 1.2. Each leading entry is in a column to the right of the leading entry in the previous row.3. Rows with all zero elements, if any, are below rows having a non-zero element.In R, we can use echelon function of matlib package to find the echelon form of the matrix.ExampleLive Demo> M MOutput [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 8 11 3 10 13 [2, ] ... Read More
Suppose, we have an array of objects like this −const arr = [ {"Date":"2014", "Amount1":90, "Amount2":800}, {"Date":"2015", "Amount1":110, "Amount2":300}, {"Date":"2016", "Amount1":3000, "Amount2":500} ];We are required to write a JavaScript function that takes in one such array and maps this array to another array that contains arrays instead of objects.Therefore, the final array should look like this −const output = [ ['2014', 90, 800], ['2015', 110, 300], ['2016', 3000, 500] ];ExampleThe code for this will be −const arr = [ {"Date":"2014", "Amount1":90, "Amount2":800}, {"Date":"2015", "Amount1":110, "Amount2":300}, {"Date":"2016", "Amount1":3000, "Amount2":500} ]; const arrify ... Read More
Suppose, we have a comma-separated string like this −const str = "a, b, c, d , e";We are required to write a JavaScript function that takes in one such and sheds it off all the whitespaces it contains.Then our function should split the string to form an array of literals and return that array.ExampleThe code for this will be −const str = "a, b, c, d , e"; const shedAndSplit = (str = '') => { const removeSpaces = () => { let res = ''; for(let i = 0; i < str.length; ... Read More
Sometimes data is incorrectly entered into systems and that is the reason we must be careful while doing data cleaning before proceeding to analysis. A data collector or the sampled unit might enter blank to an answer if he or she does not find an appropriate option for the question. This also happens if the questionnaire is not properly designed or blank is filled by mistake. Also, if we have categorical variable then a control category might be filled with blank or we may want to have a blank category to use a new one at later stage. Whatever the ... Read More
We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values inside it.Our function should remove all the repeating values keeping the first instance of repeating value in the array.ExampleThe code for this will be −const arr = [1, 5, 7, 4, 1, 4, 4, 6, 4, 5, 8, 8]; const deleteDuplicate = (arr = []) => { for(let i = 0; i < arr.length; ){ const el = arr[i]; if(i !== arr.lastIndexOf(el)){ arr.splice(i, 1); } else{ i++; }; }; }; deleteDuplicate(arr); console.log(arr);OutputAnd the output in the console will be −[ 7, 1, 6, 4, 5, 8 ]
The replacement of values in a vector with the values in the same vector can be done with the help of replace function. The replace function will use the index of the value that needs to be replaced and the index of the value that needs to be placed but the output will be the value in the vector.Example1Live Demo> x1 x1Output[1] 3 0 1 0 1 1 1 1 2 1Example> replace(x1, c(10), x1[c(1)])Output[1] 3 0 1 0 1 1 1 1 2 3 Example2Live Demo> x2 x2Output[1] 5 1 4 2 3 4 2 4 5 3 6 ... Read More
Suppose we have two arrays of objects, the first of which contains some objects with user ids and user names.The array contains objects with user ids and user addresses.The array is −const arr1 = [ {"id":"123", "name":"name 1"}, {"id":"456", "name":"name 2"} ]; const arr2 = [ {"id":"123", "address":"address 1"}, {"id":"456", "address":"address 2"} ];We are required to write a JavaScript function that takes in two such arrays and merges these two arrays to form a third array.The third array should contain the user id, name, and address object of corresponding users.ExampleThe code for this will be −const ... Read More
The standardization means converting a vector or column of an R data frame in a way such that the mean of the same becomes 0 and the standard deviation becomes 1, that is it should be converted to standard normal distribution. In R, it can be easily done with the help of scale function. Check out the below example to understand how it is done.ExampleConsider the below data frame:Live Demo> set.seed(3665) > x1 x2 x3 x4 x5 x6 df dfOutputx1 x2 x3 x4 x5 x6 1 1.3958185 49.39843 128.5224 3 4.183664 2.33406246 2 1.0467979 48.90103 120.5796 7 3.526731 0.02043217 3 ... Read More
We are required to write a JavaScript function that takes in an array of literal values. Our function should pick all those values from the array that appears exactly twice in the array and return a new array of those elements.ExampleThe code for this will be −const arr = [0, 1, 2, 2, 3, 3, 5]; const findAppearances = (arr, num) => { let count = 0; for(let i = 0; i < arr.length; i++){ const el = arr[i]; if(num !== el){ continue; }; ... Read More