Articles on Trending Technologies

Technical articles with clear explanations and examples

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 716 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

Read More

How to change the abline colour created using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 3K+ Views

To change the abline color created using ggplot2 in R, we can follow the below steps −First of all, create a data frame.Then, use ggplot2 to create the regression line with given slope and intercept.After that, create the same plot with colour argument to change the abline colour.ExampleCreate the data frameLet’s create a data frame as shown below −x

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 2K+ 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 255 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 916 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

Read More

How to scale some columns in data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 506 Views

To scale some columns in data.table object in R, we can follow the below steps −First of all, create a data.table object.hen, subset the columns with single square brackets and use lapply, list and scale function to scale those columns.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) var1

Read More

How to unsplit a split in R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 978 Views

To unsplit a split in R data frame, we can follow the below steps −First of all, create a data frame.Then, use split function to split the data frame.After that, use do.call function along with rbind function unsplit the data frame.ExampleCreate the data frameLet’s create a data frame as shown below −x

Read More

How to reorder the row indices in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Nov-2021 951 Views

If we have a data frame where row indices are not in correct sequence then we can set them to NULL to get the correct sequence of row numbers starting from 1.For example, if we have a data frame called df which has row ordered in an inappropriate way then we can reorder the row indices in correct order by using the command given below −row.names(df)

Read More
Showing 47371–47380 of 61,297 articles
Advertisements