Extract a Particular Level from Factor Column in an R Data Frame

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:50:59

2K+ Views

To extract a particular level from factor column in an R data frame, we can use levels function with the number of the factor.For Example, if we have a data frame called df that contains a factor column say F then we can find the third level in F by using the below given command −levels(df$F)[2]Example 1Following snippet creates a sample data frame −Grp

Count Each Category in a Data Table Column in R

Nizamuddin Siddiqui
Updated on 09-Nov-2021 06:46:59

523 Views

To find the count of each category in a data.table object column in R, we can follow the below steps −First of all, create a data.table object.Then, use summarise function of dplyr package after grouping along with n.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) factor

Find Percentiles for Multiple Columns in an R Data Frame

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

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

Round Each Value to Two Decimal Places in R Data Frame

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

695 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

Check If a Set is a Subset of Another Set in R

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

771 Views

To check if a set is a subset of another set, we can use all function with %in% operator. For example, if we have two vectors say x and y and we want to check whether x is a subset of y then we can use the command given below −all(x %in% y)Check out the below given examples to understand how it works.Example 1To check if a set is a subset of another set in R, use the snippet given below −set_x1

Find Length of Sequence Vector in R

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

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

Logarithm of Values in R Data Frame Columns

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

291 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

Find Cross Product of Two Vectors in R by Adding Elements

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

684 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

Change 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

Find Absolute Maximum of Each Group in Data Table Object in R

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

275 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

Advertisements