Found 2038 Articles for R Programming

How to display the superscript for a single variable in a base R plot?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:00:09

123 Views

To display the superscript in a base R plot, we would need to use expression function inside text function. For example, if we want to display X-square inside a blank plot in base R then we can use the below code:plot(1:10,type="n") text(2,2,expression(X^2))Example1> plot(1:10,type="n") > text(2,2,expression(X^2==4))Output:Example2> text(5,5,expression(X^5==Center))Output:Example3> text(9,9,expression(Squared^2))Output:

How to extract columns based on particular column values of an R data frame that matcha pattern?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 04:58:32

376 Views

The column values of an R data frame can be easily extracted by subsetting with single square brackets but if we want to extract the column values that match a pattern then we need to use grepl function inside single square brackets, this will help us to match the pattern of the values in the data frame columns.ExampleConsider the below data frame:Live Demo> set.seed(271) > x1 x2 df1 df1Output x1 x2 1 A242 B71 2 A123 B71 3 A242 B81 4 A242 B87 5 A123 B71 6 A321 B71 7 ... Read More

How to create a sphere in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 04:52:10

445 Views

To create a sphere, we can use spheres3d function of rgl package. The rgl package is specifically designed to create real-time interactive 3D plots. If we want to create a sphere then we need to pass the values for the three axes and the radius of the sphere. We can also change the color of the sphere by introducing color argument inside spheres3d function.Example1Loading rgl package and creating a sphere:> library(rgl) > spheres3d(x=1,y=1,z=1,radius=1)Output:Example2> spheres3d(0,0,0,radius=1,color="blue")Output:Example3> spheres3d(0,0,0,radius=1,color="red")Output:

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

360 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

273 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

447 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

469 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

73 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

763 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