R Programming Articles

Page 23 of 174

How to extract vector using different index for columns in an R matrix?

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

Suppose we have a matrix and a vector containing indices of equal size as the matrix then we can extract the vector from matrix using the index vector. For this purpose, we can use cbind function as shown in the below examples.Example1> M1 M1Output      [,1] [,2]  [1,]    4    0  [2,]    1    1  [3,]    1    2  [4,]    2    0  [5,]    3    2  [6,]    2    2  [7,]    1    6  [8,]    1    2  [9,]    3    1 [10,]    1    2 [11,]    2    3 [12,]    2    0 [13,]    3    0 [14,]    0    1 [15,]    2    4 [16,]    1    1 [17,]    3    1 [18,]    0    2 [19,]    2    1 [20,]    2    0Example> Index_M1 Index_M1Output[1] 2 1 2 1 2 2 1 1 2 1 1 2 1 1 1 1 2 2 1 1Example> M1[cbind(seq_along(Index_M1),Index_M1)]Output[1] 0 1 2 2 2 2 1 1 1 1 2 0 3 0 2 1 1 2 2 2Example2> M2 M2Output      [,1] [,2] [,3] [,4]  [1,]   10    9    9   11  [2,]   13    6   16    8  [3,]   11   11    8   10  [4,]   15   11    9    9  [5,]   10    8    9    9  [6,]    7   14    9   15  [7,]    8    6    8    7  [8,]    4    8    9   12  [9,]    7   12   11   10 [10,]    8    8    9   13 [11,]    9   13   11    6 [12,]   12    5   11    8 [13,]    8    6   15    8 [14,]    6   17   12    7 [15,]    8   10    9    8 [16,]   13    7   11   13 [17,]    5   10    7    7 [18,]   10   11    8    8 [19,]    5    9    9   13 [20,]    5   10    7    6Example> Index_M2 Index_M2Output[1] 3 4 3 3 3 1 3 4 4 3 1 4 3 4 4 1 2 1 1 2Example> M2[cbind(seq_along(Index_M2),Index_M2)]Output[1] 9 8 8 9 9 7 8 12 10 9 9 8 15 7 8 13 10 10 5 10

Read More

How to change the order of boxplot by means using ggplot2 in R?

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

To change the order of boxplot by means using ggplot2, we can use reorder function inside aes of ggplot. For example, if we have a data frame called df that contains two columns say x (categorical) and y(count) then the boxplot ordered by means can be created by using the command ggplot(df, aes(x=reorder(x, y, mean), y))+geom_boxplot()ExampleConsider the below data frame −> x y df dfOutput   x  y 1  A 22 2  A 17 3  A 20 4  A 36 5  A 34 6  A 25 7  A 25 8  A 30 9  A 23 10 A 29 11 B  8 ...

Read More

How to convert a matrix column into list in R?

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

To convert a matrix column into list can be done by using the apply function. We will have to read the columns of the matrix as list by using as.list function. For example, if we have a matrix called M then the columns in M can be converted into list by using the command apply(M, 2, as.list).Example1> M1 M1Output           [, 1]        [, 2] [1, ] -1.3256074 -0.07328026 [2, ]  1.1997584 -1.06542989 [3, ] -0.2214659 -1.75903298 [4, ]  1.4446361 -0.12859397 [5, ] -0.1504967  0.97264445Converting M1 columns to a list −> apply(M1, 2, as.list)Output[[1]] [[1]][[1]] ...

Read More

How to apply a manually created function to two columns in an R data frame?

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

Suppose we created a function that can take two different values at a time then we can apply that function to two columns of an R data frame by using mapply. For example, if we have a manually created function say func that multiply two values then we can apply it to a data frame called df that has two columns x and y by using the below command −mapply(func, df$x, df$y) Manually created function named as func: func mapply(func, df1$x1, df1$x2)Output[1] 24 35 18 5 56 25 4 48 16 28 30 7 24 30 30 25 12 ...

Read More

How to find the number of unique values in each row of an R data frame?

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

To find the number of unique values in each row of an R data frame, we can use apply function with length and unique function. For example, if we have a data frame called df that contains multiple columns then the number of unique values in each row of df can be found by using the command apply(df, 1, function(x) length(unique(x))).Example1Consider the below data frame −> x1 x2 x3 x4 df1 df1Output   x1 x2 x3 x4 1   3  1  1  2 2   3  2  0  2 3   3  2  0  1 4   3  0  3  1 ...

Read More

How to extract the last row from list of a data frame in R?

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

Suppose we have two frames each having 5 columns that are stored in a list in R and we want to extract the last row from each data frame then we can use the lapply function. For example, if we have a list called LIST that contains the data frames described above then we can extract the last row from each data frame using the command lapply(LIST, tail, 1).ExampleConsider the below list of data frames −> x1 x2 df1 y1 y2 df2 z1 z2 df3 List ListOutput[[1]]    x1 x2 1   6  5 2   6  5 3   ...

Read More

How to convert a column with missing values to binary with 0 for missing values in R?

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

To convert a column with missing values to binary with 0 for missing values, we can use as.integer function with complete.cases for the data frame column. For example, if we have a data frame called df that contains a column x which has some missing values then the column x can be converted to binary with 0 for missing values by using the command −as.integer(complete.cases(df$x))Example1Consider the below data frame −> x1 y1 df1 df1Output   x1 y1 1  NA  2 2   2  5 3   2 10 4   2  2 5   2  4 6  NA  7 7  NA ...

Read More

How to make a plot title partially bold using ggplot2 in R?

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

To make a plot title partially bold using ggplot2, we can use bquote function inside labs function and then changing the default font to bold using bold function as shown in the below examples. While using these functions we need to make sure that the title that we want to bold should be inside circular brackets appropriately.ExampleConsider the below data frame −> x y df dfOutput             x           y 1  -0.62160328  0.38477515 2   0.68287365 -1.56169067 3   0.75259774  1.28849990 4   0.56688920 -0.17014225 5   1.22351113 -0.32446764 6  -1.54210099  0.29001967 ...

Read More

How to find the column that has the largest sum in R?

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

To find the column that has the largest sum, we can use sort function for sorting in decreasing order with colSums and accessing the first element of the output which will be the largest sum. For example, if we have a data frame called df that contains multiple columns then the column that has the largest sum can be found by using the command −str(sort(colSums(df[, 1:length(df)]), decreasing=TRUE)[1])Example1Consider the below data frame −> x1 x2 x3 x4 df1 df1Output   x1 x2 x3 x4 1   3  4  4  5 2   6 10  3  3 3   6  5  2  5 ...

Read More

How to create a plot using ggplot2 by including values greater than a certain value in R?

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

To create a plot using ggplot2 by excluding values greater than a certain value, we can use subsetting with single square brackets and which function. For example, if we have a data frame called df that contains two columns say x and y, then the point chart by including values of x that are greater than 0 can be created by using the command −ggplot(df[which(df$x>0), ], aes(x, y))+geom_point()ExampleConsider the below data frame −> x y df dfOutput             x           y 1  -0.62160328  0.38477515 2   0.68287365 -1.56169067 3   0.75259774  1.28849990 ...

Read More
Showing 221–230 of 1,740 articles
« Prev 1 21 22 23 24 25 174 Next »
Advertisements