Differences in Two Strings in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:05:21

457 Views

We are required to write a JavaScript function that takes in two strings and find the number of corresponding dissimilarities in the strings. The corresponding elements will be dissimilar if they are not equalExampleLet’s say the following are our strings −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111';ExampleThe code for this will be −const str1 = 'Hello world!!!'; const str2 = 'Hellp world111'; const dissimilarity = (str1 = '', str2 = '') => {    let count = 0;    for(let i = 0; i < str1.length; i++){       if(str1[i] === str2[i]){         ... Read More

Reversing Even Length Words in a String using JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:03:58

365 Views

We are required to write a JavaScript function that takes in a string and reverses the words in the string that have an even number of characters in them.Let’s say the following is our string −const str = 'This is an example string';We want to reverse the even length words of the above string i.e. reverse the following words −This is an stringExampleThe code for this will be −const str = 'This is an example string'; const isEven = str => !(str.length % 2); const reverseEvenWords = (str = '') => {    const strArr = str.split(' ');    return ... Read More

Finding Index of First Non-Consecutive Number in JavaScript

AmitDiwan
Updated on 17-Oct-2020 11:02:26

147 Views

We have to write a function that takes in an array and returns the index of the first nonconsecutive number from it.Like all the numbers will be in an arithmetic progression of common difference 1. But the number, which violates this rule, we have to return its index. If all the numbers are in perfect order, we should return -1.ExampleLet’s write the code for this function −const arr = [1, 2, 3, 4, 5, 6, 8, 9, 10]; const secondArr = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; const findException = (arr) => { ... Read More

Generate Random Samples Rounded to 4 Decimal Places in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:49:47

1K+ Views

Random samples can be generated in many ways such as using discrete and continuous distributions, using integer vectors, using numerical vectors, using character vectors and/or factor vectors, also with columns of a data set. If we have the sample that is continuous in nature then the values are likely to contain many values after decimal point and we can limit those values to 4 or use any other limit using round function.Example Live Demox1

Extract String Before Slash from a Vector in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:48:06

2K+ Views

If a vector contains string values and they are separated with a special character (This special character can be anything, also it is not necessarily to be a special character) and we want to extract only the values that exists before that special then we can use gsub function. For example, if we have a vector x that contain "UT/YT", "IST/IT", "PST/PT" then gsub can help us to extract UT, IST, PST.Examplex1

Create Histogram with Relative Frequency in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:46:49

2K+ Views

The relative frequency histogram can be created for the column of an R data frame or a vector that contains discrete data. For this purpose, we can use PlotRelativeFrequency function of HistogramTools package along with hist function to generate histogram. For example, if we have a vector x for which we want to create a histogram with relative frequencies then it can be done as PlotRelativeFrequency(hist(x)).ExampleConsider the below vector − Live Demox

Cumulative Sum for Factor Levels in R Data Frame

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:42:55

627 Views

Cumulative sums are mostly used in descriptive analysis of data but sometimes we might want to calculate them in understanding the time series analysis for moving sums but it is very rare. If we have a factor column in an R data frame then it would not make sense to find the cumulative sum for all factor levels together, we must find the cumulative sums for each level. This can be easily done by using ave function.ExampleConsider the below data frame − Live Demoset.seed(15) x1

Create a Sequence of Time in Minutes with Date in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:38:27

1K+ Views

To create a sequence of time in minutes with date we can use seq function and define the date and time with as.POSIXct. For example, if we want to generate a sequence of time between 2 pm on tenth October 2020 to 4 pm on the same date then we can use the following code −seq(from=as.POSIXct("2020-10-10 14:00"), to=as.POSIXct("2020-10-10 16:00"), by="min")Example Live Demoseq(from=as.POSIXct("2020-01-01 00:01"), to=as.POSIXct("2020-01-01 01:00"), by="min")Output[1] "2020-01-01 00:01:00 IST" "2020-01-01 00:02:00 IST" [3] "2020-01-01 00:03:00 IST" "2020-01-01 00:04:00 IST" [5] "2020-01-01 00:05:00 IST" "2020-01-01 00:06:00 IST" [7] "2020-01-01 00:07:00 IST" "2020-01-01 00:08:00 IST" [9] "2020-01-01 00:09:00 IST" "2020-01-01 00:10:00 IST" [11] ... Read More

Omit Missing Values and Complete Data Frame Structure in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:36:55

123 Views

When we have alternative missing values in two columns that makes the data frame look filled with values at alternate places in columns as well. In this case, we might want to remove those missing values so that the data frame becomes complete without any missing value. For this purpose we can use na.omit function with transform function as shown in the below examples.ExampleConsider the below data frame − Live Demox1

Create Regression Model Line in a Scatterplot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 09:33:46

344 Views

To add a regression model line in a scatterplot created by using ggplot2, we need to use geom_smooth function to define the line for linear model. For example, if we have a data frame df that contains independent variable x and the dependent variable y then the regression line can be created by using the code −ggplot(df,aes(x,y))+geom_point()+geom_smooth(method="lm")ExampleConsider the below data frame − Live Demoset.seed(133) x

Advertisements