R Programming Articles

Page 46 of 174

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 490 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 512 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 243 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 988 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 286 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

How to set a specific value for a range in an R vector?

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

Suppose we have a vector that contains hundred values starting from 1 to 100 and we want to set values greater than 5 and less than 96 to 5 then it can be done with the help of ifelse function. For example, if such vector is named as x then the command will be as follows −ifelse(x>5 & x x1 x1Output[1] 2 4 1 6 7 4 0 1 6 4 0 7 1 3 3 1 4 6 7 7 0 2 7 3 9 4 4 8 6 3 3 5 4 5 6 5 6 [38] 2 ...

Read More

How to convert a string vector into title case in R?

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

We cannot be sure about the data characteristics we get for analysis and mostly it is not well organised, thus, the first task would be to make it more organised. The string values not in title case should also be taken care of if it is especially supposed to be in title case. For this purpose, we can str_to_title function of stringr package.Example1> x1 x1Output[1] "india" "united kingdom" "indonesia" "canada" [5] "canada" "india" "united kingdom" "canada" [9] "indonesia" "united kingdom" "indonesia" "canada" [13] "russia" "indonesia" "canada" "russia" [17] "united kingdom" "russia" "russia" "india" [21] "united kingdom" "india" "india" "united kingdom" ...

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