R Programming Articles

Page 22 of 174

How to add a vector to each row of a matrix in R?

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

To add a vector to reach row of a matrix, we can use addition sign (+) and create the repetition of the vector up to the number of rows in the matrix. For example, if we have a matrix called M then a vector say v can be added to each row of M by using the command −M+rep(v, each=nrow(M))Example1Consider the below matrix and the vector −> M1 M1Output      [, 1] [, 2]  [1, ]    3    2  [2, ]    3    3  [3, ]    4    2  [4, ]    5    1  [5, ...

Read More

How to add suffix to column names in R?

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

To add suffix to column names in R, we can use paste function. For example, if we have a data frame called df that contains three columns say x, y, and z and we want to add a suffix to these columns say underscore1 (_1) then it can be done by using the commandcolnames(df) x y z df1 df1Output   x y z 1  6 3 2 2  9 7 5 3  5 7 6 4  5 9 6 5  2 5 9 6  4 5 4 7  2 0 7 8  2 5 8 9  4 5 8 10 6 ...

Read More

How to match a column in a data frame with a column in another data frame in R?

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

To match a column in a data frame with a column in another data frame, we can use match function. For example, if we have two data frames called df1 and df2 each having one similar column and the second having an extra column then the matching can be done for similar columns and a new column in the first data frame can be created based on that match and the second column the second data frame. Check out the below examples to understand how it works.Example1> df1 df1Output   x1 1   2 2   2 3   1 4 ...

Read More

How to find the number of groupwise missing values in an R data frame?

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

In data science, we often face the problem of missing values and we need to define a way to replace them with an appropriate value or we can complete remove them. If we want to replace the missing then we also need to know how many missing values are there. Therefore, if we have a data frame with grouping column then finding the number of groupwise missing values can be done with aggregate function as shown in the below examples.Example1Consider the below data frame −> Group x df1 df1Output   Group  x 1      A  2 2      A ...

Read More

How to standardize only numerical columns in an R data frame if categorical columns also exist?

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

The standardization of a numerical column can be easily done with the help of scale function but if we want to standardize multiple columns of a data frame if categorical columns also exist then mutate_if function of dplyr package will be used. For example, if we have a data frame df then it can be done as df%>%mutate_if(is.numeric, scale)Example1Consider the below data frame −> x1 x2 df1 df1Output   x1 x2 1   c  4 2   c  1 3   a  4 4   a  1 5   b  0 6   c  4 7   c  2 8   ...

Read More

How to create a bar plot with bars for missing values in R?

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

To create a bar plot in R, we can use barplot function but if there exist some missing values in the data then we can use ggplot2 package. For example, if we have a data frame having two vectors say x and y, x containing categorical values with NA as one of the values and y having counts/frequency for each of the categories then the bar plot will be created by using the command ggplot(df, aes(x, y))+geom_bar(stat="identity").ExampleConsider the below data frame −> x y df dfOutput     x  y 1    A 24 2    B 21 3 45Creating ...

Read More

How to find the index of n number of maximums in each row of a matrix in R?

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

If a matrix has multiple columns and values in each row are different then there will be number of maximums equal to the number of columns. Suppose, we want to extract the index of two maximums in each row in a matrix called M then we can use the below command −t(apply(M,1,order,decreasing=TRUE)[1:2,]) Example1> M1 M1Output      [,1] [,2] [,3] [,4]  [1,]    7    4    4   10  [2,]    7    1    4    3  [3,]    9    6    6    4  [4,]    6    5    3    3  [5,]    3    3    2    4  [6,]    5    6    2    2  [7,]    4    7    3   10  [8,]    6    0   11    6  [9,]    1    6    2    2 [10,]    4    9    4    9 [11,]    4   10    3    6 [12,]    3    9    3   11 [13,]    5    4    7    2 [14,]    6    7    6    6 [15,]    2    5    6    3 [16,]    2    5    8    2 [17,]    0    3    1    6 [18,]    6   10    6    4 [19,]    3    4    5    5 [20,]    4    5    8    4Finding the index of two maximums in each of M1 −> t(apply(M1,1,order,decreasing=TRUE)[1:2,])Output      [,1] [,2]  [1,]    4    1  [2,]    1    3  [3,]    1    2  [4,]    1    2  [5,]    4    1  [6,]    2    1  [7,]    4    2  [8,]    3    1  [9,]    2    3 [10,]    2    4 [11,]    2    4 [12,]    4    2 [13,]    3    1 [14,]    2    1 [15,]    3    2 [16,]    3    2 [17,]    4    2 [18,]    2    1 [19,]    3    4 [20,]    3    2Example2> M2 M2Output      [,1] [,2] [,3] [,4]  [1,]   65   52   42   63  [2,]   52   49   43   54  [3,]   50   35   49   57  [4,]   52   42   36   52  [5,]   48   36   45   43  [6,]   49   65   62   51  [7,]   52   46   56   51  [8,]   43   51   41   53  [9,]   53   40   51   55 [10,]   52   48   48   41 [11,]   54   44   48   42 [12,]   43   34   58   54 [13,]   41   50   51   45 [14,]   47   40   56   39 [15,]   49   48   42   38 [16,]   50   56   47   56 [17,]   55   48   39   52 [18,]   49   39   48   37 [19,]   53   49   58   50 [20,]   38   57   48   59Finding the index of two maximums in each of M2 −> t(apply(M2,1,order,decreasing=TRUE)[1:2,])Output      [,1] [,2]  [1,]    1    4  [2,]    4    1  [3,]    4    1  [4,]    1    4  [5,]    1    3  [6,]    2    3  [7,]    3    1  [8,]    4    2  [9,]    4    1 [10,]    1    2 [11,]    1    3 [12,]    3    4 [13,]    3    2 [14,]    3    1 [15,]    1    2 [16,]    2    4 [17,]    1    4 [18,]    1    3 [19,]    3    1 [20,]    4    2

Read More

How to check if a matrix in R is in binary form?

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

A binary matrix contains values in form of twos such as 0/1, 1/2, Yes/No etc. If we have a matrix that has some values and we expect that there are only two values in the whole matrix then we can check whether only those two values exist in the matrix or not. For example, if we have a matrix called M then we can check whether it contains only 0/1 in the matrix by using the command all(M %in% 0:1).Example1> M1 M1Output      [, 1] [, 2] [, 3] [, 4]  [1, ]    0    0    0 ...

Read More

How to find the location of a string in an R data frame?

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

To find the location of a numerical value in an R data frame we use which function and if the value is string then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called df that contains a value say tutor then we can find the location of tutor by using the command which(df=="tutor", arr.ind=TRUE).Example1Consider the below data frame −> x1 x2 x3 df1 df1Output     x1   x2   x3 1  2018 2020 2018 2  2020 2020 2015 3  2018 2020 2015 4  2018 2015 2020 ...

Read More

How to aggregate matrix columns by row names in R?

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

To aggregate matrix columns by row names, we can use colSums with sapply and transpose the output. For example, if we have a matrix called M then the aggregate matrix columns by row names can be done using t(sapply(by(M, rownames(M), colSums), identity)).Example1Consider the below matrix −> M1 rownames(M1) M1Output  [, 1] [, 2] B    4    6 D    2    1 B    1    5 C    0    0 A    2    3 B    1    0 B    5    3 D    1    3 C    0    1 C   ...

Read More
Showing 211–220 of 1,740 articles
« Prev 1 20 21 22 23 24 174 Next »
Advertisements