Found 2038 Articles for R Programming

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

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

142 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

How to 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

How to highlight a bar in base R histogram?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:41:48

427 Views

To highlight a bar in base R histogram, we need to understand the X-axis values and pass the col argument inside hist function appropriately. We just need to put a separate value for the bar that we want to highlight and set the colouring of the rest of the bars to 0 (that is default in base R). Check out the below examples to understand how it works.Example1> x hist(x,col = c(rep(0,5),4,rep(0,5)))OutputExample2> y hist(y,col = c(rep(0,3),4,rep(0,9)))Output

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

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

385 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

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

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 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

How to find the 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

How to merge two matrices by combining rows in R?

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

332 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

How to convert a binary matrix to logical matrix in R?

Nizamuddin Siddiqui
Updated on 04-Mar-2021 08:33:27

760 Views

A binary matrix contains values such as Yes or NO, 1 or 0, or any other two values that represents opposite mostly and the globally accepted logical values are FALSE and TRUE. Therefore, to convert a binary matrix to logical matrix, we can use ifelse function and convert the one category of binary variable to appropriate logical value and for the rest returns the left-out value. This is a very easy task in R, check out the below examples to understand how it can be done.Example1Live Demo> M1 M1Output[, 1] [, 2] [1, ] "No" "Yes" [2, ] "No" "No" ... Read More

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

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

271 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

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

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

830 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

Advertisements