Found 2038 Articles for R Programming

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

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

6K+ 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

693 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

807 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

How to subset rows of an R data frame using grepl function?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:38:07

4K+ Views

The grepl function in R search for matches to argument pattern within each element of a character vector or column of an R data frame. If we want to subset rows of an R data frame using grepl then subsetting with single-square brackets and grepl can be used by accessing the column that contains character values.Example1Consider the below data frame:Live Demo> x1 y1 z1 df1 df1Output x1 y1 z1 1 A 0.8833979 5 2 B 0.5400075 1 3 C 0.6923827 3 4 B 1.5069186 2 5 B 0.8190962 2 6 B 0.8296171 1 7 ... Read More

How to find the column means of a column based on another column values that represent factor in an R data frame?

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

1K+ Views

If we have a column that represent factor then we might want to find the mean of values in other column(s) for the factor levels. This is helpful in comparing the levels of the factor. In R, we can find the mean for such type of data by using aggregate function. Check out the below examples to understand how it can be done.Example1Consider the below data frame:Live Demo> x1 y1 df1 df1Output x1 y1 1 D 5.801197 2 B 3.432060 3 B 6.154168 4 A 5.466655 5 D 5.171689 6 C 5.175170 7 B 5.353469 8 D ... Read More

How to convert a string vector into an integer vector in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:34:25

994 Views

A string vector contains element inside double-quotes and an integer vector does not have any quotes. Sometimes integer values are stored in double-quotes hence the vector of these values is treated as a string vector in R but we need the integer values to perform mathematical operations. Therefore, we can use as.integer function to convert the string vector into an integer vector.Example1Live Demo> x1 x1Output[1] "3" "2" "1" "2" "1" "1" "1" "1" "1" "1" "3" "3" "3" "1" "2" "1" "1" "2" [19] "2" "3" "3" "3" "3" "2" "3" "3" "3" "2" "1" "2" "3" "3" "2" "1" ... Read More

How to find the mean of a square matrix elements by excluding diagonal elements in R?

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

192 Views

There are many ways to find the mean of a matrix elements by excluding diagonal elements, this mean is actually the mean of lower triangular matrix and the upper triangular matrix. We can simply use mean function by creating a vector of lower and upper triangular matrix as shown in the below examples.Example1Live Demo> M1 M1Output   [, 1] [, 2] [, 3] [, 4] [1, ] 1    6    3    6 [2, ] 8    5    3    4 [3, ] 5    4    4    6 [4, ] 5    5    3    4 ... Read More

How to create a bar plot using ggplot2 with one bar having black border in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:28:24

710 Views

The bar plot can be easily created with the help of geom_bar. But if we want to have a different border for a particular bar then we first need to create the bar plot and store it in an object. After that we need to add the original plot with the bar for which we want to have a black border. Check out the below example to understand how it can be done.ExampleConsider the below data frame:Live Demo> Group Freq df dfOutput Group Freq 1 G1 18 2 G2 27 3 G3 24Loading ggplot2 ... Read More

How to reduce a matrix in R to echelon form?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:26:17

1K+ Views

The echelon form of a matrix is the matrix that has the following characteristics:1. The first non-zero element in each row, called the leading entry, is 1.2. Each leading entry is in a column to the right of the leading entry in the previous row.3. Rows with all zero elements, if any, are below rows having a non-zero element.In R, we can use echelon function of matlib package to find the echelon form of the matrix.ExampleLive Demo> M MOutput [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 8 11 3 10 13 [2, ] ... Read More

How to remove rows based on blanks in a column from a data frame in R?

Nizamuddin Siddiqui
Updated on 23-Nov-2020 10:24:09

4K+ Views

Sometimes data is incorrectly entered into systems and that is the reason we must be careful while doing data cleaning before proceeding to analysis. A data collector or the sampled unit might enter blank to an answer if he or she does not find an appropriate option for the question. This also happens if the questionnaire is not properly designed or blank is filled by mistake. Also, if we have categorical variable then a control category might be filled with blank or we may want to have a blank category to use a new one at later stage. Whatever the ... Read More

Advertisements