Found 2003 Questions for R Programming

How to display a list of plots with the help of grid.arrange in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:49:28
In data analysis, we deal with many variables at a time and we want to visualize the histogram of these variables at a time. This helps us to understand the distribution of each variable in the data set, therefore we can apply the appropriate technique to deal with those variables. To create a list of plots we can use grid.arrange function in gridExtra package that can arrange plots based on our need.ExampleConsider the below data frame −> set.seed(10) > df head(df, 20)             x1     x2           x3     ... Read More

How to create a plot for the response variable grouped by two columns using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:38:28
When two categorical variables make an impact on the response variable together then it is necessary to visualize their effect graphically because this graph helps us to understand the variation in the effect. Therefore, we can create a plot for the response variable that changes with one or both of the categorical independent variables. This can be done with the help of using interaction function in ggplot2.ExampleConsider the below data frame −> set.seed(1) > y Group1 Group2 df head(df, 20) y Group1 Group2 1    1 a Ph1 2    1 b Ph1 3    2 c Ph1 4   ... Read More

How to write text and output it as a text file using R?

Nizamuddin Siddiqui
Updated on 11-Jul-2020 12:56:06
The easiest way to write text and obtain it as an output is done by using writeLines function and cat function, and the output of these functions are connected with the help fileConn and sink.Example> fileConn writeLines(c("TutorialsPoint", "SIMPLY EASY LEARNING"), fileConn) > close(fileConn)We can do the same and view these files in R as follows −> fileConn writeLines(c(paste("TutorialsPoint", "E-learning"), "2006", "Video Courses", "Tutorials", "Books"), fileConn) > close(fileConn) > file.show("example.txt")Using sink function> sink("example3.txt") > cat("TutorialsPoint", "E-learning") > cat("") > cat("2006") > cat("") > cat("Video Courses") > cat("") > cat("Tutorials") > cat("") > cat("Books") > sink()Using only cat function> cat("TutorialsPoint E-learning", ... Read More

Which function should be used to load a package in R, require or library?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 15:02:01
The main difference between require and library is that require was designed to use inside functions and library is used to load packages. If a package is not available then library throws an error on the other hand require gives a warning message.Using library> library(xyz) Error in library(xyz) : there is no package called ‘xyz’Using requirerequire(xyz) Loading required package: xyz Warning message: In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : there is no package called ‘xyz’Here we can see that the library shows an error and require gives a warning message, since warnings are mostly avoided ... Read More

How to deal with “could not find function” error in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 15:01:06
The error “could not find function” occurs due to the following reasons −Function name is incorrect. Always remember that function names are case sensitive in R.The package that contains the function was not installed. We have to install packages in R once before using any function contained by them. It can be done as install.packages("package_name")The package was not loaded before using the function. To use the function that is contained in a package we need to load the package and it can be done as library("package_name").Version of R is older where the function you are using does not exist.If you ... Read More

How to do an inner join and outer join of two data frames in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 15:00:12
An inner join return only the rows in which the left table have matching keys in the right table and an outer join returns all rows from both tables, join records from the left which have matching keys in the right table. This can be done by using merge function.ExampleInner Join> df1 = data.frame(CustomerId = c(1:5), Product = c(rep("Biscuit", 3), rep("Cream", 2))) > df1   CustomerId Product 1 1 Biscuit 2 2 Biscuit 3 3 Biscuit 4 4 Cream 5 5 Cream > df2 = data.frame(CustomerId = c(2, 5, 6), City = c(rep("Chicago", 2), rep("NewYorkCity", 1))) > df2 CustomerId City ... Read More

Why we should use set.seed in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:58:52
The use of set.seed is to make sure that we get the same results for randomization. If we randomly select some observations for any task in R or in any statistical software it results in different values all the time and this happens because of randomization. If we want to keep the values that are produced at first random selection then we can do this by storing them in an object after randomization or we can fix the randomization procedure so that we get the same results all the time.ExampleRandomization without set.seed> sample(1:10) [1] 4 10 5 3 1 6 ... Read More

How to make list of data frames in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:57:34
This can be done by using list function.Example> df1

What is the use of tilde operator (~) in R?

Nizamuddin Siddiqui
Updated on 11-Jul-2020 12:55:17
Tilde operator is used to define the relationship between dependent variable and independent variables in a statistical model formula. The variable on the left-hand side of tilde operator is the dependent variable and the variable(s) on the right-hand side of tilde operator is/are called the independent variable(s). So, tilde operator helps to define that dependent variable depends on the independent variable(s) that are on the right-hand side of tilde operator.Example> Regression_Model Regression_Data Regression_Model_New < - lm(y~ . , data = Regression_Data)This will have the same output as the previous model, but we cannot use tilde with dot if ... Read More

How to filter rows that contain a certain string in R?

Nizamuddin Siddiqui
Updated on 06-Jul-2020 14:55:13
We can do this by using filter and grepl function of dplyr package.ExampleConsider the mtcars data set.> data(mtcars) > head(mtcars) mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 ... Read More
Advertisements