Remove Rows in R Data Frame Based on Character Column Size

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

547 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 and then subset function will be used to remove the required rows as shown in the below examples.Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output    x1   x2 1  India 1 2  India 2 3  UK    1 4  UK    2 5  China 1 6 ... Read More

Add New Column to Data Frame Using mutate in R

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

965 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 that contains three columns say x, y, a then we can add a new column say z after y using mutate function. To understand how it can be done, check out the below examples.Example1Consider the below data frame −Live Demo> x1 x3 df1 df1Output   x1 x3 1  2  3 2 ... Read More

Preserve Data Frame Structure After Applying Function in R

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

369 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 with single brackets and no arguments as shown in the below examples.Example1Consider the below data frame −Live Demo> df1 df1Output   x1 x2 1  4 2 2  6 2 3  5 2 4  2 1 5  8 4 6  7 2 7  5 3 ... Read More

Merge Two Matrices by Combining Rows in R

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

457 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 merging of these two matrices by combining rows will result in −1 2 3 2 3 5 3 2 1 1 2 3Example1Live Demo> M1 M1Output      [, 1] [, 2] [1, ]  5     2 [2, ]  7     4 [3, ]  3     6 ... Read More

Find Unique Rows in an R Data Frame

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 then all the values in a particular row are not repeated for any other row. The search of this type of rows might be required when we have a lot of duplicate rows in our data set. To do this, we can use group_by_all function of dplyr package as shown ... Read More

Find Row and Column Index of Character Value in R Data Frame

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

5K+ 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 df that contains a value say Data then we can find the row and column index of Data by using the command as which(df=="Data", arr.ind=TRUE).Example1Consider the below data frame −Live Demo> x1 x2 df1 df1Output    x1    x2 1 Female  5 2 Female  5 3 Female  6 4 Female ... Read More

Split Month and Year from 6-Digit Numbers in R Data Frame

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

477 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 month and year from the number. This can be done easily with the help of transform function as shown in the below examples.Example1Consider the below data frame −Live Demo> Date Response1 df1 df1Output   Date    Response1 1 202103   0.946367628 2 202103   1.241718518 3 202101  -0.657920816 4 202103  -0.809622853 ... Read More

Filter Single Column of a Matrix with Column Name in R

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

2K+ 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 be passed with drop=FALSE argument as shown in the below examples.Example1Live Demo> M1 colnames(M1) M1Output      V1 V2 V3 V4 [1, ]  0  0  1  0 [2, ]  1  1  1  1 [3, ]  0  0  0  0 [4, ]  0  1  1  0 [5, ]  1  1  1 ... Read More

Truncate Character Vector with Three Dots After N Characters in R

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

222 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 done by using the command str_trunc(x, 8).Example1Live Demo> x1 x1Output[1] "rstuvwxyz" "rstuvwxyz" "abcbefgh" "rstuvwxyz" "ijklmnopq" "ijklmnopq" [7] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "abcbefgh" [13] "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" "ijklmnopq" "ijklmnopq" [19] "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "abcbefgh" "abcbefgh" "ijklmnopq" [25] "ijklmnopq" "ijklmnopq" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" "rstuvwxyz" [31] "rstuvwxyz" "abcbefgh" "abcbefgh" "rstuvwxyz" "rstuvwxyz" ... Read More

Assign Column Value in Data Frame Based on Another Column in R

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

5K+ 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 the new column if there is no match else returns the original value. Check out the below example to understand how it can be done.ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output x1 x2 1 3 5 2 3 7 3 ... Read More

Advertisements