R Programming Articles

Page 58 of 174

How to create a dendrogram without X-axis labels in R?

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

A dendrogram display the hierarchical relationship between objects and it is created by using hierarchical clustering. In base R, we can use hclust function to create the clusters and the plot function can be used to create the dendrogram. For example, if we want to create the dendrogram for mtcars data without X−axis labels then it can be done as shown below −hc=hclust(dist(mtcars)) plot(hc, xlab="", sub="")Examplehead(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 ...

Read More

How to create a dashed line that passes through Y = 1 in base R?

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

Usually, plots are created with solid lines but sometimes we need to use dashed line so that the points can represent a threshold or something critical, the main objective here is to get the attention of the reader on these points. In base R, the plots are created with plot function and we can use abline function with lty =2 to draw dashed lines.Example1x

Read More

How to multiply a matrix columns and rows with the same matrix rows and columns in R?

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

To multiply a rows or columns of a matrix, we need to use %*% symbol that perform the multiplication for matrices in R. If we have a matrix M with 5 rows and 5 columns then row 1 of M can be multiplied with column 1 of M using M[1,]%*%M[,1], similarly, we can multiply other rows and columns.ExampleM

Read More

How to find the row-wise index of non-NA values in a matrix in R?

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

A matrix can also contain missing values and those missing values can be placed in a matrix by randomization as well, hence we cannot be sure about the positions of those values that are referred to as NA and the non-missing values. If we want to find the positions of the non-missing values in a matrix then apply function can be used where we can use which function to exclude NA values. Check out the below examples to understand how it works.Example1M1

Read More

How to extract a data frame's column value based on a column value of another data frame in R?

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

In data analysis, we encounter many problems with a lot of variations among them. One such problem is we have some information in a place that needs to be checked through a different place and these places can be data frames. Therefore, we can to find a data frame’s column value based on a column value of another data frame. In R, we can easily do it with the help of which function.Examplex2

Read More

How to convert a list into an array in R?

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

To convert a list into an array then firstly we need to unlist the list elements and then use the array function. For example, if we have a list defined as List then it can be converted into an array using the command array(unlist(List)). Suppose the list contain the elements as shown below −1 2 3 4 5 1 2 3 4 5 1 2 3 4 5If we convert this list into an array then the output will be 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5.Example1List1

Read More

How to check in R whether a matrix element is present in another matrix or not?

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

We can use %in% to check whether a matrix element is present in another matrix or not. For example, suppose we have two matrices defined as −   M1 1 2 3 1 2 3 1 2 3    M2 1 2 3 4 5 6 7 8 9Then M1%in%M2 will return −[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUEBut M2%in%M1 will return −[1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSEExample1M1

Read More

How to combine two factor vectors to create one in R?

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

To combine two factor vectors, we can extract the unique levels of both the vectors then combine those levels. This can be done by using unique function. Also, we can set the levels of the original vectors to the combination of the levels, in this way, we can complete both the vectors with missing levels. Check out the examples below to understand how it works.Example1x1

Read More

How to repeat a random sample in R?

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

The random sample can be repeated by using replicate function in R. For example, if we have a vector that contains 1, 2, 3, 4, 5 and we want to repeat this random sample five times then replicate(5, x) can be used and the output will be matrix of the below form:[, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 1 1 1 1 [2, ] 2 2 2 2 2 [3, ] 3 3 3 3 3 [4, ] 4 4 4 4 4 [5, ] 5 5 5 5 5Example 1> x1 x1Output[1] 1 ...

Read More

How to set the legends using ggplot2 on top-right side in R?

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

The default position of legend in a plot created by using ggplot2 is right hand side but we can change the position by using theme function that has legend.position argument and legend.justification argument. To set the legend on top-right side we can use legend.position="top" and legend.justification="right".ExampleConsider the below data frame:Consider the below data frame:> x freq df dfOutputx freq 1 Mango 212 2 Guava 220 3 Pomegranate 218Loading ggplot2 package and creating bar chart with legend:> library(ggplot2) > ggplot(df, aes(x, freq, fill=x))+geom_bar(stat="identity")Output:Creating the bar chart with legend on top-right hand side of the chart:Example> ggplot(df, aes(x, freq, fill=x))+geom_bar(stat="identity")+theme(legend.position="top", legend.justification="right")Output:

Read More
Showing 571–580 of 1,740 articles
« Prev 1 56 57 58 59 60 174 Next »
Advertisements