Found 2038 Articles for R Programming

How to select multiple elements of a list in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 06:23:49

917 Views

Generally, a list in R contains a large number of elements and each element can be of different type which is a great thing about lists. Since we can store type of data as a list element therefore storage and selection to different type of data becomes easier. And we can also select single or multiple elements of the list at a time. This can be done with the help of single square brackets.ExampleConsider the below list −> list_data list_data [[1]] [1] "India" [[2]] [1] "China" [[3]] [1] 21 32 11 [[4]] [1] "a" "b" "c" "d" "e" [[5]] ... Read More

How to create a subset of rows or columns of a matrix in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:26:10

1K+ Views

A matrix can have multiple rows and columns like a data frame. As in data frames, we sometimes require to take subsets, the same might be required with matrices. But subsetting matrices data is quite simple as compared to subsetting a data frame.ExampleConsider the below matrix −> M M [, 1] [, 2] [, 3] [, 4] [, 5] [1, ] 1 6 11 16 21 [2, ] 2 7 12 17 22 [3, ] 3 8 13 18 23 [4, ] 4 9 14 19 24 [5, ] 5 10 15 20 25Subsetting columns of matrix M −> M[, ... Read More

How to join two data frames based one factor column with different levels and the name of the columns in R using dplyr?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:20:59

391 Views

When there is a common factor with different levels, the joining of data frames is possible but the result will present all the levels with dplyr. We can make use of left_join function to join the two data frames but the size of the first data frame must be greater than the second data frame if they are not same.ExampleConsider the below data frames −> Class df1 df1 Class 1 Statistics 2 Maths 3 Chemistry 4 Physics 5 Economics 6 Political Science 7 Geography > Subject Age df2 df2 Subject Age 1 Maths 18 2 Chemistry 21 3 Physics 22 ... Read More

How to select top or bottom n elements of a vector in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:17:06

423 Views

Selection of top or bottom elements can be done with the help of head and tail function in R. It is required when we want to understand the data in a vector or perform some calculation for partial data.ExampleConsider the below vectors, we will use head and tail to select top and bottom elements in these vectors by using positive and negative signs. These will have a different way to select the elements.> x x [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" [20] "t" "u" "v" "w" ... Read More

How to access elements of nested lists in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:15:25

3K+ Views

Sometimes the lists are contained in another list but we want to access the nested list’s elements. Since these elements are part of a list then cannot be directly accessed, first we need to access the broader list and then the list that contains the element to reach the actual element.ExampleConsider the lists x1, x2, x3, x4, and x4 and the Total_List that contains these lists −> x1 x2 x3 x4 x5 Total_Lists Total_Lists [[1]] [[1]][[1]] [1] 1 2 3 4 5 [[1]][[2]] [1] 6 7 8 9 10 [[1]][[3]] [1] 11 12 13 14 15 [[2]] [[2]][[1]] [1] "a" ... Read More

How to increase the printing limit in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 13:14:02

2K+ Views

When we deal with large data then the problem of printing the data or output of the analysis arises. Due to this problem, it becomes difficult to have a look at our complete but it can be avoided. Before importing any large data or performing any calculation that may result in big output, we can change the limit of the printing by using max.print option.Example> set.seed(1) > sample(1:1000, 555555, replace=TRUE)Output[99681] 223 62  961 304  5  262 519 357 415 167 855 523 268 486 [99695] 370 916 703 179 813 833 177 154 72  789 924 918 486 647 [99709] ... Read More

How to split a big data frame into smaller ones in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 15:38:43

1K+ Views

Dealing with big data frames is not an easy task therefore we might want to split that into some smaller data frames. These smaller data frames can be extracted from the big one based on some criteria such as for levels of a factor variable or with some other conditions. This can be done by using split function.ExampleConsider the below data frame −> set.seed(1) > Grades Age Category df head(df, 20) Grades Age Category 1 A 25 6 2 B 4 ... Read More

How to create a polynomial model in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 15:25:29

178 Views

Most of the times the explanatory variables are not linearly related to the response variable and we need to find the best model for our data. In this type of situations, we move on to polynomial models to check whether they will be helpful in determining the accuracy of the predictions. This can be done by using power of the independent variables in lm function.ExampleConsider the below data frame −> set.seed(99) > x1 x2 x3 x4 y df PolynomialModel1 summary(PolynomialModel1) Call: lm(formula = y ~ x1 + I(x1^2) + x2 + x3 + x4) Residuals: Min 1Q Median 3Q Max ... Read More

How to add a column between columns or after last column in an R data frame?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 15:17:08

157 Views

Since no one is perfect, people might forget to add all columns that are necessary for the analysis but this problem can be solved. If a column is missing in our data frame and we came to know about it later then it can be added easily with the help of reordering the columns.ExampleConsider the below data frame −> x1 x2 x3 df df x1 x2 x3 1 1 a 1 2 2 b 2 3 3 c 1 4 4 d 2 5 5 e 1 ... Read More

How to delete a row from an R data frame?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 15:06:44

394 Views

While doing the analysis, we might come across with data that is not required and we want to delete it. This data can be a whole row or multiple rows. For example, if a row contains values greater than, less than or equal to a certain threshold then it might not be needed, therefore we can delete it. In R, we achieve this with the help of subsetting through single square brackets.ExampleConsider the below data frame −> set.seed(99) > x1 x2 x3 x4 x5 df df ... Read More

Advertisements