Server Side Programming Articles - Page 1660 of 2646

How to access elements of nested lists in R?

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

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

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

317 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

255 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

570 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

How to replace missing values recorded with blank spaces in R with NA or any other value?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 14:49:40

1K+ Views

Sometimes when we read data in R, the missing values are recorded as blank spaces and it is difficult to replace them with any value. The reason behind this is we need to know how many spaces we have used in place of missing values. If we know that then assigning any value becomes easy.ExampleConsider the below data frame of vectors x and y.> x y df df x y 1 1 2 3 2 3 2 4 1 43 5 2 2 6 3 7 2 3 ... Read More

How to find the correlation matrix in R using all variables of a data frame?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 14:42:15

828 Views

Correlation matrix helps us to determine the direction and strength of linear relationship among multiple variables at a time. Therefore, it becomes easy to decide which variables should be used in the linear model and which ones could be dropped. We can find the correlation matrix by simply using cor function with data frame name.ExampleConsider the below data frame of continuous variable −> set.seed(9) > x1 x2 x3 x4 x5 df df x1 x2 ... Read More

How to change the order of columns in an R data frame?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 14:32:20

759 Views

Ordering columns might be required when we want to manipulate the data. Manipulation can have several reasons such as cross verification, visualisation, etc. We should also be careful when we change anything in the original data because that might affect our processing. To change the order of columns we can use the single square brackets.ExampleConsider the below data frame −> set.seed(1) > Class Grade Score df df   Class Grade Score 1   a     A     68 2   b     B     39 3   c     C      1 4   ... Read More

How to create bar chart using ggplot2 with chart sub-title in R?

Nizamuddin Siddiqui
Updated on 10-Aug-2020 14:21:44

209 Views

There are different ways to express any chart. The more information we can provide in a chart, the better it is because a picture says thousand words. Since nobody likes to read a long-reports, we should have better reporting of charts. Therefore, we can add a chart title as well as chart sub-title in ggplot2 to help the readers.ExampleConsider the below data −> set.seed(1) > x table(x) x 2 3 4 5 6 7 8 9 11 1 3 4 2 4 2 2 1 1 > df library(ggplot2)Creating a simple bar chart −> ggplot(df, aes(x))+ + geom_bar()OutputCreating a ... Read More

Advertisements