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

Sum of Corresponding Elements in Multiple Vectors in R

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

354 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

Find Maximum Sum of Circular Sublist in Python

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

281 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

Renumber Rows if Unordered in R

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

523 Views

When we create a sample using inbuilt or imported data set then the numbering for selected rows as in the original data set, therefore, the numbering becomes unordered. To change this unordered numbering to a sequence say starting from one to the total number of rows in the sample, we can use 1:nrow(“sample_object_name”).Consider the below data frame −Example Live Demoset.seed(999) x

Find K-Sized List with Minimum Difference Between Largest and Smallest Item in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:55:35

241 Views

Suppose we have a list of numbers called nums and an integer k, we have to select elements from nums to create a list of size k such that the difference between the largest integer in the list and the smallest integer is as low as possible. And we will return this difference.So, if the input is like nums = [3, 11, 6, 2, 9], k = 3, then the output will be 4, as the best list we can make is [2, 3, 6].To solve this, we will follow these steps −sort the list numsls := a new listfor ... Read More

Create Two Plots Using ggplot2 in R

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:54:43

695 Views

The two plots created by using ggplot2 can be arranged in a vertical manner with the help gridExtra package, we simply needs to use grid.arrange function to do so. For example, if we have two plots created by using ggplot2 and saved in objects p1 and p2 then they can be vertically arranged as grid.arrange(p1,p2)Consider the below data frame −Exampleset.seed(151) x

Find Largest Grouping of Anagrams from a Word List in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:52:52

252 Views

Suppose we have a list of strings words, we have to group all anagrams together and return the size of the largest grouping.So, if the input is like words = ["xy", "yx", "xyz", "zyx", "yzx", "wwwww"], then the output will be 3, as ["xyz", "zyx", "yzx"] is the largest grouping.To solve this, we will follow these steps −lookup := a new map, initially emptyres := 0for each i in words, dop := sort i in lexicographical wayif p is in lookup, increase count, otherwise 1res := maximum of res and lookup[p]return resLet us see the following implementation to get better ... Read More

Create Barplot from Data Frame in R Using Rows as Categories

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:51:45

7K+ Views

If we have small number of rows then we might want to create bar plot for rows instead of using columns as categories. This can be done by using barplot function but we need to convert the data frame to matrix and take the transpose of it. For example, if we have a data frame data_frame with 4 rows and 4 columns, then the barplot with rows as categories can be created as barplot(t(as.matrix(data_frame)),beside=TRUE)Consider the below data frame −Example Live Demox1

Split String Values Containing Special Characters in R

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:48:49

1K+ Views

When we have a single long string or a vector of string values and the values within the string are separated by some special characters then splitting the values can help us to properly understand those strings. This could happen in situations when the string data is recorded with mistakes or have some other purpose. We can do the splitting using strsplit function.Example Live Demox1

Find Minimum Required Chances to Form a String with K Unique Characters in Python

Arnab Chakraborty
Updated on 09-Oct-2020 14:47:31

488 Views

Suppose we have a string s of lowercase alphabet characters, and another number k, we have to find the minimum number of required changes in the string so that the resulting string has at most k distinct characters. In this case The change is actually changing a single character to any other character.So, if the input is like s = "wxxyyzzxx", k = 3, then the output will be 1, as we can remove the letter "w" to get 3 distinct characters (x, y, and z).To solve this, we will follow these steps −count := a map of each character ... Read More

Advertisements