R Programming Articles

Page 47 of 174

How to set the Y-axis tick marks using ggplot2 in R?

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

The default value of Y-axis tick marks using ggplot2 are taken by R using the provided data but we can set it by using scale_y_continuous function of ggplot2 package. For example, if we want to have values starting from 1 to 10 with a gap of 1 then we can use scale_y_continuous(breaks=seq(1,10,by=1)).ExampleConsider the below data frame: x

Read More

How to add a rank column in base R of a data frame?

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

Ranking of a variable has many objectives such as defining order based on hierarchy but in data science, we use it mainly for analyzing non-parametric data. The ranking of a variable in an R data frame can be done by using rank function. For example, if we have a data frame df that contains column x then rank of values in x can be found as rank(df$x).ExampleConsider the below data frame: x1

Read More

How to detect a binary column defined with 0 and 1 in an R data frame?

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

If a column in an R data frame has only two values 0 and 1 then we call it a binary column but it is not necessary that a binary column needs to be defined with 0 and 1 only but it is a general convention. To detect a binary column defined with 0 and 1 in an R data frame, we can use the apply function as shown in the below examples.ExampleConsider the below data frame −x1

Read More

How to create a rectangle inside boxplot in base R?

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

To create a rectangle inside boxplot in base R, we can use rect function after creating the boxplot. The rect function has many arguments but for the creation of a rectangle only first four are necessary and these are defined as xleft - a vector (or scalar) of left x positions, ybottom - a vector (or scalar) of bottom y positions, xright - a vector (or scalar) of right x positions and ytop - a vector (or scalar) of top y positions.Examplex

Read More

How to find the absolute maximum of a matrix with sign if it contains negative values in R?

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

If we have positive as well as negative values in a matrix then the maximum of the matrix will be a positive number but if we want to ignore the sign then a number represented with negative sign can also be the maximum. If we want to get the maximum with its sign then which.max function can be used in R. Check out the below examples to understand how to do it.ExampleM1

Read More

How to create a column with the serial number of values in character column of an R data frame?

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

A group column in an R data frame have duplicate values and we might want to create a column with the serial number based on the values such as first value of the first group gets 1, the same value gets 2 when occurred second time in the same column and so on. This can be done by using ave function as shown in the below examples.ExampleConsider the below data frame −S.No

Read More

How to create a table of sums of a discrete variable for two categorical variables in an R data frame?

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

If we want to create a table of sums of a discrete variable for two categorical variables then xtabs function can be used. The output will be a contingency table or cross tabulation table which looks like a matrix. For example, if we have a data frame df with two categorical column x and y and a count column freq then the table of sums for freq can be created by using xtabs(freq~x+y,data=df1).ExampleConsider the below data frame −x1

Read More

How to find the mean of three-dimensional array in R?

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

A three-dimensional array can have matrices of different size and they are not necessarily to be square or rectangular. Also, all the elements in an array are of same data type. If we want to find the mean of a three-dimensional array then apply function can be used where we need to refer the columns and rows of the array elements using combination function.ExampleA1 apply(A1,c(1,2),mean) [,1] [,2] [1,] 5 7 [2,] 6 8ExampleA2

Read More

What is the use of pheatmap function in R?

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

The pheatmap function is used to create clustered heatmaps but we can change the aesthetics of the plot by using color argument which is one of the main functionalities of pheatmap function. There are many other arguments that differentiate pheatmap from heatmap function.Examplelibrary(pheatmap) M1

Read More

How to create a plot in base R without margins?

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

To create a plot without margins, we first need to define that margin in a way that the plot created after that will not have margins and this can be done by using par function. We would need to pass mar function within par function as par(mar=c(0,0,0,0)).Examplepar(mar=c(0,0,0,0)) plot(1:10)OutputExamplebarplot(1:10)Output

Read More
Showing 461–470 of 1,740 articles
« Prev 1 45 46 47 48 49 174 Next »
Advertisements