Server Side Programming Articles - Page 1447 of 2650

How to create a data frame in R with list elements?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:06:42

629 Views

If a list has the same length of elements (not sub-elements) as the length of each vector for which we want to create the data frame then we first need to create the data frame of vectors then we can easily add the list into the data frame. But if we have a list and other vectors then data frame cannot be created as data.frame function will read each value of the list separately.ExampleLive Demo> df1 df1Output   x y 1  6 1 2  8 1 3  6 2 4  8 1 5  5 1 6  3 1 7  6 1 ... Read More

How to create a horizontal line in a histogram in base R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:02:39

1K+ Views

A horizontal line in a histogram is not much useful but we might want to create it in some situations such as where we want to display a specific value on the Y-axis that helps us to differentiate in the density of frequencies above or below a certain value. To create a horizontal line in a histogram, we simply need to use abline function as shown in the below exampleExample> x hist(x) > abline(h=100)OutputExample> abline(h=100,col="blue")OutputExample> abline(h=100,col="blue",lwd=5)Output

How to display the superscript for a single variable in a base R plot?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:00:09

168 Views

To display the superscript in a base R plot, we would need to use expression function inside text function. For example, if we want to display X-square inside a blank plot in base R then we can use the below code:plot(1:10,type="n") text(2,2,expression(X^2))Example1> plot(1:10,type="n") > text(2,2,expression(X^2==4))Output:Example2> text(5,5,expression(X^5==Center))Output:Example3> text(9,9,expression(Squared^2))Output:

How to extract columns based on particular column values of an R data frame that matcha pattern?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 04:58:32

581 Views

The column values of an R data frame can be easily extracted by subsetting with single square brackets but if we want to extract the column values that match a pattern then we need to use grepl function inside single square brackets, this will help us to match the pattern of the values in the data frame columns.ExampleConsider the below data frame:Live Demo> set.seed(271) > x1 x2 df1 df1Output x1 x2 1 A242 B71 2 A123 B71 3 A242 B81 4 A242 B87 5 A123 B71 6 A321 B71 7 ... Read More

How to create a sphere in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 04:52:10

649 Views

To create a sphere, we can use spheres3d function of rgl package. The rgl package is specifically designed to create real-time interactive 3D plots. If we want to create a sphere then we need to pass the values for the three axes and the radius of the sphere. We can also change the color of the sphere by introducing color argument inside spheres3d function.Example1Loading rgl package and creating a sphere:> library(rgl) > spheres3d(x=1,y=1,z=1,radius=1)Output:Example2> spheres3d(0,0,0,radius=1,color="blue")Output:Example3> spheres3d(0,0,0,radius=1,color="red")Output:

Program to fill Min-max game tree in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:55:51

3K+ Views

Suppose we have a binary tree representing a game state of a two player game. Every internal node is filled with 0 and the leaves values represent the end score. Player 1 wants to maximize the end score while player 2 wants to minimize the end score. Player 1 will always make moves on nodes at even levels and player 2 will always make moves on odd levels. We have to fill in the binary tree with the resulting scores assuming both of players play optimally.So, if the input is likethen the output will beTo solve this, we will follow ... Read More

Program to find maximum sum of two non-overlapping sublists in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:52:48

324 Views

Suppose we have a list of numbers called nums and two values x and y, we have to find the maximum sum of two non-overlapping sublists in nums which have lengths x and y.So, if the input is like nums = [3, 2, 10, -2, 7, 6] x = 3 y = 1, then the output will be 22, as the sublist with length 3 we select [3, 2, 10] and for the other we select [7].To solve this, we will follow these steps −P := a list with single element 0for each x in A, doinsert (last element of ... Read More

Program to find maximum sum by removing K numbers from ends in python

Arnab Chakraborty
Updated on 20-Nov-2020 05:51:18

414 Views

Suppose we have a list of numbers called nums and another value k. We have to find the maximum sum of elements that we can delete, given that we must pop exactly k times, where each pop can be from the left or the right end.So, if the input is like nums = [2, 4, 5, 3, 1] k = 2, then the output will be 6, as we can delete 2 and the 4.To solve this, we will follow these steps −window := sum of all numbers from index 0 through k - 1ans := windowfor i in range ... Read More

Program to find maximum additive score by deleting numbers in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:50:07

193 Views

Suppose we have a list of numbers called nums. Let us consider an operation where we can select a number, then remove it and increase our score by the sum of the number and its two adjacent numbers. If we can perform this operation as many times as we want as long as we do not select the first or the last number in the list. We have to find the maximal score possible.So, if the input is like nums = [2, 3, 4, 5, 6], then the output will be 39, as we can select 5, then sum will ... Read More

Program to find a target value inside given matrix or not in Python

Arnab Chakraborty
Updated on 20-Nov-2020 05:48:13

453 Views

Suppose we have a 2D matrix, where each row and column is sorted in non-decreasing order, we have to check whether given target is present inside it or not.So, if the input is like243034316632And target = 31, then the output will be TrueTo solve this, we will follow these steps −col := column size of matrix - 1for i in range 0 to row size of matrix, dowhile matrix[i, col] > target and col >= 0, docol := col - 1if matrix[i, col] is same as target, thenreturn Truereturn FalseLet us see the following implementation to get better understanding −Example Live ... Read More

Advertisements