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

Replace Missing Values in R Data Frame Column

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

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

Name Data Frame Column with Vector Value 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

Get 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

Replace Sign Combined with Specific Values in R Data Frame

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

125 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

Find 95% Confidence Interval for 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

Convert X-Axis Label to Italic in Bar Plot 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

Find Correlation Matrix for Data Frame with Missing Values in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:07:29

480 Views

To find the correlation matrix for a data frame, we can use cor function with the data frame object name but if there exist missing values in the data frame then it is not that straight forward. In such type of situations, we can use complete.obs with the cor function so that the missing values will be ignored while calculating the correlation coefficients.Example1Consider the below data frame:Live Demo> x1 x2 x3 df1 df1Output x1 x2 x3 1 NA 3 512 2 8 7 512 3 5 2 520 4 NA 1 NA 5 NA 2 512 6 NA 4 ... Read More

Generate Orthogonal Polynomials in R

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:05:29

676 Views

We can say that orthogonal is a synonym of perpendicular. If the inner product (inner product is generalization of dot product) of two polynomials is zero then we call them orthogonal polynomials. In R, we can find the orthogonal product by using poly function as shown in the below examples.Example1Live Demo> x xOutput[1] 1.53798786 -0.85463326 2.39444451 0.82559418 -2.22197322 -1.04243823 [7] -0.04693054 -0.68691236 -1.63040923 -1.42408865Example> orthogonal_x orthogonal_xOutput 1 2 [1, ] 0.41743651 -0.01687537 [2, ] -0.12158589 -0.21414848 [3, ] 0.61038362 0.54027924 [4, ... Read More

Advertisements