Fill NA with Last Observation in R Data Frame

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:33:35

482 Views

There are multiple ways to fill missing values in data analysis and one of the ways is filling them with the previous value in the same column of the data frame. For example, if we have a column x in data frame df and this columns x contains some NA values then we can fill them with the values in the upper row. This can be done with the help of na.locf function of zoo package.Consider the below data frame −Example Live Demoset.seed(477) x

Create Subset of R Data Frame with Complete Cases of a Particular Column

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:25:49

350 Views

If we have missing values in a data frame then all the values cannot be considered complete cases and we might want to extract only values that are complete. We might want extract the complete cases for a particular column only. Therefore, we can use negation of is.na for the column of the data frame that we want to subset.Consider the below data frame −Example Live Demoset.seed(123) x

Create Random Sample of Rows for a Specific Column Value in R

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:10:04

941 Views

Random sampling is an important part of data analysis, mostly we need to create a random sample based on rows instead of columns because rows represent the cases. To create a random sample of some percentage of rows for a particular value of a column from an R data frame we can use sample function with which function.Consider the below data frame −Example Live Demoset.seed(887) grp

Create Barplot with One Bar Having Different Color in R

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:06:57

558 Views

A bar plot represents discrete data and the bars in the bar plot are usually of same color but we might want to highlight a particular bar based on the characteristics of the data or the objective of the analysis project. For example, if a particular bar represents highly severe situation or highly unimportant situation then we can change the color that particular bar so that people can easily point out that bar.Consider the below data frame −Example Live Demox

Add String Before Each Numeric Value in R Data Frame Column

Nizamuddin Siddiqui
Updated on 14-Oct-2020 09:03:10

4K+ Views

Sometimes the unique identity column of a data frame is not recorded as intended, it contains only numeric values that does not solve the data characteristic purpose. Therefore, we might want to add a string before those numeric values to make the data more sensible for viewers and analysts. This can be easily done with the help of gsub function.Consider the below data frame −Example Live Demoset.seed(111) x1

Excluding Extreme Elements from Average Calculation in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:21:22

205 Views

We are required to write a JavaScript function that takes in an array of Number. Then the function should return the average of its elements excluding the smallest and largest Number.ExampleThe code for this will be −const arr = [5, 3, 5, 6, 12, 5, 65, 3, 2]; const findExcludedAverage = arr => {    const creds = arr.reduce((acc, val) => {       let { min, max, sum } = acc;       sum += val;       if(val > max){          max = val;       };       if(val < min){          min = val;       };       return { min, max, sum };    }, {       min: Infinity,       max: -Infinity,       sum: 0    });    const { max, min, sum } = creds;    return (sum - min - max) / (arr.length / 2); }; console.log(findExcludedAverage(arr));OutputThe output in the console −8.666666666666666

Equality of Corresponding Elements in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:19:50

180 Views

We are required to write a JavaScript function that takes in two arrays of literals. The function should check the corresponding elements of the array. The function should return true if all the corresponding elements of the array are equal otherwise it should return false.ExampleThe code for this will be −const arr1 = [6, 7, 8, 9, 10, 11, 12, 14]; const arr2 = [6, 7, 8, 9, 10, 11, 12, 14]; const areEqual = (first, second) => {    if(first.length !== second.length){       return false;    };    for(let i = 0; i < first.length; i++){       if(first[i] === second[i]){          continue;       }       return false;    };    return true; }; console.log(areEqual(arr1, arr2));OutputThe output in the console −True

Splitting a String into Parts in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:18:15

362 Views

We are required to write a JavaScript function that takes in a string and a number n (such that n exactly divides the length of string) and we need to return an array of string of length n containing n equal parts of the string.ExampleThe code for this will be −const str = 'we will be splitting this string into parts'; const num = 6; const divideEqual = (str, num) => {    const len = str.length / num;    const creds = str.split("").reduce((acc, val) => {       let { res, currInd } = acc;       ... Read More

Find the Second Most Frequent Element in Array using JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:16:41

690 Views

We are required to write a JavaScript function that takes in a string and returns the character from the string that appears for the second most number of times.ExampleThe code for this will be −const arr = [5, 2, 6, 7, 54, 3, 2, 2, 5, 6, 7, 5, 3, 5, 3, 4]; const secondMostFrequent = arr => {    const map = arr.reduce((acc, val) => {       if(acc.has(val)){          acc.set(val, acc.get(val) + 1);       }else{          acc.set(val, 1);       };       return acc;    }, new Map);    const frequencyArray = Array.from(map);    return frequencyArray.sort((a, b) => {       return b[1] - a[1];    })[1][0]; }; console.log(secondMostFrequent(arr));OutputThe output in the console −2

Product of Numbers in a Nested Array in JavaScript

AmitDiwan
Updated on 14-Oct-2020 08:14:52

253 Views

We are required to write a JavaScript function that takes in an array of nested arrays of Numbers and some falsy values (including 0) and some strings as well and the function should return the product of number values present in the nested array. If the array contains some 0s, we should ignore them as well.ExampleThe code for this will be −const arr = [    1, 2, null, [       2, 5, null, undefined, false, 5, [          1, 3, false, 0, 2       ], 4, false    ], 4, 6, 0 ... Read More

Advertisements