R Programming Articles

Page 45 of 174

How to put space between words that start with uppercase letter in string vector in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To put space between words that start with uppercase letter in string vector in R, we can use gsub function. Since we can have uppercase letters as well as lowercase letters in the string vector hence, we need to properly specify both type of characters for creating the space between words. Check out the below examples to understand how it works.Examplex1

Read More

How to extract strings that contains a particular substring in an R vector?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

Suppose we have a vector that contains multiple string elements and we want to find out which string element has a particular substring. This can be done with the help of grep function. For example, if we have a vector called x that contains five string elements each of varying length then finding which of the elements has a substring say programming then it can be done by using the command grep("programming",x,fixed=TRUE)Examplex1

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 861 Views

If we have a character column in an R data frame then we might want to check whether a particular value exist in the column or not. For example, if we have a gender column then we might want to check whether transgender exists in that column or not. This can be done with the help of grepl function. Check out the below examples to understand how it works.Consider the below data frame −Examplex

Read More

How to create combination of multiple vectors in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To create combination of multiple vectors, we can use expand.grid function. For example, if we have six vectors say x, y, z, a, b, and c then the combination of vectors can be created by using the command expand.grid(x,y,z,a,b,c).Examplex1

Read More

How to get the list of available data frames in R environment?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

When we perform any type of data analysis, there are many types of objects that are created in the R environment such as vector, data frame, matrix, lists, arrays, etc. If we want to get the list of available data frames in R environment then we can use the below command −names(which(unlist(eapply(.GlobalEnv,is.data.frame))))Examplex1

Read More

How to plot time series data with labels in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 606 Views

If we have time series data stored in a data frame then plotting the same as a time series cannot be done directly, also the labels for the series might not be possible directly. Therefore, we first need to convert the data frame to a time series object by using the function ts as shown in the below example and then using the plot function to create the plot, this will display the labels for the series as well.Consider the below data frame −ExampleTime

Read More

How to find the subtotal in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 749 Views

By subtotal we mean finding the sum of values based on grouping column. For example, if we have a data frame called df that contains three numerical columns as x, y, z and one categorical column say Group then the subtotal of x, y, z for each category in Group can be found by using the command aggregate(cbind(x,y,z)~Group,data=df,FUN=sum).Consider the below data frame −Examplex1

Read More

How to collapse data frame rows in R by summing using dplyr?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To collapse data frame rows by summing using dplyr package, we can use summarise_all function of dplyr package. For example, if we have a data frame called df that has a categorical column say Group and one numerical column then collapsing of rows by summing can be done by using the command −df%>%group_by(Group)%>%summarise_all(funs(sum))Consider the below data frame −ExampleGroup

Read More

How to create a subset using character column with multiple matches in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 745 Views

Subsetting is one of the most important aspects of data analysis. One such situation could be subsetting the character column based on multiple values. For example, if a character column of an R data frame has 5 categories then we might want to extract only 2 or 3 or 4 values then it can be done by using the filter function of dplyr package with str_detect function of stringr package.Consider the below data frame −ExampleGroup

Read More

How to change the legend shape using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

By default, the shape of legend is circular but we can change it by using the guides function of ggplot2 package. For example, if we have a data frame with two numerical columns say x and y, and one categorical column Group then the scatterplot between x and y for different color values of categories in categorical column Group having different shape of legends can be created by using the below command −ggplot(df, aes(x, y, color=Group))+geom_point()+guides(colour=guide_legend(override.aes=list(shape=0)))Here, we can change the shape argument value to any value between starting from 0 to 25.Consider the below data frame −Examplex

Read More
Showing 441–450 of 1,740 articles
« Prev 1 43 44 45 46 47 174 Next »
Advertisements