Found 33676 Articles for Programming

How to change column names to capital letters from lower case or vice versa in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 14:42:39

2K+ Views

Mostly, we get data that contain column names in lowercase or just first letter is in upper case. If we want to convert those column names to all capital letter words or uppercase then toupper function can be used to the names of the columns. This can be done by using the below syntax −Syntaxnames(“data_frame_name”)

How to extract odds ratio of intercept and slope coefficient from simple logistic model in R?

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

316 Views

To create the simple logistic model, we need to use glm function with family = binomial because the dependent variable in simple logistic model or binomial logistic model has two categories, if there are more than two categories then the model is called as multinomial logistic model. If we want to extract the odds ratio of slope and intercept from the simple logistic model then exp function needs to be used with model object as shown in the below examples.Example Live Demoset.seed(999) x1

How to repeat a column of a data frame and join it with another data frame in R by rows?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 14:01:10

239 Views

Suppose we have a data frame df1 that contains 5 columns and another data frame df2 that contains only column but the data type of the columns in both the data frames is same. Now we might want to add the column of the second data frame starting at the end of the rows of the first data frame by creating the same number of columns as in first data frame. This might be required by researchers to understand the impact of an external variable on the result of the analysis and it can be done with the help of ... Read More

How to create random values in R up to a range of values starting from 1?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 13:56:02

191 Views

Random sampling is a technique used by almost every researcher, analyst, financial analyst, data scientist, or even a leader and if we way that almost everyone uses it at least once in a lifetime then it won’t be surprise. Because we use it in one or the way in our life even if we don’t know about it. To take a random sample or creating random values up to a range of values starting from 1, we can simply use sample function in R. Checkout below examples to understand how this function works for sampling with replacement.Example Live Demosample(100)Output[1] 17 76 ... Read More

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

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

348 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

952 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

187 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]

Advertisements