R Programming Articles

Page 43 of 174

How to find unique matrices in a list in R?

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

A list can contain many types of elements such as vectors, matrices, data frames etc. If we have matrices in a list then to find unique matrices in that list, we can simply use unique function. For example, if we have a list called LIST that contains matrices having some duplicate matrices then unique matrices can be extracted by using unique(LIST).Example1list(M1=matrix(1:25, ncol=5), M2=matrix(1:25, ncol=5), M3=matrix(1:25, ncol=5), M4=matrix(rpois(25, 5), ncol=5)) List1Output$M1 [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 6 11 16 21 [2, ] 2 7 12 17 22 [3, ] 3 8 13 18 ...

Read More

How to find mean and standard deviation from frequency table in R?

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

To find the mean and standard deviation from frequency table, we would need to apply the formula for mean and standard deviation for frequency data. For example, if we have a data frame called df that contains a column x for units and frequency for counts then the mean and standard deviation can be calculated as −Mean = sum(df$x*df$frequency)/sum(df$frequency) SD = sqrt(sum((df$x−Mean)**2*df$frequency)/(sum(df$frequency)−1)) respectively.Example1x

Read More

How to check for palindrome in R?

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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 197 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.Example1x1

Read More

How to export data frame in R to excel?

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

To export data frame in R to excel can be done with the help of write.xlsx function of xlsx package. We would need to pass the data frame name, the file name and the sheet name in which we want to save the data frame. For example, if we have a data frame called df with file name Excel_df and on sheet1 then the data frame will be saved by using the below command −write.xlsx(df, file="Excel_df.xlsx", sheetName = "Sheet1")Consider the below data frame −Examplex

Read More

How to plot matrix columns as lines in base R?

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

To plot matrix columns as lines in base R, we can use matplot function but we need to read the matrix as a data frame using as.data.frame function and for creating lines the type argument will be used. For example, if we have a matrix called M then the columns of M can be plotted as lines using the command matplot(as.data.frame(M),type="l").Consider the below data frame −ExampleM

Read More

How to save a csv and read using fread in R?

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

To save a csv file, we can use write.csv function in R and if we want to read it using fread then fread function will be used with the name of the csv file. The benefit of reading the csv file with fread function is that there will be a variable added to the original csv file which contains the id as integers starting from 1 to the length of column values.Consider the below data frame −Examplex

Read More

How to define the breaks for a histogram using ggplot2 in R?

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

To manually define the breaks for a histogram using ggplot2, we can use breaks argument in the geom_histogram function. While creating the number of breaks we must be careful about the starting point and the difference between values for breaks. This will define the number of bars for histogram so it should be taken seriously and should be according to the distribution of the data.Consider the below data frame −Examplex

Read More

How to hide NA values in an R matrix?

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

The hiding of NA values does not mean removal of NA values. If we have some NA values in an R matrix then we can hide them using blanks with double quotes. For example, suppose we have a matrix called M that contains some NA values then M can be printed by hiding NA value using print.table(M,na.print="")ExampleM1

Read More

How to fill NA values with previous values in an R data frame column?

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

To fill NA values with next and previous values, we can use na.locf function of zoo package with fromLast = TRUE. This is the situation of a column as shown below −x 0 NA NA 1 1 NA 0 1The output after filling NA values with next and previous values will be −x 0 0 0 1 1 1 0 1Consider the below data frame −Examplex1

Read More
Showing 421–430 of 1,740 articles
« Prev 1 41 42 43 44 45 174 Next »
Advertisements