Found 2038 Articles for R Programming

How to round each value to two decimal places in columns if some columns are categorical in R data frame?

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

410 Views

To round each value to two decimal places in columns 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 round each value to two decimal places in columns if some columns are categorical.ExampleCreate the data frameLet’s create a data frame as shown below −Level

How to find the length of sequence vector in R?

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

1K+ Views

A sequence vector is created by using the sequence of numbers such as 1 to 15, 21 to 51, 101 to 150, -5 to 10. The length of this type of vectors can be found only by using the length function.For example, if we have a sequence vector say X then the length of X can be found by using the command given below −length(X)Example 1To find the length of sequence vector in R, use the code given below −x1

Find the percentiles for multiple columns in an R data frame.

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:45:16

1K+ Views

To find the percentiles for multiple columns in R data frame, we can use apply function with quantile function and providing the quantile probabilities with probs argument.For Example, if we have a data frame called df that contains multiple columns and we want to find three percentiles 0.25, 0.70, 0.90 then we can use the command given below −apply(df[],2,quantile,probs=c(0.25,0.70,0.90))Example 1Following snippet creates a sample data frame −x1

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

156 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

517 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

384 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

3K+ 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

130 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

879 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

Advertisements