Server Side Programming Articles

Page 1107 of 2109

How to find the length of columns for missing values in R?

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

The length of columns for missing values means the number of missing values in the data frame. This can be easily done with the help of colSums function where we will find the total number of NA values with is.na. For example, if we have a data frame called df that contains some missing values then the length of columns for missing values can be found by using the command colSums(is.na(df)).Example1Consider the below data frame −> x1 x2 x3 x4 df1 df1Output   x1 x2 x3 x4 1  NA NA  2  2 2  NA NA NA  2 3   1 NA ...

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 372 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 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 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

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 948 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 apply a manually created function to two columns in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 808 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 extract the last row from list of a data frame in R?

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

How to create stacked plot with density using ggplot2 in R?

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

To create stacked plot with density using ggplot2, we can use geom_density function of ggplot2 package and position="stack". For example, if we have a data frame called df that contains two columns say x and y, where x is categorical and y is numerical then the stacked plot with density can be created by using the command −ggplot(df, aes(y, y=..density..))+geom_density(aes(fill=x), position="stack")ExampleConsider the below data frame −> x y df dfOutput   x y 1  C 3 2  C 5 3  B 4 4  A 7 5  B 1 6  A 6 7  D 4 8  C 3 9  C 7 10 ...

Read More
Showing 11061–11070 of 21,090 articles
Advertisements