Found 2038 Articles for R Programming

How to convert data.table object into a matrix in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 13:54:16

229 Views

A data.table object is very similar to a data frame in R, therefore, converting a data.table object to a matrix is not a difficult job. We just need to use as.matrix function and store the data.table object into a new object that will belong to the matrix, otherwise R will not be able to convert the data.object to a matrix. For example, if we have a data.table object DT then to convert it into a matrix, we should use the below example code −DT_matrix

How to deal with warning message “Removed X rows containing missing values” for a column of an R data frame while creating a plot?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 13:47:16

734 Views

If we have missing values/NA in our data frame and create a plot using ggplot2 without excluding those missing values then we get the warning “Removed X rows containing missing values”, here X will be the number of rows for the column that contain NA values. But the plot will be correct because it will be calculated by excluding the NA’s. To avoid this error, we just need to pass the subset of the data frame column that do not contains NA values as shown in the below example.Consider the below data frame with y column having few NA values ... Read More

How to visualize two categorical variables together in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 13:44:32

5K+ Views

The categorical variables can be easily visualized with the help of mosaic plot. In a mosaic plot, we can have one or more categorical variables and the plot is created based on the frequency of each category in the variables. To create a mosaic plot in base R, we can use mosaicplot function. The categories that have higher frequencies are displayed by a bigger size box and the categories that have less frequency are displayed by smaller size box.Consider the below data frame −Example Live Demox1

How to create a line chart in R using plot function with larger width?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 15:06:04

129 Views

To create a line chart in base R using plot function, we need to use type = "l" so that R understand the plot needs to have a line instead of points. If we want to increase the width of the line then lwd argument can be used. The value lwd = 0 is the default value for the width.Consider the below vector and create the line chart −Examplex

How to create a graph in R using ggplot2 with all the four quadrants?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:38:34

2K+ Views

The default graph created by using ggplot2 package shows the axes labels depending on the starting and ending values of the column of the data frame or vector but we might want to visualize it just like we do in paper form of graphs that shows all of the four quadrants. This can be done by using xlim, ylim, geom_hline, and geom_vline functions with ggplot function of ggplot2 package.Consider the below data frame −Example Live Demox

How to create a subset of matrix in R using greater than or less than a certain value of a column?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:37:17

1K+ Views

Subsetting can be required in many different ways, we can say that there might be infinite number of ways for subsetting as it depends on the objective of the bigger or smaller analysis. One such way is subsetting a matrix based on a certain value of column of the matrix. In R, we can easily do the same with the help of subset function as shown in below example.Example Live DemoM3)Output  [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 4 14 24 34 44 54 64 74 84 94 [2,] 5 15 25 35 45 55 65 75 85 95 [3,] 6 16 26 36 46 56 66 76 86 96 [4,] 7 17 27 37 47 57 67 77 87 97 [5,] 8 18 28 38 48 58 68 78 88 98 [6,] 9 19 29 39 49 59 69 79 89 99 [7,] 10 20 30 40 50 60 70 80 90 100Examplesubset(M,M[,1]75)Output[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 6 16 26 36 46 56 66 76 86 96 [2,] 7 17 27 37 47 57 67 77 87 97 [3,] 8 18 28 38 48 58 68 78 88 98 [4,] 9 19 29 39 49 59 69 79 89 99 [5,] 10 20 30 40 50 60 70 80 90 100Examplesubset(M,M[,9]>81)Output[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 2 12 22 32 42 52 62 72 82 92 [2,] 3 13 23 33 43 53 63 73 83 93 [3,] 4 14 24 34 44 54 64 74 84 94 [4,] 5 15 25 35 45 55 65 75 85 95 [5,] 6 16 26 36 46 56 66 76 86 96 [6,] 7 17 27 37 47 57 67 77 87 97 [7,] 8 18 28 38 48 58 68 78 88 98 [8,] 9 19 29 39 49 59 69 79 89 99 [9,] 10 20 30 40 50 60 70 80 90 100Examplesubset(M,M[,9]

How to create a chart by covering the area of the plot from bottom left to upper right in R?

Nizamuddin Siddiqui
Updated on 14-Oct-2020 14:39:29

47 Views

The plot area in plot window is fixed by default and we can create a lint chart with extended width so that the chart covers the area of the plot from bottom left to upper right. This can be done by using very large width of the line chart with the help of lwd argument.Consider the below vector and create the very wide line chart to cover the plot area −Examplex

How to find the sum of anti-diagonal elements in a matrix in R?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:36:02

462 Views

The anti-diagonal elements in a matrix are the elements that form straight line from right upper side to right bottom side. For example, if we have a matrix as shown below −1 2 3 4 5 6 7 8 9then the diagonal elements would be 1, 5, 9 and the anti-diagonal elements would be 3, 5, 7.To find the sum of these anti-diagonal elements, we can use apply function.Example Live DemoM1

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

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:18:17

698 Views

It is common the find the correlation coefficient between columns of an R data frame but we might want to find the correlation coefficient between rows of two data frames. This might be needed in situations where we expect that there exists some relationship row of an R data frame with row of another data frame. For example, row of an R data frame showing buying trend of a customer in one year and the same row of the other data frame showing buying trend of the same customer in another year.Consider the below data frame −Example Live Demox1Read More

How to deal with warning message `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. in R while creating a histogram?

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:16:46

7K+ Views

The default value for bins is 30 but if we don’t pass that in geom_histogram then the warning message is shown by R in most of the cases. To avoid that, we can simply put bins=30 inside the geom_histogram() function. This will stop showing the warning message.Consider the below data frame −x

Advertisements