Remove Last Few Rows from an R Data Frame

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:38:37

2K+ Views

An R data frame can contain a very large number of rows and we might want to get rid of some rows if they’re not supposed to be helpful in our data analysis. Therefore, we can remove these rows prior to starting the analysis process. We can say that this removal of some rows is a part of data cleaning and obviously data cleaning helps us creating a smooth data set for analysis. In R, we can simply use head function to remove last few rows from an R data frame, also we can store them as a new data ... Read More

Sort R Data Frame Columns in Ascending and Descending Order

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:33:28

284 Views

Sorting of columns of an R data frame is not difficult but sometimes we want to sort them in opposite orders, for example, we might want to sort some columns in ascending order and some in descending order. This variation in sorting purpose makes it a little complicated. Therefore, we can use negation with sort function to sort the columns that we want to sort in descending order.ExampleConsider the below data frame − Live Demoset.seed(111) x1

Change Title Position of Plot in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:27:31

1K+ Views

When we create a plot using plot function, the title of the plot appears on top of the plot while using main argument. If we use title function to create the title of the plot then we can adjust its position in many different ways such as any position between below and top border of the plot.Examplesx

Rescale Continuous Variable to Range 0 to 1 in R

Nizamuddin Siddiqui
Updated on 24-Aug-2020 11:23:03

492 Views

Rescaling a continuous means that we want to standardize it with some properties and if we are using 0 to 1 as a range that represents that property. Most of the times, the objective behind rescaling is we want to nullify the effect of measurement units of the variable under consideration. To rescale so that the range becomes 0 to 1, we can use rescale function of scales package.ExampleLoading scales package −Examplelibrary(scales) x1

Selectively Retrieve Value from JSON Output in JavaScript

AmitDiwan
Updated on 24-Aug-2020 10:08:55

246 Views

We have the following data inside a json file data.json −data.json{    "names": [{       "name": "Ramesh",       "readable": true    }, {       "name": "Suresh",       "readable": false    }, {       "name": "Mahesh",       "readable": true    }, {       "name": "Gourav",       "readable": true    }, {       "name": "Mike",       "readable": false    } ] }Our job is to create a function parseData that takes in the path to this file as one and only argument, ... Read More

Compare Two Sentences Word by Word in JavaScript

AmitDiwan
Updated on 24-Aug-2020 10:06:27

197 Views

The idea here is to take two strings as input and return true if a is substring of b or b is sub string of a, otherwise return false.For example −isSubstr(‘hello’, ‘hello world’) // true isSubstr(‘can I use’ , ‘I us’) //true isSubstr(‘can’, ‘no we are’) //falseTherefore, in the function we will check for the longer string, the one with more characters and check if the other is its substring or not.Here is the code for doing so −Exampleconst str1 = 'This is a self-driving car.'; const str2 = '-driving c'; const str3 = '-dreving'; const isSubstr = (first, second) ... Read More

Convert Binary String to Base 10 Number in JavaScript

AmitDiwan
Updated on 24-Aug-2020 10:01:56

278 Views

We are required to write a JavaScript function that takes in a binary number as a string and returns its numerical equivalent in base 10. Therefore, let’s write the code for the function.This one is quite simple, we iterate over the string using a for loop and for each passing bit, we double the number with adding the current bit value to it like this −Exampleconst binaryToDecimal = binaryStr => {    let num = 0;    for(let i = 0; i < binaryStr.length; i++){       num *= 2;       num += Number(binaryStr[i]);    };   ... Read More

Turn Words into Whole Numbers in JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:53:55

2K+ Views

We have to write a function that takes in a string as one and only argument, and return its equivalent number.For example −one five seven eight -------> 1578 Two eight eight eight -------> 2888This one is pretty straightforward; we iterate over the array of words splitted by whitespace and keep adding the appropriate number to the result.The code for doing this will be −Exampleconst wordToNum = (str) => {    const legend = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];    return str.toLowerCase().split(" ").reduce((acc, val) => {       const index = legend.indexOf(val);     ... Read More

Product of All Other Numbers in an Array in JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:50:42

364 Views

Let’s say, we have to write a function that takes an array of numbers as argument. We have to return a new array with the products of each number except the index we are currently calculating product for.For example, if arr had 5 indices and we were creating the value for index 1, the numbers at index 0, 2, 3 and 4 would be multiplied. Similarly, if we were creating the value for index 2, the numbers at index 0, 1, 3 and 4 would be multiplied and so on.Note − It is guaranteed that all the elements inside the ... Read More

Taking Part from Array of Numbers by Percent in JavaScript

AmitDiwan
Updated on 24-Aug-2020 09:48:23

1K+ Views

We have an array of Number literals like this −const numbers = [10, 6200, 20, 20, 350, 900, 26, 78, 888, 10000, 78, 15000, 200, 1280, 2000, 450];We are supposed to write a function that takes an array of numbers and a number between [0, 100], basically this number represents a certain percent. Let us denote this number by x for now.Now we have to return a subarray of first n elements of the original array that add up equal to or just less than the x % of the total sum of all array elements.Take a simpler example −const ... Read More

Advertisements