Found 33676 Articles for Programming

How to remove the first replicate in a vector using another vector that contains similar elements in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 07:02:58

135 Views

To remove the first replicate in a vector using another vector, we can use match function. For example, if we have a vector x that contain values 0, 1, 2, 3, 4, 5 and 0, 1, 2 are repeated in another vector say then the removal of this replicate will result in values 3, 4, and 5. This can be done by using x[-match(y,x)].Example Live Demox1

How to create a list of matrices in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 06:53:29

3K+ Views

To create a list of matrices, we simply need to find the matrix object inside list function. For example, if we have five matrix objects either of same or different dimensions defined as Matrix1, Matrix2, Matrix3, Matrix4, and Matrix5 then the list of these matrices can be created as −List_of_Matrix

How to create unordered triplets of a vector elements in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 06:43:13

252 Views

To create triplets of a vector elements, we can use combn function. For example, if we have a vector x that contain values 1, 2, 3, 4, 5 then the unordered triplets of x can be created by using combn(x,3). This will create a matrix where the elements of the vector x would not be arranged in a particular order.Example Live Demox1

How to create a data frame with a column having repeated values in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 06:23:46

18K+ Views

To create a data frame with a column having repeated values, we simply need to use rep function and we can repeat the values in a sequence of the values passed or repeating each value a particular number of times. For example, if we have three values 1, 2, 3 then the data frame can be created by repeating these values as 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3 or by repeating the same as 1, 1, 1, 2, 2, 2, 3, 3, 3.Example 1 Live Demox

How to find the index of an element in a matrix column based on some condition in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 06:19:59

344 Views

We might want to find the position of a value in a matrix column which is less than a certain value. This will help us to identify the position of critical or threshold values in each column. For example, if we have a matrix M that contains 5 rows and 5 columns with vales in the range of 1 to 100 then we might want to find the index of values in each column that are less than 50 so that we can understand how many columns have such type of values. In R, we can easily do this by ... Read More

How to subset factor columns in an R data frame?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 06:14:42

784 Views

Subsetting of factor columns can be done by creating an object of all columns using sapply with is.factor to extract only factor column in the future then passing that object into subsetting operator single square brackets. For example, if we have a data frame df that contains three columns x, y, z and two of them say x and y are factor columns then we can use Factors

How to create a scatterplot using ggplot2 with different shape and color of points based on a variable in R?

Nizamuddin Siddiqui
Updated on 17-Oct-2020 06:08:32

542 Views

In general, the default shape of points in a scatterplot is circular but it can be changed to other shapes using integers or sequence or the variable. We just need to use the argument shape inside geom_point function and pass the variable name. For example, if we want to create the scatterplot with varying shapes of a variable x then we can use geom_point(shape=x). And if we want to change the size then integer values can be used.ExampleConsider the below data frame − Live Demoset.seed(151) x

How to remove the row names or column names from a matrix in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:50:58

23K+ Views

To remove the row names or column names from a matrix, we just need to set them to NULL, in this way all the names will be nullified. For example, if we have a matrix M that contain row names and column names then we can remove those names by using the command colnames(M)

How to replicate a matrix by rows in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:35:03

3K+ Views

The replication of matrix by rows means that repeating a matrix one or more times but row-wise. For example, if we have a matrix that contains only one row and three columns then the replication of that matrix three times will repeat that one row three times. This can be done by using rep function along with matrix function as shown in the below example.Example Live DemoM

How to extract website name from their links in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:25:38

233 Views

If we have a list of website links and we want to extract the website name from those links then it is a time-consuming task because we would need to copy each name one-by-one. Therefore, it is better to extract them using a function in R and save time. To extract the website name from the website link, we can use suffix_extract function of urltools package. This will extract the host, subdomain, domain and suffix. And it is known that the domain values are the website names.Loading urltools package −library(urltools)Website links stored in a vector −Web_LinksRead More

Advertisements