Server Side Programming Articles

Page 1106 of 2109

How to assign a column value in a data frame based on another column in another R data frame?

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

To assign a column value based on another column, we can use ifelse function. The ifelse function checks whether the value in one column of one data frame matches the value in another column of another data frame by using equal sign (==) and then replace the original value with the new column if there is no match else returns the original value. Check out the below example to understand how it can be done.ExampleConsider the below data frame −> x1 x2 df1 df1Output x1 x2 1 3 5 2 3 7 3 0 ...

Read More

How to create boxplot with multiple factor levels using ggplot2 in R?

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

To create a boxplot, we have one factor and one numerical column and the boxplot is created for each category or levels in that factor. Now if we have two factors then the boxplot can be created for both factor levels by passing fill argument in geom_boxplot. This will help us to differentiate between the boxplots for the two factors. Check out the below examples to understand how it works.ExampleConsider the below data frame −> x y grp df dfOutput       x              y  grp 1 Female    0.790349405 b 2 ...

Read More

How to display average line for y variable using ggplot2 in R?

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

To display the average line for y variable using ggplot2, we can use geom_hline function along with the yintercept. In the yintercept, we would need to calculate the mean of the y variable and we can also change the colour of the line using color argument inside the geom_hline function.ExampleConsider the below data frame −> x y df dfOutputx y 1 -1.07323904 0.368641641 2 0.92531148 -0.196530651 3 -0.57433739 0.710957804 4 1.17367100 0.300110517 5 0.00769624 -1.287517035 6 0.64901161 -0.476105351 7 0.70197701 -0.683592585 8 -0.80807441 -1.716264317 9 0.10827026 0.116964308 10 -1.10451308 0.660382307 11 -0.01612692 -1.182533283 12 2.20292198 -1.890223763 13 -1.03368161 -0.526983486 14 ...

Read More

How to subset a data frame based on a vector values in R?

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

If we have a vector and a data frame, and the data frame has a column that contains the values similar as in the vector then we can create a subset of the data frame based on that vector. This can be done with the help of single square brackets and %in% operator. The %in% operator will help us to find the values in the data frame column that matches with the vector values. Check out the below examples to understand how it works.Example1Consider the below data frame df1 and vector v1 −> x1 x2 df1 df1Outputx1 x2 1 2 ...

Read More

How to change the legend title in ggplot2 in R?

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

In ggplot2, by default the legend title is the title of the grouping column of the data frame. If we want to change that title then scale_color_discrete function. For example, if we have a data frame called df that contains two numerical columns x and y and one grouping column say group then the scatterplot with a different legend title can be created by using the below command −ggplot(df, aes(x, y, color=group))+geom_point()+scale_color_discrete("Gender")ExampleConsider the below data frame −> x y grp df dfOutput             x          y    grp 1  -2.27846496  0.8121008   Male ...

Read More

How to create bar plot with log values using ggplot2 in R?

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

To create the bar plot using ggplot2, we simply need to use geom_bar function and if we want to have the log scale of y variable then it can be set with aes under geom_bar. For example, if we have a data frame called df that contains a categorical column x and a numerical column y then the bar plot with log of y can be created by using the below command −ggplot(df, aes(x, y))+geom_bar(stat="identity", aes(y=log(y)))ExampleConsider the below data frame −> x y df dfOutput   x     y 1 S1 53347 2 S2 84208 3 S3 12140 4 S4 ...

Read More

How to create boxplot of vectors having different lengths in R?

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

If we have multiple vectors of different lengths then the boxplot for such vectors can be created by creating a single data frame using those vectors with a categorical column showing the name of the vectors and a numerical column having the corresponding values. Then boxplot function will be used as shown in the below example.ExampleConsider the below vector x and y and create the data frame using them −> x y df dfOutput   X Grp 1  4   x 2  2   x 3  1   x 4  2   x 5  0   x 6  2   x ...

Read More

How to find the number of non-empty values in an R data frame column?

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

To find the number of non-empty values, we can find the negation of the sum of empty values which is actually the total number of non-empty values. For example, if we have a data frame df that contains a column x which has some empty values then to find the total number of non-empty values we can find the opposite/negation of total empty values. This can be done with the help of sum function and negation operator as shown in the below examples.Example1Consider the below data frame −> x df1 df1Output   x 1  1 2  2 3   4   ...

Read More

How to plot rows of a data frame as lines in R?

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

To plot row of a data frame as lines, we can use matplot function but we would need to transpose the data frame because transposed values of the data frame will be read as columns and the matplot function plot the columns not rows. For example, if we have a data frame called df then the plot of rows as lines can be created by using the command −matplot(t(df), type="l")Example1Consider the below data frame −> x1 x2 x3 df1 df1Output  x1 x2 x3 1  0  9  5 2  3  4  2 3  0  2  1 4  3  7  3 5 ...

Read More

How to display zero frequency for bars in base R barplot?

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

When we create a barplot in base R, the bars are plotted for all the values in the vector but if we have a gap in the values then the bar with zero frequency for that gap is not plotted. For example, if we have a vector called x that contains 100 values consisting of 0, 1, 3 then the barplot will not represent zero frequency for 2. To solve this problem, we can use factor function in the barplot function as shown in the below examples.Example1> x xOutput  [1] 0 1 1 1 3 1 3 1 0 1 1 5 1 8 3 2 3 3 3 3 2 4 2 0 5 3 1 0 1 1 4 1 4 2 3 1 3  [38] 1 4 2 2 5 1 2 5 2 3 1 1 0 1 1 2 3 4 3 1 1 2 1 2 5 1 1 3 3 1 3 1 3 0 4 1 1  [75] 2 2 5 2 2 1 2 2 3 1 4 2 1 3 2 0 2 1 0 4 1 5 2 3 2 0 1 1 1 1 2 3 1 3 1 3 2 [112] 3 2 4 4 1 1 0 2 6 1 4 0 5 0 1 2 2 2 3 5 2 2 2 1 4 2 2 1 0 0 1 4 4 1 3 5 1 [149] 1 5 1 2 0 1 2 2 1 1 2 1 0 5 2 3 2 3 1 4 1 2 2 1 1 1 0 0 2 0 1 2 3 4 4 4 2 [186] 2 2 1 1 1 1 4 3 2 2 2 1 0 5 2Example> barplot(table(factor(x,levels=0:10)))OutputExample2> y barplot(table(factor(y,levels=1:5)))Output

Read More
Showing 11051–11060 of 21,090 articles
Advertisements