Convert Matrix into Color Matrix in R

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:14:37

1K+ Views

To convert a matrix into a color matrix, we can make use of image function. There are multiple ways for assigning the colors but the easiest one might be by defining the minimum and maximum value in the matrix. Also, we can do this by using the shades of a single color as shown in the example 3.Example1Live Demo> M1 M1Output   [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 6    3    5    4    3 [2, ] 9    4    5    2    5 [3, ] 3    2   ... Read More

Create a Plot in Base R with Tick Marks Excluding Axes Lines

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:09:28

161 Views

To create a plot with tick marks but without axes lines, we first need to create the plot without axes and then add the tick marks. This can be done with the help of plot function and axis function in base R. The axis function will help us to decide where do we need the tick marks and the ticks.Example1> plot(1:10,axes=FALSE) > axis(1,c(1:10),col=NA,col.ticks=1)OutputExample2Live Demo> x xOutput[1] 5 2 1 2 1Example> plot(x,axes=FALSE) > axis(1,c(1:5),col=NA,col.ticks=1)Output

Combine Lists of Data Frames in R

Nizamuddin Siddiqui
Updated on 02-Jan-2021 10:03:42

473 Views

If we have a list of data frames and the size of those data frames is same then we might want to combine the lists so that the data frames can be combined. This can be done by using mapply function along with cbind. For example, if we have two lists of data frames defined as List1 and List2 then we can combine them using the command −mapply(cbind, List1, List2, SIMPLIFY=FALSE).ExampleConsider the below data frame −Live Demo> x1 x2 df1 df1Output      x1        x2 1   0.2378371  0.51433808 2   0.0638975 -1.66077353 3   0.3987209  0.68480587 ... Read More

Increase Space Between Horizontal Legends Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 02-Jan-2021 09:58:35

4K+ Views

Generally, the space between two legend entries is not large enough and it becomes difficult to read the legend names if the names are long. In this case, we need to increase the margin between the legend entries/names but this would be required when the legends are horizontally aligned as vertical legends can be read as it is. For this purpose, we can use legend.text argument inside theme function of ggplot2 package.ExampleConsider the below data frame −Live Demo> x y df dfOutput x y 1 Male 501 2 Female 520Loading ggplot2 ... Read More

Add Row in R Data Frame at a Specific Place

Nizamuddin Siddiqui
Updated on 02-Jan-2021 09:53:56

578 Views

The data collected for the first time is utilised as it is but when we need to go for secondary data to conduct the same or similar study again, we can use new data as well as the primary data. In this type of situations, we might want to randomly organize data rows that includes new and old data. Also, there is a possibility of missing data row which is found at later stage in the study then it can be also added. Hence, a row might be required to added in the existing data frame. This can be done ... Read More

Various Types of Keys in DBMS

David Meador
Updated on 30-Dec-2020 16:08:21

24K+ Views

The different types of keys in DBMS are −Candidate Key - The candidate keys in a table are defined as the set of keys that is minimal and can uniquely identify any data row in the table.Primary Key - The primary key is selected from one of the candidate keys and becomes the identifying key of a table. It can uniquely identify any data row of the table.Super Key - Super Key is the superset of primary key. The super key contains a set of attributes, including the primary key, which can uniquely identify any data row in the table.Composite Key -  If ... Read More

Check If Each Internal Node of a BST Has Exactly One Child in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:52:26

306 Views

Suppose we have the preorder traversal of a binary search tree (BST). We have to check whether each internal node has only one child or not.So, if the input is like preorder = [22, 12, 13, 15, 14], then the output will be True as BST is like −To solve this, we can follow one efficient approach. As all decedents of a node is either smaller or larger, then we can we can follow these steps −Get the next preorder successor of the nodeGet the last preorder successor of the nodeNow when both the successors are less than or greater ... Read More

Check If Difference of Areas of Two Squares Is Prime in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:50:47

278 Views

Suppose we have two numbers x and y. We have to check whether difference of their areas is prime or not.So, if the input is like x = 7, y = 6, then the output will be True as the difference of their square is 49 - 36 = 13 which is prime.To solve this, we will follow these steps −if (x + y) is prime number and (x - y) is 1, thenreturn Trueotherwise,return FalseLet us see the following implementation to get better understanding −Example Live Demodef is_prime(num) :    if num

Check Decimal Representation of Octal Number for Divisibility by 7 in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:48:54

204 Views

Suppose we have one octal number. We have to check whether the decimal representation of the given octal number is divisible by 7 or not.So, if the input is like n = 61, then the output will be True as the decimal representation of 61 is 6*8 + 1 = 48 + 1 = 49 which is divisible by 7.So, if the input is like n = 61, then the output will be True as the decimal representation of 61 is 6*8 + 1 = 48 + 1 = 49 which is divisible by 7.To solve this, we will follow ... Read More

Check If Count of Divisors Is Even or Odd in Python

Arnab Chakraborty
Updated on 30-Dec-2020 13:47:18

849 Views

Suppose we have a number n, we have to find its total number of divisors are even or odd.So, if the input is like n = 75, then the output will be Even, as the divisors are [1, 3, 5, 15, 25, 75].To solve this we shall follow one simple and efficient approach. We have observed that when a number is perfect square then only it has odd number of divisors. So if the number is not perfect square then it will have even divisors. So here we will only check whether the number is perfect square or not and ... Read More

Advertisements