Found 26504 Articles for Server Side Programming

How to create transparent bar plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:24:21

3K+ Views

To create transparent barplot using ggplot2, we can use alpha argument inside geom_bar function. For example, if we have a data frame called df that contains a categorical column say x and a numerical column say count then the bar plot with transparency can be created by using the command ggplot(df,aes(x,y))+geom_bar(alpha=0.1,stat="identity")ExampleConsider the below data frame − Live Demox

What is the difference between class and typeof function in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:24:02

6K+ Views

The class function in R helps us to understand the type of object, for example the output of class for a data frame is integer and the typeof of the same object is list because data frames are stored as list in the memory but they are represented as a data frame. Check out the below examples with multiple type of objects to understand the differences.Example1 Live Demox1

How to recode factors in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:23:43

315 Views

Sometimes we have factor levels that can be combined or we want to group those levels in a single level. It is mostly done in situations where we have only one value for a particular factor level or there exists some theoretical concept that leads to combining the factor levels. For example, if we have a data frame called df that contains a factor column say x having four categories A, B, C, and D then they can be grouped into A and B as −df$x[df$x %in% c("A","B")]

How to delete a list element that only contains NA in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:03:31

517 Views

To delete a list element that only contains NA, we can use Filter function with Negate function. For example, if we have a list called LIST that contains one or more elements having all NA’s then we can delete those elements using the command −Filter(Negate(anyNA),LIST)Example1Consider the below list − Live DemoList1

How to find the object size in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:02:50

5K+ Views

To find the object size in R, we can use object.size function. For example, if we have a data frame called df then the size of df can be found by using the command object.size(df). Similarly, if we have a vector say x then it’s size can be found using object.size(x) and for a matrix M it can be object.size(M).Example1Consider the below data frame − Live Demox

How to detect multicollinearity in categorical variables using R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:02:06

1K+ Views

The multicollinearity is the term is related to numerical variables. It means that independent variables are linearly correlated to each other and they are numerical in nature. The categorical variables are either ordinal or nominal in nature hence we cannot say that they can be linearly correlated.ExampleConsider the below data frame − Live Demox

How to subset a data.table object using a range of values in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 12:54:18

3K+ Views

To subset a data.table object using a range of values, we can use single square brackets and choose the range using %between%. For example, if we have a data.table object DT that contains a column x and the values in x ranges from 1 to 10 then we can subset DT for values between 3 to 8 by using the command DT[DT$x %between% c(3,8)].Example1Loading data.table package and creating a data.table object −library(data.table) x1

How to find prime factors of a number in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:07:03

992 Views

A prime number is the number that is only divisible by itself and one. These prime numbers can also divide other numbers hence they become a factor of those numbers. For example, 5 is a prime number and it also divides 20. To find the prime factors of a number, we can use primeFactors function of numbers package.Exampleslibrary(numbers)primeFactors(100)[1] 2 2 5 5primeFactors(1000)[1] 2 2 2 5 5 5 primeFactors(32547)[1] 3 19 571primeFactors(12354767)[1] 17 726751 primeFactors(21457)[1] 43 499primeFactors(99)[1] 3 3 11 primeFactors(365748)[1] 2 2 3 29 1051primeFactors(214687)[1] 11 29 673 primeFactors(3587497)[1] 3587497primeFactors(35874)[1] 2 3 3 1993 primeFactors(268713)[1] 3 3 73 409primeFactors(298473)[1] ... Read More

How to find the column mean by excluding NA’s and if all values are NA then output NA in R data frame?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 12:59:35

847 Views

To find the column mean by excluding NA’s can be easily done by using na,rm but if we want to have NA if all the values are NA then it won’t be that straight forward. Therefore, in such situation, we can use ifelse function and return the output as NA if all the values are NA as shown in the below examples.Example1Consider the below data frame − Live Demox1

How to find the sum of non-missing values in an R data frame column?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 12:57:49

12K+ Views

To find the sum of non-missing values in an R data frame column, we can simply use sum function and set the na.rm to TRUE. For example, if we have a data frame called df that contains a column say x which has some missing values then the sum of the non-missing values can be found by using the command sum(df$x,na.rm=TRUE).Example1Consider the below data frame − Live Demox1

Advertisements