Found 2038 Articles for R Programming

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

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, we can use cut function.Example1 Live DemoConsider the below data frame −set.seed(141) x1

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

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

578 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 be done by using gsub function.Example1 Live Demox1

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

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

9K+ 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 str_sub(x,2,−2).Example1library(stringr) x1

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

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 will be counted by using the command − setDT(df)[,list(Count=.N),names(df)].Example1 Live DemoConsider the below data frame −x1

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

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

97 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 can be used. For example, for a vector x, it will be done as pmax(x,2).Example1 Live Demox1

How to convert multiple columns into single column in an R data frame?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:57:19

9K+ Views

To convert multiple columns into single column in an R data frame, we can use unlist function. For example, if we have data frame defined as df and contains four columns then the columns of df can be converted into a single by using data.frame(x=unlist(df)).Example1 Live DemoConsider the below data frame −x1

How to replace the outliers with 5th and 95th percentile values in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:57:04

294 Views

There are many ways to define an outlying value and it can be manually set by the researchers as well as technicians. Also, we can use 5th percentile for the lower outlier and the 95th percentile for the upper outlier. For this purpose, we can use squish function of scales package as shown in the below examples.Example1library(scales) x1

How to find the percentage of missing values in an R data frame?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:55:10

925 Views

To find the percentage of missing values in an R data frame, we can use sum function with the prod function. For example, if we have a data frame called df that contains some missing values then the percentage of missing values can be calculated by using the command: (sum(is.na(df))/prod(dim(df)))*100Example1 Live DemoConsider the below data frame −x1

How to remove rows for categorical columns that has three or less combination of duplicates in an R data frame?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:55:16

300 Views

In Data Analysis, we sometimes decide the size of the data or sample size based on our thoughts and this might result in removing some part of the data. One such thing could be removing three or less duplicate combinations of categorical columns and it can be done with the help of filter function of dplyr package by grouping with group_by function.Example1 Live DemoConsider the below data frame −set.seed(121) x1

How to create a large vector with repetitive elements of varying size in R?

Nizamuddin Siddiqui
Updated on 08-Feb-2021 12:51:52

330 Views

To create a large vector of repetitive elements of varying size we can use the rep function along with the logical vector as an index. The logical vector that contains TRUE or FALSE will define the selection or omission of the values in the vector created with the help of rep function as shown in the below examples. If the vector created by using rep is larger than the logical vector then the logical vector will be recycled.Example1 Live Demox1

Advertisements