Found 33676 Articles for Programming

How to set the position of legend of a ggplot2 graph to left-top side in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 09:26:26

624 Views

To set the position of legend of a ggplot2 graph to left-top side in R, we can follow the below steps −First of all, create a data frame.Then, create a plot using ggplot2 with legend.After that, add theme function to the ggplot2 graph to change the position of legend.Create the data frameLet's create a data frame as shown below − Live Demo> x y Grp df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −          x          y     Grp 1   1.534536456 ... Read More

How to divide the data.table object rows in R by row minimum?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 09:24:13

180 Views

To divide the row values by row minimum in R’s data.table object, we can follow the below steps −First of all, create a data.table object.Then, use apply function to divide the data.table object row values by row minimum.Create the data.table objectLet’s create a data.table object as shown below −> library(data.table) > x y z DT DTOn executing, the above script generates the below output(this output will vary on your system due to randomization) −    x y z 1:  4 1 3 2:  4 3 1 3:  1 3 5 4:  1 5 5 5:  3 3 1 6:  3 ... Read More

How to divide the matrix rows by row minimum in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 09:22:37

119 Views

To divide matrix row values by row minimum in R, we can follow the below steps −First of all, create a matrix.Then, use apply function to divide the matrix row values by row minimum.Create the matrixLet’s create a matrix as shown below − Live Demo> M MOn executing, the above script generates the below output(this output will vary on your system due to randomization) −     [,1] [,2] [,3] [1,]  16    1   18 [2,]  18   12    1 [3,]  12   19   19 [4,]   7    2   18 [5,]  10   14   16 [6,]   8    4    8 [7,]  15   14    4 [8,]   2    1   17 [9,]  11   12    8 [10,] 11   19    6 [11,]  8    2    7 [12,]  8   14    1 [13,]  5   19   18 [14,] 17   14    6 [15,] 13   12   18 [16,]  1   19   14 [17,]  9   19   17 [18,] 10   11    6 [19,]  3    2    2 [20,] 18   15    9 [21,] 11   18   11 [22,]  4   19    7 [23,]  3    9   19 [24,] 12    7   19 [25,]  5    5    2Divide the matrix row values by row minimumUsing apply function to divide the row values of M by row minimum − Live Demo> M M_new M_newOutput          [,1]      [,2]     [,3] [1,]  16.000000  1.000000  18.000000 [2,]  18.000000  12.000000 1.000000 [3,]  1.000000   1.583333  1.583333 [4,]  3.500000   1.000000  9.000000 [5,]  1.000000   1.400000  1.600000 [6,]  2.000000   1.000000  2.000000 [7,]  3.750000   3.500000  1.000000 [8,]  2.000000   1.000000  17.000000 [9,]  1.375000   1.500000  1.000000 [10,] 1.833333   3.166667  1.000000 [11,] 4.000000   1.000000 3.500000 [12,] 8.000000   14.000000 1.000000 [13,] 1.000000   3.800000 3.600000 [14,] 2.833333   2.333333 1.000000 [15,] 1.083333   1.000000 1.500000 [16,] 1.000000   19.000000 14.000000 [17,] 1.000000   2.111111 1.888889 [18,] 1.666667   1.833333 1.000000 [19,] 1.500000   1.000000 1.000000 [20,] 2.000000   1.666667 1.000000 [21,] 1.000000   1.636364 1.000000 [22,] 1.000000   4.750000 1.750000 [23,] 1.000000   3.000000 6.333333 [24,] 1.714286   1.000000 2.714286 [25,] 2.500000   2.500000 1.000000

How to find the critical value of F for regression anova in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 09:19:34

475 Views

To find the critical value of F for regression anova in R, we can follow the below steps −First of all, create a data frame.Then, create the regression model.After that, find the critical value of F statistic using qf function.Create the data frameLet's create a data frame as shown below − Live Demo> x y df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −   x y 1  5 5 2  0 9 3  1 3 4  3 5 5  2 5 6  2 4 7  3 6 8  4 6 ... Read More

How to convert data frame values to their rank in R?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 09:17:48

1K+ Views

To convert data frame values to their rank in R, we can follow the below steps −First of all, create a data frame.Then, find the rank of each value using rank function with lapply.Create the data frameLet's create a data frame as shown below − Live Demo> x1 y1 df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −           x1          y1 1  -0.9045608  1.95315551 2  -1.6222122 -1.19880638 3   0.8618855 -0.33643175 4  -0.3547085 -0.18097356 5  -0.3043621 -0.60342210 6   0.5606001 -0.83618995 7  -1.1752752 ... Read More

How to create a new level using unique levels of a factor in R data frame?

Nizamuddin Siddiqui
Updated on 13-Aug-2021 09:15:47

470 Views

To create a new level using unique levels of a factor in R data frame, we can follow the below steps −First of all, create a data frame with factor column.Then, create a new level for the unique values in the factor column using table and levels function.Create the data frameLet's create a data frame as shown below − Live Demo> Factor df dfOn executing, the above script generates the below output(this output will vary on your system due to randomization) −Factor 1 O 2 D 3 K 4 M 5 C 6 M 7 K 8 F 9 L 10 ... Read More

Golang Program to Determine Recursively Whether a Given Number is Even or Odd

Rishikesh Kumar Rishi
Updated on 02-Aug-2021 07:09:21

346 Views

StepsTake a number from the user and store it in a variable.Pass the number as an argument to a recursive function.Define the base condition as the number to be lesser than 2.Otherwise, call the function recursively with the number minus 2.Then, return the result and check if the number is even or odd.Print the final result.Enter a number: 124Number is even!Enter a number: 567Number is odd!Example Live Demopackage main import (    "fmt" ) func check(n int) bool{    if n < 2 {       return n % 2 == 0    }    return check(n - 2) } ... Read More

Golang Program to Print the Numbers in a Range (1, upper) without Using any Loops

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:54:37

236 Views

StepsDefine a recursive function.Define a base case for that function that the number should be greater than zero.If the number is greater than 0, call the function again with the argument as the number minus 1.Print the number.Enter the upper limit: 512345Enter the upper limit: 1512..15Example Live Demopackage main import (    "fmt" ) func printNo(number int){    if number >= 1{       printNo(number-1)       fmt.Println(number)    } } func main(){    var upper int    fmt.Print("Enter the upper limit: ")    fmt.Scanf("%d", &upper)    printNo(upper) }OutputEnter the upper limit: 5 1 2 3 4 5

Golang Program to Find the Gravitational Force Acting Between Two Objects

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:53:38

432 Views

StepsRead both the masses and the distance between the masses and store them in separate variables.Initialize one of the variables to the value of gravitational constant, G.Then, the formula f=(G*m1*m2)/(r**2) is used to determine the force acting between the masses.Rounding off up to two decimal places, print the value of the force.Enter the first mass: 1000000Enter the second mass: 500000Enter the distance between the centres of the masses: 20Hence, the gravitational force is: 0.08 NEnter the first mass: 90000000Enter the second mass: 7000000Enter the distance between the centres of the masses: 20Hence, the gravitational force is: 105.1 NExplanationUser must enter ... Read More

Golang Program to Find the Area of a Triangle Given All Three Sides

Rishikesh Kumar Rishi
Updated on 31-Jul-2021 15:52:40

550 Views

StepsRead all the three sides of the triangle and store them in three separate variables.Using the Heron's formula, compute the area of the triangle.Print the area of the triangle.Enter first side: 15Enter second side: 9Enter third side: 7Area of the triangle is: 20.69Enter first side: 5Enter second side: 6Enter third side: 7Area of the triangle is: 14.7ExplanationUser must enter all three numbers and store them in separate variables.First, the value of s is found which is equal to (a+b+c)/2Then, the Heron's formula is applied to determine the area of the triangle formed by all three sides.Finally, the area of the ... Read More

Advertisements