Found 26504 Articles for Server Side Programming

How to find the log of each value in columns if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:38:37

271 Views

To find the log of each value if some columns are categorical in R data frame, we can follow the below steps −First of all, create a data frame.Then, use numcolwise function from plyr package to find the log if some columns are categorical.ExampleCreate the data frameLet’s create a data frame as shown below −Level

How to find the cross product of two vectors in R by adding the elements?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:38:00

658 Views

To find the cross product of two vectors in R by adding the elements, we can calculate outer product by using %o% operator.For example, if we have two vectors say x and y then cross product of these two vectors by adding the elements can be found by using the command given below −x%o%yCheck out the below examples to understand how it works.Example 1To find the cross product of two vectors in R by adding the elements, use the code given below −x1

How to create sample space of throwing two dices in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:33:30

507 Views

When we through two dices the sample space has thirty-six outcomes as shown below −(1,1),(1,2),(1,3),(1,4),(1,5),(1,6) (2,1),(2,2),(2,3),(2,4),(2,5),(2,6) (3,1),(3,2),(3,3),(3,4),(3,5),(3,6) (4,1),(4,2),(4,3),(4,4),(4,5),(4,6) (5,1),(5,2),(5,3),(5,4),(5,5),(5,6) (6,1),(6,2),(6,3),(6,4),(6,5),(6,6)Now, to create the sample space of throwing two dices in R, we can use the below mentioned command −expand.grid(1:6,1:6) ExampleTo create sample space of throwing two dices in R, use the command given below −expand.grid(1:6,1:6)OutputIf you execute the above given command, it generates the following output −  Var1 Var2 1  1   1 2  2   1 3  3   1 4  4   1 5  5   1 6  6   1 7  1   2 8  2   2 9  3   2 10 4   2 11 5   2 12 6   2 13 1   3 14 2   3 15 3   3 16 4   3 17 5   3 18 6   3 19 1   4 20 2   4 21 3   4 22 4   4 23 5   4 24 6   4 25 1   5 26 2   5 27 3   5 28 4   5 29 5   5 30 6   5 31 1   6 32 2   6 33 3   6 34 4   6 35 5   6 36 6   6

How to change the position of X-axis in base R plot?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:37:30

4K+ Views

By default, the position of X-axis is at the bottom and we know that it is always there in all software/programming tools. If we want to change the position of X-axis in base R plot then we can use axis function with pos argument. The pos argument will decide the position of the X-axis on Y-axis.Check out the Example mentioned below to understand how it can be done.ExampleTo change the position of X-axis in base R plot, use the following command −plot(1:10)OutputIf you execute the above given snippet, it generates the following Output −To create a point chart with X-axis ... Read More

How to find the absolute maximum of each group in data.table object in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:34:20

230 Views

To find the absolute maximum of each group in data.table object in R, we can follow the below steps −First of all, create a data.table object.Then, use summarise_each function of dplyr package along with which.max and abs function to find the absolute maximum of each group after grouping with group_by.Example 1Create the data.table objectLet’s create a data.table object as shown below −library(data.table) Class

How to remove dot at last position from every value in R data frame column?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:28:15

1K+ Views

To remove dot at last position from every value in R data frame column, we can follow the below steps −First of all, create a data frame with a column having dot at last position in every value.Then, use gsub function to remove the dot at last position from every value in the column.ExampleCreate the data frameLet’s create a data frame as shown below −x

Apply a mathematical operation on rows of an R data frame stored in a list.

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:28:31

1K+ Views

To apply a mathematical operation such as multiplication, division, subtraction and addition on rows of an R data frame stored in a list, we can use apply function.For Example, if we have a list called LIST that contains a data frame df with two columns say X and Y and we want to add rows of df then we can use the command mentioned below −apply(df,1,function(x) x["X"]+x["Y"])Check out the Example given below to understand how it can be done.ExampleFollowing snippet creates a sample data frames: −x1

How to find the position of minimum of each numerical column if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:25:43

119 Views

To find the position of minimum of each numerical column if some columns are categorical in R data frame, we can follow the below steps −First of all, create a data frame.Then, use numcolwise function from plyr package to find the minimum of each numerical column if some columns are categorical.Example 1Create the data frameLet’s create a data frame as shown below −factor1

How change the color of facet title using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:22:07

737 Views

To change the color of facet title using ggplot2 in R, we can use theme function with strip.text.x. argument.For Example, if we have a data frame called df that contains three columns say X, Y and F where F is a factor column then we can create facetted scatterplots between X and Y for values in F having different colored facet title by using the below mentioned command with facet title in different color −ggplot(df, aes(X, Y))+geom_point()+facet_wrap(~F)+theme(strip.text.x=element_text(colour="red"))ExampleFollowing snippet creates a sample data frame −PriceRead More

How to multiply corresponding values from two matrices in R?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:19:48

275 Views

To multiply corresponding values from two matrices in R, we can follow the below steps −First of all, create two matrices.Then, use mapply function to multiply corresponding values from those two matrices.ExampleCreate the first matrixLet’s create a matrix as shown below −M1

Advertisements