Found 33676 Articles for Programming

Bomb Enemy in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:38:01

359 Views

Suppose we have a 2D grid, here each cell is either a wall 'W', an enemy 'E' or that is empty '0', We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.So, if the input is likethen the output will be 3, as placing bomb at the green place, it will kill three enemies.To solve this, we will follow these steps −ret := 0n := row count ... Read More

Sort Transformed Array in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:35:05

257 Views

Suppose we have a sorted array of integer nums and integer values a, b and c. We have to apply a quadratic function of the form f(x) = ax^2 + bx + c to each element x in the array. And the final array must be in sorted order.So, if the input is like nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5, then the output will be [3, 9, 15, 33]To solve this, we will follow these steps −Define function f(), that takes x, a, b, c −return ax^2 + bx + cFrom ... Read More

Line Reflection in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:32:53

491 Views

Suppose we have n points on a 2D plane, we have to check whether there is any line parallel to y-axis that reflect the given points symmetrically, in other words, check whether there exists a line that after reflecting all points over the given line the set of the original points is the same that the reflected ones.So, if the input is like points = [[1, 1], [-1, 1]]then the output will be trueTo solve this, we will follow these steps −Define one set okn := size of pointsminVal := infmaxVal := -inffor initialize i := 0, when i < ... Read More

How to replace missing values in a column with corresponding values in other column of an R data frame?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:23:52

489 Views

Often, we get missing data for analysis and we need to replace those missing values with something. Sometimes, we might want to replace them with the corresponding values in other column especially in situations when both the columns depict similar characteristics. This type of replacement can be easily done with the help of mutate function of dplyr package as shown in the below examples.Example1Consider the below data frame:Live Demo> set.seed(214) > x1 x2 df1 df1Output x1 x2 1 4 75 2 8 24 3 5 38 4 4 38 5 7 NA 6 6 24 7 10 75 8 4 75 ... Read More

How to create a matrix using vector generated with rep function in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:21:56

383 Views

Matrix can be only generated if we pass an even number of elements for it. If we want to create a matrix using vector generated with rep function then the length of this vector must be divisible by 2. For example, if we have a vector x that is created with rep function and it’s length is 20 then the matrix say M of size 10x2 using that vector can be constructed by using matrix(x, ncol=2).Example 1Live Demo> x M1 M1Output[, 1] [, 2] [1, ] 10 10 [2, ] 4 4 [3, ] 7 7 [4, ] 3 3 ... Read More

How to name a data frame column with a vector value that has the same name in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:19:03

579 Views

To change the column name of a data frame in R, we can use setNames function. For example, if we have a data frame called df that contains column x and we want to change it to value “Ratings” which is stored in a vector called x then we can use the code df x y yOutput x 1 3 2 8 3 3 4 9 5 5 6 5 7 10 8 2 9 6 10 6 11 3 12 5 13 9 14 1 15 1 16 6 17 2 18 6 19 10 20 6Changing x in y to Ratings:Example> y yOutputRatings 1 3 2 8 3 3 4 9 5 5 6 5 7 10 8 2 9 6 10 6 11 3 12 5 13 9 14 1 15 1 16 6 17 2 18 6 19 10 20 6Let’s have a look at another example:ExampleLive Demo> S df_Salary df_SalaryOutput S 1 31827 2 24697 3 45790 4 45345 5 22294 6 30749 7 37721 8 33535 9 45941 10 24028 11 48927 12 33818 13 49152 14 43334 15 20294 16 29664 17 23358 18 20475 19 39355 20 40386Changing S in df_Salary to Salary:Example> df_Salary df_SalaryOutputSalary 1 31827 2 24697 3 45790 4 45345 5 22294 6 30749 7 37721 8 33535 9 45941 10 24028 11 48927 12 33818 13 49152 14 43334 15 20294 16 29664 17 23358 18 20475 19 39355 20 40386

How to get the list of packages installed in base R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:16:59

643 Views

There is no limitation to installation of packages in R but base R also has some packages associated to it. Hence, there is no need to install or load them in R console every time. We can directly use any of the base R package functions to perform the analysis. If we want to get the list of these package then we can use the code as shown below:Example> installed.packages(priority="base")OutputPackage LibPath Version Priority base "base" "C:/Program Files/R/R-4.0.2/library" "4.0.2" "base" compiler "compiler" "C:/Program Files/R/R-4.0.2/library" "4.0.2" "base" datasets "datasets" "C:/Program Files/R/R-4.0.2/library" "4.0.2" "base" graphics "graphics" "C:/Program Files/R/R-4.0.2/library" "4.0.2" "base" grDevices "grDevices" "C:/Program ... Read More

How to replace $ sign combined with some specific values in an R data frame?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:15:03

124 Views

Sometimes we get very dirty data and that is the reason data analysis is a difficult task. Most of the data scientists look for clean data but it is almost impossible due to data warehouses often just focus on the data availability instead of the quality of data. One of the head scratching situations is getting an unnecessary value placed at different position in a random manner, $ sign is also a that type of value. We can remove this from an R data frame by using lapply function.ExampleConsider the below data frame:Live Demo> x y df1 df1Outputx y 1 ... Read More

How to find the 95% confidence interval for the glm model in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:12:21

6K+ Views

To find the confidence interval for a lm model (linear regression model), we can use confint function and there is no need to pass the confidence level because the default is 95%. This can be also used for a glm model (general linear model). Check out the below examples to see the output of confint for a glm model.Example1Live Demo> set.seed(3214) > x1 y1 Model1 summary(Model1)OutputCall: glm(formula = y1 ~ x1, family = "binomial") Deviance Residuals: Min 1Q Median 3Q Max -1.6360 -1.4156 0.7800 0.8567 0.9946 Coefficients: Estimate Std. Error z value Pr(>|z|) (Intercept) 0.34851 1.17554 0.296 0.767 ... Read More

How to convert the X-axis label in a bar plot to italic using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:09:53

1K+ Views

Obviously, the default font of axes-labels is not italic in R just like any other statistical analysis tool but we can make it using ggplot2. For this purpose, we can use theme function of ggplot2 package where we have an option to change the font of the axis labels using axis.text.x argument.ExampleConsider the below data frame:Live Demo> x y df dfOutput x y 1 A 24 2 B 23 3 C 25 4 D 27Loading ggplot2 package and creating a bar plot:Example> library(ggplot2) > ggplot(df, aes(x, y))+geom_bar(stat="identity")Output:Creating bar plot with italic X-axis labels:Example> ggplot(df, aes(x, y))+geom_bar(stat="identity")+theme(axis.text.x=element_text(face=c("italic", "italic", "italic", ... Read More

Advertisements