Generate Bernoulli Random Variable in R

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:23:16

7K+ Views

Each value in Bernoulli random variable represents success or a failure for a single trial that makes it different from Binomial random variable because a Binomial random variable represents number of success or failure for a number of trials. To generate a Bernoulli random variable, we can use rbinom function but we need to pass 1 for size argument.Example Live Demorbinom(120, 1, 0.71)Output[1] 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 0 ... Read More

Count Occurrences of Unique Values in an R Data Frame

Nizamuddin Siddiqui
Updated on 19-Oct-2020 14:17:05

846 Views

A data frame in R can have infinite number of unique values and it can also contain many repeated values. Therefore, finding the number of all unique values in the data frame can help us to understand the diversity in the data but this most done in situations where we expect to have repeated elements otherwise it would not make sense. To count the number of occurrences of all unique values, we can use table function along with the unlist as shown in the below examples.Consider the below data frame −Example Live Demox1

Homogeneity of Variance Test for Two-Way ANOVA in R

Nizamuddin Siddiqui
Updated on 19-Oct-2020 13:53:02

1K+ Views

In general, we can say that the homogeneity of variance test is the type of test that compares the variance of two or more variables and finds the significant difference between or among them if exists. For a two-way anova, one of the most commonly used homogeneity of variance test is Levene’s Test and it can be easily done with the help of leveneTest function of car package in base R.Consider the below data frame −Example Live Demoset.seed(151) x1F) group 6 0.6593 0.6835 13

Calculate Number of Elements Greater Than a Certain Value in a Vector in R

Nizamuddin Siddiqui
Updated on 19-Oct-2020 13:50:16

9K+ Views

In data analysis, sometimes we need to count the number of values that are greater than or less than a certain value, and this certain value could be a threshold. For example, we might have a vector that contain values for blood pressure of people and we might want check how many values are greater than 120. In this type of situation, we can use length function as shown in the below examples.Example Live Demox11])Output[1] 9 Example Live Demox25])Output[1] 93Example Live Demox35])Output[1] 42Example Live Demox40])Output[1] 108Example Live Demox51])Output[1] 107Example Live Demox65])Output[1] 31Example Live Demox71])Output[1] 21Example Live Demox84])Output[1] 19Example Live Demox9118])Output[1] 11Example Live Demox105000])Output[1] 68Read More

Similarities Between Different Strings in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:56:30

127 Views

We have two arrays of Numbers, and we are required to write a function intersection() that computes their intersection and returns an array that contains the intersecting elements in any order. Each element in the result should appear as many times as it shows in both arrays.For example:If input is −arr1 = ['hello', 'world', 'how', 'are', 'you']; arr2 = ['hey', 'world', 'can', 'you', 'rotate'];Then the output should be −['world', 'you'];Approach:Had the arrays been sorted, we could have used the two-pointer approach with initially both pointing to 0 the start of the respective array and we could have proceeded with increasing ... Read More

Grouping of Same Kind of Numbers in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:53:57

172 Views

We have an array of numbers like this −const arr = [-1, -2, -1, 0, -1, -2, -1, -2, -1, 0, 1, 0];We are required to write a JavaScript function that counts the consecutive groups of nonnegative (positives and 0) numbers in the array.Like here we have consecutive non negatives from index 3 to 3 (only one element, but still a cluster) which forms one group and then from 9 to end of array forms the second group.So, for this array, the function should return 2.Therefore, let’s write the code for this function −ExampleThe code for this will be −const ... Read More

Finding Pandigital Numbers Using JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:49:16

187 Views

We are required to write a JavaScript function that takes in a string representing a number. The function returns true if the number is pandigital, false otherwise.A pandigital number is a number that contains all digits (0-9) at least once.Therefore, let’s write the code for this function −ExampleThe code for this will be −const numStr1 = '47458892414'; const numStr2 = '53657687691428890'; const isPandigital = numStr => {    let legend = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];    for(let i = 0; i < numStr.length; i++){       if(!legend.includes(numStr[i])){          continue;   ... Read More

Twice Join of Two Strings in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:47:42

149 Views

We are required to write a JavaScript function that takes in two strings; creates and returns a new string with first 2 words of first string, next two words of second string, then first, then second and so on.For example: If the strings are −const str1 = 'Hello world'; const str2 = 'How are you btw';Then the output should be −const output = 'HeHollw o arwoe rlyodu btw';Therefore, let’s write the code for this function −ExampleThe code for this will be −const str1 = 'Hello world'; const str2 = 'How are you btw'; const twiceJoin = (str1 = '', str2 ... Read More

Find the Second Most Frequent Character in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:45:37

283 Views

We are required to write a JavaScript function that takes in a string and returns the character which makes second most appearances in the string.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Hello world, I have never seen such a beautiful weather in the world'; const secondFrequent = str => {    const map = {};    for(let i = 0; i < str.length; i++){       map[str[i]] = (map[str[i]] || 0) + 1;    };    const freqArr = Object.keys(map).map(el => [el, map[el]]);    freqArr.sort((a, b) => b[1] - ... Read More

Construct Array from String Unique Characters in JavaScript

AmitDiwan
Updated on 19-Oct-2020 10:43:20

145 Views

We are required to write a JavaScript function that takes in a string and starts mapping its characters from 0.And every time, the function encounters a unique (non-duplicate) character it should increase the mapping count by 1 otherwise it should map the same number for duplicate characters.For example: If the string is −const str = 'heeeyyyy';Then the output should be −const output = [0, 1, 1, 1, 2, 2, 2, 2];Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'heeeyyyy'; const mapString = str => {    const res = [];   ... Read More

Advertisements