Found 33676 Articles for Programming

How to return the same index for consecutively duplicated values in an R vector?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:33:07

140 Views

It is obvious that duplicate values in an R vector do not have same indexes but we might want to create the same index for consecutively duplicated values, this will help to recognize the groups of duplicated values. For this purpose, we can use cumsum function along with diff function as shown in the below examples.Example1 Live Demox1

How to check for palindrome in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:32:32

4K+ Views

A palindrome is a word or any value that is being read in the same way from right to left as in left to right. For example, 12321, 514212415, ABCDEDCBA, etc. To check palindrome in R, we can create a function using stri_reverse function of stringi package as shown in the below examples.Example1library(stringi) palindrome

How to change the Y-axis title to horizontal using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:32:50

4K+ Views

The default direction of Y-axis title using ggplot2 in R is vertical and we can change to horizontal. For this purpose, we can use theme function of ggplot2 package. We would need to use the argument of theme function as axis.title.y=element_text(angle=0)) and this will write the Y-axis title to horizontal but the position will be changed to top.Example Live DemoConsider the below data frame −x

How to check if a list element is greater than a certain value in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:28:18

964 Views

If we have a list that contains numeric elements and we want to check whether the elements are greater than a certain value then as.numeric function can be used. The output of the function will be in 0/1 format where 0 represents FALSE and 1 represents TRUE. For example, if we have a list called LIST then to check whether elements in LIST are greater than 2 can be done as as.numeric(LIST>2).Example1 Live DemoList15)Output[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0Example2 Live DemoList22)Output[1] 1 1 1 1 0 0 1 0 1 1 1 1 1 0 0 0 1 1 1 0

How to create bar chart based on two groups in an R data frame?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:28:33

658 Views

To create a bar chart based on two groups, we can use geom_bar function of ggplot2 package with position argument that defines the position of the groups. For example, if we have a data frame called df that contains two categorical variable x1 and x2 and the one response variable y then the bar chart can be created by using the below command −ggplot(df,aes(x1,y,fill=x2))+geom_bar(position=position_dodge(),stat="identity")Example Live DemoConsider the below data frame &minusGender

How to perform rounding in R to next 10 instead of nearest 10?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:22:00

244 Views

In general, most commonly used rounding is rounding to nearest 10 or nearest 100 but sometimes we actually want to remove the values after a value instead of rounding. For example, removing values after 2 decimal places, this is the type of situation where we need to round to next 10 instead of nearest 10. This can be done with the help of floor function as shown in the below examples.Example1 Live Demox1

How to extract the factor levels from factor column in an R data frame?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:21:43

13K+ Views

To extract the factor levels from factor column, we can simply use levels function. For example, if we have a data frame called df that contains a factor column defined with x then the levels of factor levels in x can be extracted by using the command levels(df$x). This extraction is helpful if we have a large number of levels.Example1 Live DemoConsider the below data frame −x1

How to use column index instead of column name while using group_by of dplyr in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:21:06

975 Views

When we use group_by function of dplyr package, we need to pass the column name(s) that are categorical in nature. If we want to use the index of the same column(s) then group_by_at function needs to be used, where we can pass the column index as the argument.Example1 Live DemoConsider the below data frame −x1 1 A 2 2 B 6 3 C 5 4 D 7Example2 Live Demoy1

How to find the sum of rows, columns, and total in a matrix in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:20:45

10K+ Views

To find the sum of row, columns, and total in a matrix can be simply done by using the functions rowSums, colSums, and sum respectively. The row sums, column sums, and total are mostly used comparative analysis tools such as analysis of variance, chi−square testing etc.Example1 Live DemoM1

How to find the number of unique values in a vector by excluding missing values in R?

Nizamuddin Siddiqui
Updated on 09-Feb-2021 12:13:10

1K+ Views

If there exist missing values in an R vector then it is counted as a unique value in the vector, therefore the extraction of unique values cannot be done directly. For this purpose, we need to use unique with na.omit function. For example. If we have a vector called x with missing values then the extraction of unique values can be done as length(unique(na.omit(x))).Example1 Live Demox1

Advertisements