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

1K+ 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

593 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

817 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

459 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

Differentiate Between Categorical and Numerical Independent Variables in R

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

887 Views

For categorical variable, each level is considered as an independent variable and is recognized by factor function. On the other hand, the numerical independent variable is either continuous or discrete in nature.Check out the Example given below for linear regression model summary to understand the difference between categorical and numerical independent variables.ExampleFollowing snippet creates a sample data frame −x

Reverse and Add Function in Java

Sunidhi Bansal
Updated on 03-Nov-2021 08:02:33

716 Views

We are given with an integer and the agenda here is to reverse the digits of the number and add the reversed number to the original number and check if the resultant number is a palindrome or not and the process is repeated until it does. The breaking point of the process is 1000 iterations and a value greater than the maximum long value( Long.MAX_VALUE).For ExamplesInput − 1678Output − Palindrome of the given input 1678 293392Explanation − The input number is first reversed and then added to the original number, it is then checked for palindrome if it is not a palindrome ... Read More

Create Upper Triangular Matrix Using Vector Elements in R

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

3K+ Views

To create an upper triangular matrix using vector elements, we can first create the matrix with appropriate number of columns and rows then take the transpose of that matrix. After that we will assign the lower triangular matrix elements to 0.The selection of number of rows and columns plays an important role here so we need to be careful while choosing them.Check out the examples given below to understand how it can be done.Example 1Following snippet creates a vector −x1

Reverse Actual Bits of a Given Number in Java

Sunidhi Bansal
Updated on 03-Nov-2021 07:58:16

1K+ Views

Given an integer n that is not negative. The goal is to reverse the bits of n and report the number that results from doing so. While reversing the bits, the actual binary form of the integer is used; no leading 0s are taken into account.Let us see various input output scenarios for thisInput − 13Output − Reverse actual bits of the given number 11(13)10 = (1101)2. After reversing the bits, we get: (1011)2 = (11)10.Explanation − The binary bits are obtained from the input number which is then reversed and finally converted to decimal format which is returned as output.Input − 18Output − ... Read More

Advertisements