Replace a Value in an R Vector

Nizamuddin Siddiqui
Updated on 08-Sep-2020 11:02:04

10K+ Views

To replace a value in an R vector, we can use replace function. It is better to save the replacement with a new object, even if you name that new object same as the original, otherwise the replacements will not work with further analysis. As you can see in the object x5(in examples), when we replaced 5 with 3, the previous replacement of -1 with 0 returned as in original vector. Therefore, we should save it in a new object.Examples Live Demo> x1 x1Output[1] 1 2 3 4 5 6 7 8 9 10 > replace(x1, x1==5, 10)Output[1] 1 2 3 ... Read More

Add New Column in R Data Frame with Count Based on Factor Column

Nizamuddin Siddiqui
Updated on 08-Sep-2020 10:54:26

3K+ Views

While doing the data analysis, often we have to deal with factor data and we might want to find the frequency or count of a level of factor and the other variable combination. This helps us to make comparison within and between factor levels. Therefore, we can add a new column as count to find the required frequency and it can be done by using group_by and mutate function of dplyr package.ExampleConsider the below data frame − Live Demo> Group Rating df head(df, 20)Output   Group Rating 1    A    1 2    B    6 3    C    2 ... Read More

Create Cumulative Sum Chart with Count on Y-Axis in R using ggplot2

Nizamuddin Siddiqui
Updated on 08-Sep-2020 10:43:14

2K+ Views

Cumulative sums are often used to display the running totals of values and these sums also help us to identify the overall total. In this way, we can analyze the variation in the running totals over time. To create the cumulative sum chart with count on Y-axis we can use stat_bin function of ggplot2 package.ExampleConsider the below data frame − Live Demo> x df head(df, 20)Output      x 1 1.755900133 2 1.185746239 3 0.821489888 4 1.358420721 5 2.719636441 6 2.885153151 7 1.131452570 8 0.302981998 9 0.433865254 10 2.373338327 11 0.428436149 12 1.835789725 13 2.600838211 14 2.108302471 15 1.164818373 16 1.547473189 ... Read More

Handle Missing Values for Correlation Matrix in R

Nizamuddin Siddiqui
Updated on 08-Sep-2020 10:39:12

3K+ Views

Often the data frames and matrices in R, we get have missing values and if we want to find the correlation matrix for those data frames and matrices, we stuck. It happens with almost everyone in Data Analysis but we can solve that problem by using na.omit while using the cor function to calculate the correlation matrix. Check out the examples below for that.ExampleConsider the below data frame − Live Demo> x1 x2 x3 x4 df head(df, 20)Output x1     x2    x3    x4 1 2 2.6347839 4 2.577690 2 3 0.3082031 1 6.250998 3 1 0.3082031 3 7.786711 4 ... Read More

Increase Line Width in Boxplot Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 08-Sep-2020 10:33:22

3K+ Views

When we create a boxplot using ggplot2, the default width of the lines in the boxplot is very thin and we might want to increase that width to make the visibility of the edges of the boxplot clearer. This will help viewers to understand the edges of the boxplot in just a single shot. We can do this by using lwd argument of geom_boxplot function of ggplto2 package.ExampleConsider the below data frame − Live Demo> ID Count df head(df, 20)Output ID Count 1 S1 20 2 S2 14 3 S3 17 4 S4 30 5 S1 17 6 S2 23 7 S3 ... Read More

Demonstrate Nested Loops with Return Statements in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:58:48

662 Views

Here’s an example with two loops, outer and inner −Examplelet demoForLoop = ()=>{    for(var outer=1;outer

Convert String to Camel Case with JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:52:41

1K+ Views

In order to convert string into camel case, you need to lowercase the first letter of the word and the first letter of the remaining words must be in capital.Following is the code to convert any string into camel case −Examplefunction convertStringToCamelCase(sentence) {    return sentence.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g,    function(camelCaseMatch, i) {       if (+camelCaseMatch === 0)          return "";       return i === 0 ? camelCaseMatch.toLowerCase() :       camelCaseMatch.toUpperCase();    }); } console.log(convertStringToCamelCase("Add two variables"));To run the above program, you need to use the following command −node fileName.js.Here, my file name is ... Read More

Display Only the Current Year in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:51:37

490 Views

To display only the current year, use getFullYear(). Following is the code −Example Live Demo Document The Current Year=    document.getElementById("theCurrentYear").innerHTML = new    Date().getFullYear(); To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −

Strip Quotes with JavaScript to Convert into JSON Object

AmitDiwan
Updated on 07-Sep-2020 08:50:06

2K+ Views

For this, you can use replace() along with parse(). Following is the code −Examplevar studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("The actual object="); console.log(studentDetails); var removeFirstAndLast = studentDetails.substring(1, studentDetails.length-1) var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`) console.log(removeDoubleQuotes) var output = JSON.parse(removeDoubleQuotes); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo103.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo103.js The actual object= "{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}" {"name": "John", "subjectName": "Introduction To JavaScript"} { name: 'John', subjectName: 'Introduction To JavaScript' }Read More

Copy Objects with Object.assign() in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:48:54

199 Views

The Object.assign() method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'Set' on the target.The syntax is as follows −Object.assign(target, ...source objects);Following is the code to copy object −Examplevar object = {first: second => second + 1000} var object2= Object.assign({}, object); console.log("The result="); console.log(object2.first(1000));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo102.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo102.js The result= 2000Read More

Advertisements