R Programming Articles

Page 46 of 174

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 766 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

How to create group names for consecutively duplicate values in an R data frame column?

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

The grouping of values can be done in many ways and one such way is if we have duplicate values or unique values then the group can be set based on that. If all the values are unique then there is no sense for grouping but if we have varying values then the grouping can be done. For this purpose, we can use rleid function as shown in the below examples.Example1Consider the below data frame −> x df1 df1Output x 1 2 2 1 3 2 4 2 5 1 6 0 ...

Read More

What are some examples of data sets with missing values in R?

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

Instructors/educators often need to teach missing value imputation to their students; hence they require datasets that contains some missing values or they need to create one. We also have some data sets with missing values available in R such as airquality data in base R and food data in VIM package. There could be many other packages that contain data sets with missing values but it would take a lot of time to explore them. Thus, we have shared the example of airquality and some data sets from VIM package.Example 1head(airquality, 20)Output Ozone Solar.R Wind Temp Month Day 1 41   ...

Read More

How to concatenate string vectors separated with hyphen in R?

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

The concatenation of string vectors will create combination of the values in the vectors thus, we can use them for interaction between/among the vectors. In R, we can use expand.grid along with apply to create such type of combinations as shown in the below examples.Example 1x1

Read More

How to match the names of a vector in sequence with string vector values in another vector having same values in R?

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

If we want to match the names of a vector in sequence with string vector values in another vector having same values then pmatch function can be used. The pmatch function means pattern match hence it matches all the corresponding values and returns the index of the values. Check out the below examples to understand how it works.Examplex1

Read More

How to truncate a numerical vector to a specified number of decimal places in R?

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

The truncation means removing the number of decimal places but not rounding. For example, if we have a value 5.1742145 then truncating to one decimal place will be 5.1 and rounding will be 5.2. In R, we can do this by using trunc function as shown in the below examples.Examplex1

Read More

How to replace upper triangular matrix with lower triangular matrix and vice versa in R?

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

The upper triangular matrix can be replaced with lower triangular matrix by transposing the whole matrix and extracting upper triangular matrix from it then storing it in the original matrix. For example, if we have a matrix M then upper triangular matrix of M can be replaced with lower triangular matrix by using the below code −M1[upper.tri(M1)]

Read More

How to find the sum of division in R if zero exists in the vectors?

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

To find the sum of division if zero exists in the vectors, we need to assign NA to zeros in both the vectors and then use the sum function with na.rm set to TRUE. For example, if we have two vectors x and y that contains some zeros then we can divide x by y using the below commands −x[x==0] y yOutput[1] 1 5 3 1 9 1 3 8 9 0 1 7 3 2 3 3 2 9 3 1 9 5 5 2 5 4 4 7 4 5 9 1 9 9 4 2 3 [38] ...

Read More

How to check if all values in a vector are integer or not in R?

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

To check whether all values in a vector in R are integer or not, we can round the vector using floor function then subtract the vector values from it and check whether the output is zero or not. If the output will be zero that means the value is integer otherwise it is not. The floor function returns the largest integer that is smaller or equal to the actual value. For example, if we have a vector x then it can be done as x-floor(x)==0.Example1> x1 x1Output[1] 4 0 2 8 6 1 3 7 3 4 0 7 2 ...

Read More
Showing 451–460 of 1,740 articles
« Prev 1 44 45 46 47 48 174 Next »
Advertisements