Create a Matrix Using Vector of String Values in R

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:40:50

2K+ Views

You might have heard that matrix can contain only numerical values but it is also possible to create a matrix with string values, and of course calculations using these types of matrices would not be possible. To create a matrix using string values, we can first create a vector of strings then define its dimension with dim function and that will convert the vector into matrix of string values.Example Live DemoM1

Check if We Can Fill Square with Distinct Elements in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:39:48

126 Views

Suppose we have one n × n matrix containing values from 0 to n. Here 0 represents an unfilled square, we have to check whether we can fill empty squares such that in each row and each column every number from 1 to n appears exactly once.So, if the input is like002201123then the output will be True, as we can set the matrix to312231123To solve this, we will follow these steps −Define a function find_empty_cell() . This will take matrix, nfor i in range 0 to n, dofor j in range 0 to n, doif matrix[i, j] is same as ... Read More

Find Position of Values in Vector in R

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

750 Views

Finding the position of one of more values that are common in two vectors can be easily done with the help of match function. The match function will match the values in first and second vector then return the index or position of these common values in second vector.Example Live Demoset.seed(145) x1

Plot Values with Log Scales on X and Y 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

Find Largest Sum of Path Between Two Nodes in Binary Tree using Python

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

268 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

Set Level of a Factor Column to NA in R Data Frame

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

765 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"]

Create 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

Find Largest Sum of Non-Adjacent Elements in a List using Python

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

902 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

Check If a Data Frame Exists 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

Find Sum of Contiguous Sublist with Maximum Sum in Python

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

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

Advertisements