Convert String Vector to Integer Vector in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:34:25

2K+ Views

A string vector contains element inside double-quotes and an integer vector does not have any quotes. Sometimes integer values are stored in double-quotes hence the vector of these values is treated as a string vector in R but we need the integer values to perform mathematical operations. Therefore, we can use as.integer function to convert the string vector into an integer vector.Example1Live Demo> x1 x1Output[1] "3" "2" "1" "2" "1" "1" "1" "1" "1" "1" "3" "3" "3" "1" "2" "1" "1" "2" [19] "2" "3" "3" "3" "3" "2" "3" "3" "3" "2" "1" "2" "3" "3" "2" "1" ... Read More

Sort Array and Set Default Value in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:32:58

378 Views

We are required to write a JavaScript function that takes in an array of literal values as the first argument and a string as the second argument.Our function should sort the array alphabetically but keeping the string provided as the second argument (if it exists in the array) as the first element, irrespective of the text it contains.ExampleThe code for this will be −const arr = ["Apple", "Orange", "Grapes", "Pineapple", "None", "Dates"]; const sortKeepingConstants = (arr = [], text = '') => {    const sorter = (a, b) => {       return (b === text) - (a ... Read More

Find Mean of Square Matrix Elements Excluding Diagonal in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:32:40

289 Views

There are many ways to find the mean of a matrix elements by excluding diagonal elements, this mean is actually the mean of lower triangular matrix and the upper triangular matrix. We can simply use mean function by creating a vector of lower and upper triangular matrix as shown in the below examples.Example1Live Demo> M1 M1Output   [, 1] [, 2] [, 3] [, 4] [1, ] 1    6    3    6 [2, ] 8    5    3    4 [3, ] 5    4    4    6 [4, ] 5    5    3    4 ... Read More

Join Two Objects by Key in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:31:41

857 Views

Suppose, we have two child and parent JSON arrays of objects like these −const child = [{    id: 1,    name: 'somename',    parent: {       id: 2    }, }, {    id: 2,    name: 'some child name',    parent: {       id: 4    } }]; const parent = [{    id: 1,    parentName: 'The first',    child: {} }, {    id: 2,    parentName: 'The second',    child: {} }, {    id: 3,    parentName: 'The third',    child: {} }, {    id: 4,    parentName: 'The ... Read More

Form a Sequence Out of an Array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:28:25

829 Views

Suppose we have a sorted array of numbers like this where we can have consecutive numbers.const arr = [1, 2, 3, 5, 7, 8, 9, 11];We are required to write a JavaScript function that takes one such array.Our function should form a sequence for this array. The sequence should be such that for all the consecutive elements of the array, we have to just write the starting and ending numbers replacing the numbers in between with a dash (-), and keeping all other numbers unchanged.Therefore, for the above array, the output should look like −const output = '1-3, 5, 7-9, ... Read More

Create Bar Plot with Black Border Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:28:24

948 Views

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

Reduce a Matrix in R to Echelon Form

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:26:17

1K+ Views

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

Convert Array of Objects to Array of Arrays in JavaScript

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

905 Views

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

Trim and Split String to Form Array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:24:18

889 Views

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

Remove Rows Based on Blanks in a Column from a Data Frame in R

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:24:09

5K+ Views

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

Advertisements