Create Integer Column in R Data Frame with Leading Zeros

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:28:24

609 Views

To create an integer column in an R data frame with leading zeros, we can use sprintf function.For Example, if we want to create a data frame column having values starting from 1 to 10 and we want to have 1 as 01 and so on then we can use the command given below −data.frame(x=sprintf('%0.2d',1:10))Check out the Examples given below to understand how it works.Example 1To create an integer column in an R data frame with leading zeros, use the command given below −df1

Change Y-Axis Title in Base R Plot

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:26:02

10K+ Views

The easiest way to change the Y-axis title in base R plot is by using the ylab argument where we can simply type in the title. But the use of ylab does not help us to make changes in the axis title hence it is better to use mtext function, using which we can change the font size, position etc. of the title.Check out the below example to understand how it can be done.ExampleUse the code given below to change the Y-axis title in base R plot −plot(1:10)OutputIf you execute the above given code, it generates the following Output −Add ... Read More

Count Non-Missing Values by Group in R Data Frame

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:24:08

1K+ Views

To find the number of non-missing values in each column by group in an R data frame, we can use summarise_each function of dplyr package with negation of is.na function.For Example, if we have a data frame called df that contains a grouping column say G and some other columns having few NAs then we can find the number of non-missing values in each column by grouping column G with the help of below command −df%%group_by(G)%%summarise_each(funs(sum(!is.na(.))))Example 1Following snippet creates a sample data frame −Group

Display X-bar in Base R Plot

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:23:56

3K+ Views

To display x-bar in base R plot, we can use text function and define the bar with bar function inside expression function.For example, if we have a vector called x that contains first ten numbers (1 to 10) then we can display it’s mean inside the base R plot by using the command given below −text(0.97, 5.5, expression(bar("x")))Check out the examples given below to understand how it works.Example 1Use the code given below to display x-bar in base R plot −plot(mean(1:100)) text(0.97, 50.5, expression(bar("x")))OutputIf you execute the above given snippet, it generates the following Output −Example 2Use the code given ... Read More

Color Scatterplot Points Based on a Threshold Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:22:06

2K+ Views

To color scatterplot points based on a threshold using ggplot2, we first need to define a column with the threshold value and then we can use that column inside aes for coloring. The column with threshold can be created by using cut function.Check out the example given below to understand how it can be done.ExampleFollowing snippet creates a sample data frame −x

Create Multiple Regression Lines Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:18:41

979 Views

To create multiple regression lines using ggplot2, we can use grouping inside aes.For example, if we have a data frame called that contains two numerical columns say x and y and a categorical column say C then the regression lines between x and y for all the categories in C can be created by using the below given command −ggplot(df, aes(x, y, group=C))+geom_point()+stat_smooth(method="lm")ExampleFollowing snippet creates a sample data frame −data(mtcars) head(mtcars, 20)OutputThe following dataframe is created −                   mpg  cyl disp   hp drat  wt    qsec   vs am gear carb Mazda ... Read More

Recursively Print All Sentences from List of Word Lists in C++

Sunidhi Bansal
Updated on 03-Nov-2021 08:14:36

563 Views

Given a list of words. The goal is to create all possible sentences that can be formed by taking words from the list using a recursive approach. You can only take one word at a time from both the lists.Let us see various input output scenarios for thisInput −sentence[row][col] = {{"I", "You"},    {"Do", "do not like"},    {"walking", "eating"}}Output   −I Do walking I Do eating I like walking I like eating You Do walking You Do eating You like walking You like eatingExplanation − Taking one word from each list in sentence[0-2] gives above sentences.Input −sentence[row][col] = {{"work", "live"}, {"easy", "happily"}}Output −work ... Read More

Remove Duplicate Columns from a Matrix in R

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:13:28

793 Views

To remove duplicate columns from a matrix in R, we can use unique function.For Example, if we have a matrix called M that contains some duplicate columns then we can use the below command to remove those duplicate columns −unique(M,MARGIN=2)Example 1Following snippet creates a sample matrix −M1

Find Minimum for Each Row Based on Selected Columns in R Data Frame

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:12:18

4K+ Views

To find the minimum for each row based on few columns in an R data frame, we can use pmin function inside with function.For example, if we have a data frame called df that contains five columns say x, y, z, a, and b then minimum for each row based on columns x, y, and b can be found by using the command given below −with(df,pmin(x,y,b))Example 1Following snippet creates a sample data frame −x1

Display Text in Base R Plot with Outline

Nizamuddin Siddiqui
Updated on 03-Nov-2021 08:05:39

435 Views

The display of text in base R plot with outline is not possible, for this purpose we would need to use shadowtext function of TeachingDemos package. The shadowtext function will be applied after creating the plot in base R.We will have to provide the location of the text inside the plot and some other arguments such as text that needs to be displayed, color of outline, and size for better display.Example 1Use the following code to display text in base R plot with outline −plot(1) shadowtext(1.2, 1.2, "Point at 1", col="white", cex=2)OutputIf you execute the above given code, it generates ... Read More

Advertisements