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")]
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
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
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
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
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
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
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
To convert a list to JSON, we can use toJSON function of jsonlite package. For example, if we have a list called LIST then it can be converted to a JSON by using the command toJSON(LIST,pretty=TRUE,auto_unbox=TRUE). We need to make sure that the package jsonlite is loaded in R environment otherwise the command won’t work.Example Live DemoList
To randomly sample rows from an R data frame using sample_n, we can directly pass the sample size inside sample_n function of dplyr package. For example, if we have data frame called df then to create a random sample of 5 rows in df can be done by using the command −df%>%sample_n(5)Example1Consider the below data frame − Live Demox1