Found 2038 Articles for R Programming

How to concatenate string vectors separated with hyphen in R?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 13:03:09

347 Views

The concatenation of string vectors will create combination of the values in the vectors thus, we can use them for interaction between/among the vectors. In R, we can use expand.grid along with apply to create such type of combinations as shown in the below examples.Example 1 Live Demox1

What are some examples of data sets with missing values in R?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 13:01:43

680 Views

Instructors/educators often need to teach missing value imputation to their students; hence they require datasets that contains some missing values or they need to create one. We also have some data sets with missing values available in R such as airquality data in base R and food data in VIM package. There could be many other packages that contain data sets with missing values but it would take a lot of time to explore them. Thus, we have shared the example of airquality and some data sets from VIM package.Example 1 Live Demohead(airquality, 20)Output Ozone Solar.R Wind Temp Month Day 1 41 ... Read More

How to detect a binary column defined with 0 and 1 in an R data frame?

Nizamuddin Siddiqui
Updated on 05-Dec-2020 12:54:26

733 Views

If a column in an R data frame has only two values 0 and 1 then we call it a binary column but it is not necessary that a binary column needs to be defined with 0 and 1 only but it is a general convention. To detect a binary column defined with 0 and 1 in an R data frame, we can use the apply function as shown in the below examples.ExampleConsider the below data frame − Live Demox1

How to subset a data frame by excluding the column names that are stored in a vector in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:59:47

529 Views

Subsetting of a data frame can be done in many ways and one such say is selecting the columns that are stored in a vector. Suppose we have a data frame df that has columns x, y, and z and the column names y and z are stored in a vector called V then we can subset df by excluding column names in V as select(df, -all_of(V)).ExampleConsider the below data frame:Live Demo> x1 x2 x3 x4 df1 df1Outputx1 x2 x3 x4 1 3 4 0 5 2 4 1 2 6 3 4 1 2 3 4 8 1 7 ... Read More

How to convert a named vector to a list in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:57:44

427 Views

A named vector cannot be directly converted to a list because we would need to un-name the vector names and convert those names to names of the list elements. This can be done by using lapply function function. For example, suppose we have a named vector x then it can be converted to a list by using the command x x1 names(x1) x1OutputA B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9 10 11 12 ... Read More

How to find the correlation coefficient between two data frames in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:55:50

4K+ Views

If two data frames in R have equal number of columns then we can find the correlation coefficient among the columns of these data frames which will be the correlation matrix. For example, if we have a data frame df1 that contains column x and y and another data frame df2 that contains column a and b then the correlation coefficient between df1 and df2 can be found by cor(df1, df2).Example1Consider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 39.56630 38.25632 2 39.43689 44.14647 3 40.80479 37.43309 ... Read More

How to add a straight line to a plot in R starting from bottom left and ending at top right?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:53:16

112 Views

The abline function can give us a straight line from intercept 0 with slope 1 in an existing plot. We would need to pass the coefficients inside the function as abline(coef = c(0,1)). Therefore, we can use this function to add a line starting from bottom left and ending at top right. This is also called diagonal line because it joins the end points on one side with the opposite of the other side.Example> plot(1:10,type="n") > abline(coef=c(0,1))Output:

How to find the mean of row values in an R data frame using dplyr?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:51:50

1K+ Views

The mean of row values can be found by using rowwise function of dplyr package along with the mutate function to add the new column of means in the data frame. The rowwise function actually helps R to read the values in the data frame rowwise and then we can use mean function to find the means as shown in the below examples.Example1Consider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 0 8 2 2 3 3 2 5 4 0 5 5 3 2 6 0 10 7 3 5 8 1 7 9 0 ... Read More

How to display central limit theorem using uniform random variable in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:48:55

131 Views

The central limit theorem says that as the sample size increases the distribution of the sample means approaches normal distribution. Therefore, irrespective of the actual population distribution if we take samples of larger size and find the mean of these samples then the distribution of these sample means will be approximately normal. We can display this in R, by creating the histogram of such type of means.Example1> x y

How to find the residual of a glm model in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:46:44

1K+ Views

In a linear model, a residual is the difference between the observed value and the fitted value and it is not different for a general linear model. The difference between linear model and the general linear model is that we use a probability distribution to create a general linear model. If we want to find the residual for a general linear model then resid function can be used just like it is used with the linear model.Example1Consider the below data frame:Live Demo> x1 y1 df1 df1Output x1 y1 1 4 2 2 3 3 3 5 3 4 4 2 ... Read More

Advertisements