Found 26504 Articles for Server Side Programming

How to remove repeated numbers in sequence in R data frame column?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:12:14

413 Views

To remove repeated numbers in sequence in R data frame column, we can follow the below steps −First of all, create a data frame.Then, use diff function and subsetting with single square brackets to remove repeated numbers in sequence.Example 1Create the data frameLet’s create a data frame as shown below −x

How to split a factor variable into n number of variables equal to factor size with full length in R data frame?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:09:01

436 Views

To split a factor variable into n number of variables equal to factor size with full length in R data frame, we can follow the below steps −First of all, create a data frame.Then, use mtabulate function of qdapTools package to split the factor variable.ExampleCreate the data frameLet’s create a data frame as shown below −factor

How to find the column means by factor levels in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:08:38

4K+ Views

To find the column means by factor levels, we can use summarise function along with mean function after creating the group of factor levels with group_by function.For example, if we have a data frame called df that contains a factor column say F and a numerical column say Num then we can find the mean of Num column by factor levels by using the below given command −df%>%group_by(F)%>%summarise(Average=mean(Num))Example 1Following snippet creates a sample data frame −grp

How to replace total string if partial string matches with another string in R data frame column?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:07:51

1K+ Views

To replace total string if partial string matches with another string in R data frame column, we can follow the below steps −First of all, create a data frame with string column.Then, use gsub function to replace total string if partial string matches with another string.ExampleCreate the data frameLet’s create a data frame as shown below −String

How to find the number of common words between two string vectors in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:04:55

650 Views

To find the number of common words between two string vectors, we would first need to split both the vectors using unlist and strsplit function and then we can apply length function along with intersect function.Check out the below examples to understand how it can be done.Example 1Following snippet creates a vector −x1

How to create separate columns for first and last name in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:03:36

1K+ Views

Most of the times in data analysis, the first name and last name of people are joined together or we can say they are stored in a single space hence we need to separate them to make the data easily readable. To create separate columns for first and last name in R, we can use extract function of tidyr package.Check out the below given examples to understand how it can be done.Example 1Following snippet creates a sample data frame −Names

How to change the row order in an R data frame?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 06:00:02

7K+ Views

To change the row order in an R data frame, we can use single square brackets and provide the row order at first place.For example, if we have a data frame called df that contains 10 rows then we can change the row order by using the command given below −df[c(6:8,2,5,9,10,1,3:4),]Check out the below examples to understand how it works.Example 1Following snippet creates a sample data frame −x1

How to find the number of characters in each row of a string column in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:56:29

1K+ Views

If we have a string column in an R data frame and the strings are mixed with numbers and we want to find the number of characters in each row of the string column then nchar function can be used with gsub function as shown in the below examples.Since R is case sensitive, we need to make sure that we use correct notation for small and upper-case letters while doing this type of analysis.Example 1Following snippet creates a sample data frame −x

How to create histogram like plot with different color of bars in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:52:20

205 Views

To create histogram like plot with different color of bars, we can use col argument and provide the range up to the number of bars we expect in the histogram like plot.For example, if we have a vector of length 10 called X then we can create histogram like plot of X with different color of bars using the command given below −plot(X,type="h",col=1:10,lwd=5)Check out the below example to understand how it works.ExampleTo create histogram like plot with different color of bars, use the following command −x

How to randomly split a vector into n vectors of different lengths in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 05:50:43

819 Views

To randomly split a vector into n vectors of different lengths, we can use sample function and set the repl argument to TRUE.For example, if we have a vector called X of size 100 then we can split it randomly into 10 vectors of different lengths by using the below command −split(X,sample(10,100,repl=TRUE))Example 1To randomly split a vector into n vectors of different lengths, use the following code −x

Advertisements