Found 33676 Articles for Programming

How to plot values with log scales on x and y axis or on a single axis in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:30:33

4K+ Views

We can plot numerical values in R with many scales and that includes log scale as well. Also, it is possible to plot the values with log scales on both the axes. In base R, the best way to do this is defining the axes values with decimal representation as shown in the below examples with well-defined log.Consider the below vector −Example Live Demoset.seed(555) x

How to set a level of a factor column in an R data frame to NA?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:28:27

736 Views

In data analysis, we often face inappropriate data and hence the data analysis becomes difficult. An example of inappropriate data is reading missing values with a different value by naming them as Missing or Not Available. It can be done by using below syntax −Syntaxlevels(“data_frame_name”$”Column_name”)[levels(“data_frame_name”$”Column_name”=="Missing"]

Program to find the largest sum of the path between two nodes in a binary tree in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:29:23

239 Views

Suppose we have a binary tree; we have to find the maximum sum of any path between any two nodes.So, if the input is likethen the output will be 62 as the nodes are [12, 13, 14, 16, 7].To solve this, we will follow these steps −Define a function utils() . This will take rootif root null, thenreturn 0l := utils(left of root)r := utils(right of root)max_single := maximum of (max of l and r) + value of root) and value of rootmax_top := maximum of max_single and l + r + value of rootres := maximum of res and ... Read More

How to create a subset based on levels of a character column in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:22:03

1K+ Views

In R programming, mostly the columns with string values can be either represented by character data type or factor data type. For example, if we have a column Group with four unique values as A, B, C, and D then it can be of character or factor with four levels. If we want to take the subset of these columns then subset function can be used. Check out the example below.Consider the below data frame −Exampleset.seed(888) Grp

How to check whether a data frame exists or not in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:17:31

2K+ Views

Sometimes we keep writing codes in the programming console and suddenly we need to use something that was used in the upper side of programming console then recalling it becomes a little ambiguous if we forget about it. In this case, we might want to check whether something exists or not and that something could be a data frame in R programming. For this purpose, we can use the below syntax −Syntaxexists("data_frame_name")&&is.data.frame(get("data_frame_name "))Consider the below data frame −Example Live Demoset.seed(101) x1

Program to find largest sum of non-adjacent elements of a list in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:18:41

879 Views

Suppose we have a list of numbers called nums, we will define a function that returns the largest sum of non-adjacent numbers. Here the numbers can be 0 or negative.So, if the input is like [3, 5, 7, 3, 6], then the output will be 16, as we can take 3, 7, and 6 to get 16.To solve this, we will follow these steps−if size of nums

How to find the rank of a matrix in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:14:24

8K+ Views

The rank of a matrix is defined as the maximum number of linearly independent vectors in rows or columns. If we have a matrix with dimensions R x C, having R number of rows and C number of columns, and if R is less than C then the rank of the matrix would be R. To find the rank of a matrix in R, we can use rankMatrix function in Matrix package.Loading Matrix package −library(Matrix)Example Live DemoM1

How to find the sum of corresponding elements in multiple vectors even if they contain NA’s in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:05:29

327 Views

If we have multiple vectors then the sum of the corresponding elements can be found by using rowSums functions and the vectors can be combined by using cbind so that R can easily read the corresponding elements. But if there are NA values in one or more vectors then we also need to add na.rm=TRUE argument.Example Live Demoset.seed(100) x1

Program to find sum of contiguous sublist with maximum sum in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:15:29

988 Views

Suppose we have an array A. We have to find the contiguous sublist which has the maximum sum, and also return its sum. So if the array A is like A = [-2, 1, -3, 4, -1, 2, 1, -5, 4], then the sum will be 6. And the subarray will be [4, -1, 2, 1].To solve this we will try to use Dynamic programming approach.define an array dp same as the size of A, and fill it with 0dp[0] := A[0]for i := 1 to size of A – 1dp[i] := maximum of dp[i – 1] + A[i] and ... Read More

Program to find the maximum sum of circular sublist in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:04:22

259 Views

Suppose we have a list of numbers nums, now consider a circular list of nums where the start and end of nums are neighbors. We have to find the maximum sum of a non-empty sublist in the circular list.So, if the input is like nums = [2, 3, -7, 4, 5], then the output will be 14, as we can take the sub list [4, 5, 2, 3] which sums to 14.To solve this, we will follow these steps −max_sum := negative infinity, cur_max := 0min_sum := positive infinity, cur_min := 0for each num in nums, docur_max := maximum of ... Read More

Advertisements