Nizamuddin Siddiqui has Published 2307 Articles

How to convert a data frame with categorical columns to numeric in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 12:02:25

888 Views

We might want to convert categorical columns to numeric for reasons such as parametric results of the ordinal or nominal data. If we have categorical columns and the values are represented by using letters/words then the conversion will be based on the first character of the category. To understand the ... Read More

How to remove rows from data frame in R that contains NaN?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:57:56

12K+ Views

The NaN values are referred to as the Not A Number in R. It is also called undefined or unrepresentable but it belongs to numeric data type for the values that are not numeric, especially in case of floating-point arithmetic. To remove rows from data frame in R that contains ... Read More

How to check whether a column exists in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:57:42

778 Views

If we have very large data set then it is highly that we forget the column names, therefore, we might want to check whether a particular column exists in the data frame or not if we know the column name. For this purpose, we can use grep function that will ... Read More

How to find the frequency of values greater than or equal to a certain value in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:54:46

842 Views

In Data Analysis, we often need to look for less than, less than equal to, greater than, or greater than equal to values to compare them with some threshold. Sometimes we also require the frequency of these values. Therefore, we can use sum function for this purpose. For example, if ... Read More

How to find the counts of categories in categorical columns in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:48:37

5K+ Views

If we have two categorical columns in an R data frame then we can find the frequency/count of each category with respect to each category in the other column. This will help us to compare the frequencies for all categories. To find the counts of categories, we can use table ... Read More

How to create a categorical variable using a data frame column in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:46:57

1K+ Views

If a variable is numerical then it can be converted into a categorical variable by defining the lower and upper limits. For example, age starting from 21 and ending at 25 can be converted into a category say 21−25. To convert an R data frame column into a categorical variable, ... Read More

How to remove only last character from a string vector in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:43:56

570 Views

Sometimes the string vector contains unnecessary characters at the end or at the starting and do not make sense, it is also possible that the string makes sense but nor required there is a spelling mistake. In such type of cases, we need to remove the unnecessary characters. This can ... Read More

How to replace vector values less than 2 with 2 in an R vector?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:43:26

96 Views

If we have a vector that contains values with less than, equal to, and greater than 2 and the value 2 is the threshold. If this threshold value is defined for lower values and we want to replace the values that are less than 2 with 2 then pmax function ... Read More

How to count the number of duplicate rows in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:43:02

3K+ Views

To count the number of duplicate rows in an R data frame, we would first need to convert the data frame into a data.table object by using setDT and then count the duplicates with Count function. For example, if we have a data frame called df then the duplicate rows ... Read More

How to remove the first and last character in a string in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 09-Feb-2021 11:42:41

8K+ Views

To remove the first and last character in a string, we can use str_sub function of stringr package. For example, if a word say tutorialspoint is mistakenly typed as ttutorialspointt and stored in a vector called x then to remove the first and last “t”, we can use the command ... Read More

Advertisements