Found 2038 Articles for R Programming

How to create duplicate matrices and merge them together in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:38:34

291 Views

To create duplicate matrices, we can use replicate function that will repeat the original matrix and if we want to merge those matrices together then we can use rbind with do.call. For example, if we have a matrix called M then creation of it’s one duplicate and merging them together can be done using the command −do.call(rbind,replicate(2,M,simplify=FALSE))Example Live DemoM

How to find the opposite of %in% in R?

Nizamuddin Siddiqui
Updated on 06-Mar-2021 13:28:52

4K+ Views

To find the opposite of %in%, we can use negation operator ! (exclamation sign). For example, if we have a data frame df that contains a column say x then to subset df by excluding some values (say 2, 3) we can use the command subset(df,!(x %in% c(2,3))).Example1Consider the below data frame − Live Demox1

How to change the size of dots in dotplot created by using ggplot2 in R?

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

2K+ Views

To change the size of dots in dotplot created by using ggplot2, we can use binwidth argument inside geom_dotplot. For example, if we have a data frame called df that contains a column x for which we want to create the dotplot then the plot with different size of dots can be created by using the command ggplot(df,aes(x))+geom_dotplot(binwidth=2).ExampleConsider the below data frame − Live Demox

How to create transparent bar plot using ggplot2 in R?

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

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

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

203 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

342 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

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

Advertisements