Found 33676 Articles for Programming

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

701 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

628 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

172 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

188 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

How to add a column in an R data frame with consecutive numbers?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:45:10

7K+ Views

Addition of a column with consecutive might have different objectives such as getting the sequence of numbers, representing serial numbers, representing ids, identification of each row, or a variable. We can use the sequence starting from any number up to the number of rows if we know the number of rows for this purpose.Example1Consider the below data frame:Live Demo> x1 x2 df1 df1Output x1 x2 1 6.137898 5.203712 2 5.283467 5.057344 3 5.873749 4.907388 4 7.628762 5.012650 5 4.134700 4.988379 6 5.340686 4.684900 7 5.126999 4.821752 8 3.722762 4.974044 9 ... Read More

How to find the cumulative sum but restarts it if a value is 1 in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:42:36

909 Views

Sometimes we want to find the conditional cumulative sums and these conditions can be resetting the cumulative if a particular value occurs. For example, finding the cumulative sum of a variable frame but restarting the sum if 1 occurs. In R, we can do this with the help of with, ave and cumusum function as shown in the below examples.Example1Consider the below data frame:Live Demo> ID Ratings df1 df1Output ID Ratings 1 1 0 2 2 2 3 3 0 4 4 0 5 5 0 6 6 ... Read More

How to find the group-wise correlation coefficient in R?

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

929 Views

If we have two continuous and one categorical column in an R data frame then we can find the correlation coefficient between continuous values for the categories in the categorical column. For this purpose, we can use by function and pass the cor function with the spearman method as shown in the below examples.Example1Consider the below data frame:Live Demo> x1 y1 z1 df1 df1Output x1 y1 z1 1 A 1.1155324 2 2 C 0.9801564 3 3 B 0.9116162 1 4 A 0.8406772 3 5 C 0.8009355 2 6 A 0.9331637 2 7 B 1.0642089 ... Read More

Advertisements