Convert Character Values in R Data Frame Column to Lower Case

Nizamuddin Siddiqui
Updated on 05-Mar-2021 05:32:19

3K+ Views

The character values can be stored in uppercase, lowercase, or a mixture of the two. If we have values that are either in uppercase or the mixture of lower and upper then we can convert those character values to only lowercase by using tolower function. We simply need to pass the vector or column of the data frame inside the tolower function as shown in the below examples.Example1Consider the below data frame −Live Demo> x1 y1 df1 df1Output   x1     y1 1  C  -0.1036851 2  C  -0.6176530 3  B   0.5763786 4  A   0.1943794 5  C   1.1196470 ... Read More

Subset Columns with Less Than Four Categories in R Data Frame

Nizamuddin Siddiqui
Updated on 05-Mar-2021 05:28:48

157 Views

If column is categorical then there can be at least two categories and there is no limit for the total number of categories but it will also depend on the total number of cases. If we have a data frame that contain some categorical columns having more or less categories than 4 then we might want to subset columns having less than four categories. This could be required in situations when we want to subset the data biasedly or have some predefined data characteristics that allows this change. The subset of such columns can be done with the help of ... Read More

Create Frequency Table in Data Frame Format in R

Nizamuddin Siddiqui
Updated on 05-Mar-2021 05:23:24

8K+ Views

To create a frequency table in R, we can simply use table function but the output of table function returns a horizontal table. If we want to read the table in data frame format then we would need to read the table as a data frame using as.data.frame function. For example, if we have a table called T then to convert it into a data frame format we can use the command as.data.frame(T).Example1Live Demo> x1 x1Output[1] 2 0 2 3 2 3 1 2 1 4 0 0 4 4 1 3 1 2 1 3 2 3 2 1 ... Read More

Remove Rows in R Data Frame Based on Character Column Size

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

558 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

976 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

378 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

466 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

488 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

Advertisements