Nizamuddin Siddiqui has Published 2307 Articles

How to remove rows using character column that has elements of size less than 3 in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 05-Mar-2021 05:20:38

430 Views

To find the number of characters in character vector elements or the elements in a character column of an R data frame, we can use nchar function. Therefore, if we want to remove rows that has elements of size less than 3 we would need to use the same function ... Read More

How to add a new column to a data frame using mutate in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 05-Mar-2021 05:15:15

827 Views

The mutate function of dplyr package in R can help us to add a new column to a data frame and the benefit of using mutate is that we can decide the position of the new column during the addition. For example, if we have a data frame called df ... Read More

How to preserve data frame structure after applying a function in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 05-Mar-2021 05:09:17

270 Views

When we apply a function using apply family, by default the output is not in the form of a data frame. If we want to preserve the original data frame structure then we need to set the application of the apply family by setting it to the original data frame ... Read More

How to merge two matrices by combining rows in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 20:55:21

328 Views

By combining rows means that we want to concatenate rows of matrices but create separate columns as in the original matrices. For example, if we have two matrices say M1 and M2 as shown below −M1 1 2 3 3 2 1 M2 2 3 5 1 2 3Then ... Read More

How to find the unique rows in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 20:44:08

1K+ Views

A unique row in an R data frame means that all the elements in that row are not repeated with the same combination in the whole data frame. In simple words, we can say that if we have a data frame called df that contains 3 columns and 5 rows ... Read More

How to find the row and column index of a character value in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 20:19:52

4K+ Views

To find the row and column index for a numerical value in an R data frame we use which function and if the value is character then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called ... Read More

How to split month and year from 6-digit numbers in an R data frame column?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 20:05:07

382 Views

Sometimes we get data that is not in the form to proceed with the analysis and one such situation is dates stored in 6-digit numbers as 202105 that represents fifth month of year 2021 instead of date format as 2021/05. Therefore, we need to split the date and extract the ... Read More

How to filter single column of a matrix with column name in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 19:25:51

1K+ Views

To filter a single column of a matrix in R if the matrix has column names, we can simply use single square brackets but this will result in a vector without the column name. If we want to use the column name then column name or column number needs to ... Read More

How to truncate character vector with three dots after n characters in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 19:21:59

141 Views

To truncate character vector with three dots after n characters can be done with the help of str_trunc function of stringr package. For example, if we have a character vector say x and each value containing 10 characters then truncating those values with three dots after 5 characters can be ... Read More

How to assign a column value in a data frame based on another column in another R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 04-Mar-2021 19:18:25

4K+ Views

To assign a column value based on another column, we can use ifelse function. The ifelse function checks whether the value in one column of one data frame matches the value in another column of another data frame by using equal sign (==) and then replace the original value with ... Read More

Advertisements