Found 33676 Articles for Programming

How to combine two factor vectors to create one in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 14:13:20

3K+ Views

To combine two factor vectors, we can extract the unique levels of both the vectors then combine those levels. This can be done by using unique function. Also, we can set the levels of the original vectors to the combination of the levels, in this way, we can complete both the vectors with missing levels. Check out the examples below to understand how it works.Example1x1

How to check in R whether a matrix element is present in another matrix or not?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 14:11:03

333 Views

We can use %in% to check whether a matrix element is present in another matrix or not. For example, suppose we have two matrices defined as −   M1 1 2 3 1 2 3 1 2 3    M2 1 2 3 4 5 6 7 8 9Then M1%in%M2 will return −[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUEBut M2%in%M1 will return −[1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSEExample1 Live DemoM1

How to display xtable values in scientific form in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 14:08:58

325 Views

The xtable function of xtable package creates a Latex table. We can use digits argument with negative sign to convert the values in the original table into scientific form. For example, if we have a data frame defined as df then we can read it with xtable as xtable(df,digits=-10).Loading xtable package −library(xtable)Example1data1

How to convert a list into an array in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 14:06:59

729 Views

To convert a list into an array then firstly we need to unlist the list elements and then use the array function. For example, if we have a list defined as List then it can be converted into an array using the command array(unlist(List)). Suppose the list contain the elements as shown below −1 2 3 4 5 1 2 3 4 5 1 2 3 4 5If we convert this list into an array then the output will be 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5.Example1List1

How to convert a data frame row into character vector in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 14:04:58

847 Views

To create a character vector in R we can enclose the vector values in double quotation marks but if we want to use a data frame row values to create a character vector then as.character function can be used. For example, if we have a data frame df then the values in first row of the df can form a character vector using as.character(df[1,]).Example Live DemoConsider the below data frame −set.seed(3232) x

How to find the position of minimum value in a vector that contains integers as strings in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 14:00:12

185 Views

If integer vector is read as strings and we want to find the position of the minimum value in that vector then we need to use as.numeric along with the vector to read it as a numeric vector then use which function for finding the position of the minimum value. For example, if we have a vector x that contains first ten integers as strings then to find the position of the minimum we can use which(as.numeric(x)==min(as.numeric(x))).Example1x1

How to extract the strings between two words in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:58:13

3K+ Views

While dealing with text data, we sometimes need to extract values between two words. These words can be close to each other, at the end sides or on random sides. If we want to extract the strings between two words then str_extract_all function of stringr package can be used.Loading stringr package −library(stringr)Example1 Live Demox1

How to extract a data frame’s column value based on a column value of another data frame in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:56:08

3K+ Views

In data analysis, we encounter many problems with a lot of variations among them. One such problem is we have some information in a place that needs to be checked through a different place and these places can be data frames. Therefore, we can to find a data frame’s column value based on a column value of another data frame. In R, we can easily do it with the help of which function.Example Live DemoConsider the below data frame −set.seed(12121) x1

How to create sets using vector values in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:54:34

2K+ Views

A set in mathematics is defined as the collection of unique elements and the order of the elements does not matter. In R, we can create sets using set_power function of sets package. For example, if we have a vector x that contains A, B, C then the sets using the vector x can be created by using set_power(x).Loading sets package −library(sets)Examplesx1

What is the difference between creating a matrix by using matrix function or as.matrix function in R?

Nizamuddin Siddiqui
Updated on 06-Nov-2020 13:53:43

254 Views

The difference between as.matrix and matrix function is that nrow argument or ncol argument are not helpful with as.matrix function but with matrix function we can use them. Therefore, we can actual define a matrix with matrix function but if we have a data frame or data table then it can be converted to matrix by using as.matrix function.Examples of creating matrix with as.matrix and matrix functionExample1 Live DemoM

Advertisements