Remove Rows in an R Data Frame Using Row Names

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

16K+ Views

There are a lot of ways to subset an R data frame and sometimes we need to do it by removing rows. In general, the rows are removed by using the row index number but we can do the same by using row names as well. This can be done by storing the row names that should be removed in a vector and then removing through subsetting with single square brackets as shown in the below examples.ExampleConsider the below data frame:> x y row.names(df) dfOutput x y A ... Read More

Determine Isomorphic Strings in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:08:50

810 Views

Two strings (str1 and str2) are isomorphic if the characters in str1 can be replaced to get str2.For example −const str1 = 'abcde'; const str2 = 'eabdc';These two are an example of isomorphic stringsWe are required to write a JavaScript function that in two strings. The function should determine whether or not the two input strings are isomorphic.Exampleconst str1 = 'abcde'; const str2 = 'eabdc'; const isIsomorphic = (str1 = '', str2 = '') => {    if (str1.length !== str2.length) {       return false;    };    for (let i = 0;    i < str1.length; i++) ... Read More

Count Prime Numbers from 2 to N in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:07:59

436 Views

We are required to write a JavaScript function that takes in a number, say n, as the first and the only argument.The function should then return the count of all the prime numbers from 2 upto the number n.For example −For n = 10, the output should be: 4 (2, 3, 5, 7) For n = 1, the output should be: 0Exampleconst countPrimesUpto = (num = 1) => {    if (num < 3) {       return 0;    };    let arr = new Array(num).fill(1);    for (let i = 2; i * i < num; i++) ... Read More

Finding Trailing Zeros of a Factorial in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:06:56

438 Views

Given an integer n, we have to write a function that returns the number of trailing zeroes in n!.For example −trailingZeroes(4) = 0 trailingZeroes(5) = 1 because 5! = 120 trailingZeroes(6) = 1Exampleconst num = 17; const findTrailingZeroes = num => {    let cur = 5, total = 0;    while (cur

Calculate Average from JSON Data Based on Multiple Filters in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:05:38

1K+ Views

Suppose, we have an array of objects like this −const arr = [    { "SupplierName" : "John", "Category " : "A", "Points" : 3 },    { "SupplierName" : "John", "Category " : "A", "Points" : 11 },    { "SupplierName" : "John", "Category " : "A", "Points" : undefined },    { "SupplierName" : "John", "Category " : "B", "Points" : 2 },    { "SupplierName" : "John", "Category " : "B", "Points" : 6 },    { "SupplierName" : "Praveen", "Category " : "A", "Points" : 3 },    { "SupplierName" : "Praveen", "Category " : "A", ... Read More

Sort Array Based on Min and Max Date in JavaScript

AmitDiwan
Updated on 23-Nov-2020 07:03:07

629 Views

Suppose, we have an array of string dates like this −const arr = [    "2017-01-22 00:21:17.0",    "2017-01-27 11:30:23.0",    "2017-01-24 15:53:21.0",    "2017-01-27 11:34:18.0",    "2017-01-26 16:55:48.0",    "2017-01-22 11:57:12.0",    "2017-01-27 11:35:43.0" ];We are required to write a JavaScript function that takes in one such array. The function should find the oldest and the newest date from this array.And then the function should finally return an object containing those two dates.Exampleconst arr = [    "2017-01-22 00:21:17.0",    "2017-01-27 11:30:23.0",    "2017-01-24 15:53:21.0",    "2017-01-27 11:34:18.0",    "2017-01-26 16:55:48.0",    "2017-01-22 11:57:12.0",    "2017-01-27 11:35:43.0" ]; const ... Read More

Group a JavaScript Array

AmitDiwan
Updated on 23-Nov-2020 07:01:10

332 Views

Suppose, we have a JavaScript array like this −const data = [    {       "dataId": "1",       "tableName": "table1",       "column": "firstHeader",       "rows": [          "a", "b", "c"       ]    },    {       "dataId": "2",       "tableName": "table1",       "column": "secondHeader",       "rows": [          "d", "e", "f",       ]    }, {       "dataId": "3",       "tableName": "table2",       "column": "aNewFirstHeader",     ... Read More

Transform Multiple Spaces in a String to One Space in JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:58:05

320 Views

We have to write a JavaScript program that takes a variable user string through an input in HTML. Then through JavaScript the program should check for more than one consecutive spaces in the string.And the program should replace all such instances of more than one consecutive spaces with only one space.We can use a regular expression as the first parameter of replace. /\s{2, }/g to achieve the desired results. Let us write the code for this function −Example Live Demo REMOVE SPACES    function removeSpaces() {       var textInput = insertText.value;   ... Read More

Sorting Only a Part of an Array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:56:13

887 Views

We are required to write a JavaScript function that takes in an array of strings as the first argument and two numbers as second and third argument respectively.The purpose of our function is to sort the array. But we have to sort only that part of the array that falls between the start and end indices specified by second and third argument. Keeping all the other elements unchanged.For example −const arr = ['z', 'b', 'a']; sortBetween(arr, 0, 1);This function should sort the elements at 0 and 1 index only. And the array should become −const output = ['b', 'z', 'a'];Exampleconst ... Read More

Grade Book Challenge in JavaScript

AmitDiwan
Updated on 23-Nov-2020 06:55:03

342 Views

We are required to write a function that finds the mean of the three scores passed to it and returns the letter value associated with that grade according to the following table.Exampleconst findGrade = (...scores) => {    const {       length    } = scores;    const sum = scores.reduce((acc, val) => acc + val);    const score = sum / length;    if (score >= 90 && score = 80 ) {       return 'B';    } else if (score >= 70 ) {       return 'C';    } else if (score >= 60) {       return 'D';    } else{       return 'F';    }; } console.log(findGrade(5,40,93)); console.log(findGrade(30,85,96)); console.log(findGrade(92,70,40));OutputAnd the output in the console will be −F C D

Advertisements