Found 33676 Articles for Programming

Find distinct elements common to all rows of a matrix in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:07:56

308 Views

Suppose we have a square matrix of order m x m; we have to find all the distinct elements common to all rows of the given matrix.So, if the input is like13215417153243615215412152643221942215then the output will be [2, 4, 15]To solve this, we will follow these steps −Define a function sortRows() . This will take matrixn := count of rowsfor i in range 0 to n, dosort the list matrix[i]In the main method, do the following −n := count of rowssortRows(matrix)current_idx := a list of size n, fill with 0for i in range 0 to n, docurrent_idx[i] := 0f := 0while ... Read More

Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i] in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:04:02

239 Views

Suppose we have two arrays A and B of n integers, now consider an array C, where the i-th number will be d*A[i] + B[i] and here d is any arbitrary real number. We have to find d such that array C has maximum number of zeros. Also return the number of zeros.So, if the input is like A = [15, 40, 45] and B = [4, 5, 6], then the output will be d = -0.266666, number of zeros will be 1To solve this, we will follow these steps −n := size of Amy_map := a new mapcount := ... Read More

Find combined mean and variance of two series in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:01:19

833 Views

Suppose we have two different series A1 and A2 of size b and a respectively. We have to find the mean and variance of combined series.So, if the input is like A1 = [24, 46, 35, 79, 13, 77, 35] and A2 = [66, 68, 35, 24, 46], then the output will be Mean = [44.1429, 47.8], sd = [548.694, 294.56], combined mean = 45.6667, d1_square = 2.322, d2_square = 4.5511, combined_var = 446.056To solve this, we will follow these steps −Define a function mean() . This will take arrreturn average of arr elementsDefine a function sd() . This will ... Read More

How to repeat a simulation to a fixed number of times in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:10:14

995 Views

Often, we simulate random values from different distributions in R. The base R provides some inbuilt functions for the same and if we want to repeat the simulation a fixed number of times then we write these inbuilt functions again and again. But we can do multiple simulations using a single line of code with the help of replicate function, that means if we want to simulate ten uniform random variables ten times then it can be done by using replicate function.Examplesreplicate(10, runif(5, 2, 5)) [, 1] [, 2] [, 3] [, 4] [, 5] [, 6] [, 7] [, ... Read More

How to create a column in an R data frame with cumulative sum?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:08:58

608 Views

The cumulative sum is used to determine the total sum of a variable or group and helps us to understand the changes in the values of that variable or group over time. While creating the cumulative, we must be sure that the total sum and the cumulative sum of the last value (depending on the direction of sum) are same. We can use mutate function of dplyr package to find the cumulative and create a column for it.ExampleConsider the below data frame −x1

How to change the starting and ending points of axes labels using plot function in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:07:17

813 Views

When we create a plot using plot function, the axes labels are automatically created based on the values of the variables that is being plotted. It is possible to set a limit to the labels for both the axes, X-axis and Y-axis and we can do this by using xlim and ylim options. For example, if we have the variable limits from 0 to 50 for variable that is going to be plotted on X-axis then it can be done as xlim = c(0,50).Exampleset.seed(99) x

How to create a rank variable using mutate function of dplyr package in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:05:29

557 Views

A rank variable is created to convert a numerical variable into ordinal variable. This is useful for non-parametric analysis because if the distribution of the numerical variable is not normal or there are assumptions of parametric analysis that cannot be followed by the numerical variable then the raw variable values are not analyzed directly. To create a rank variable using mutate function, we can use dense_rank argument.ExampleConsider the below data frame −set.seed(7) x1

How to extract first two characters from a string in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:04:35

2K+ Views

A string can be short or long, also we can have a vector or list of strings as well in R. Extraction of partial string is common when we want to use the strings for single or multiple comparisons. If we want to extract first two characters from a string, we can use substr function and the syntax is substr(“String_object Or String”,start=1,stop=2)Examplesx1

How to create boxplot with horizontal lines on the minimum and maximum in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 13:01:22

1K+ Views

A boxplot shows the minimum, first quartile, median, third quartile, and maximum. When we create a boxplot with ggplot2 it shows the boxplot without horizontal lines on the minimum and maximum, if we want to create the horizontal lines we can use stat_boxplot(geom= 'errorbar') with ggplot function of ggplot2.ExampleConsider the below data frame −set.seed(101) Gender

How to create a scatterplot with colors of the group of points in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:56:05

715 Views

A scatterplot is the plot that has one dependent variable plotted on Y-axis and one independent variable plotted on X-axis. Sometimes the pair of dependent and independent variable are grouped with some characteristics, thus, we might want to create the scatterplot with different colors of the group based on characteristics. For this purpose, we can use colour argument in ggplot function.ExampleConsider the below data frame −set.seed(123) x

Advertisements