Programming Articles - Page 1728 of 3363

How to save the summary statistics into a data frame in R?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 08:32:19

5K+ Views

When we find the summary statistics of a data frame then the output is returned as a table and each of the column records the minimum, first quartile, median, median, third quartile, and maximum with their names. If we want to save this summary as a data frame then it is better to calculate it with apply function and store it as data.frame.ExampleConsider the below data frame − Live Demox1

How to determine the row that have min and max values in an R data frame column?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 08:22:05

2K+ Views

In data analysis, often we require to determine the minimum and maximum values because these values help us to understand the limits of a column or variable under consideration. This can be done by using which.max for maximum and which.min for minimum with single square brackets to extract the rows.ExampleConsider the below data frame − Live Demox1

How to reduce the space between two plots that are joined with grid.arrange in R?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 08:14:01

2K+ Views

When we join or combine plots using grid.arrange the scales of the first plot comes in between as X-axis even if the independent variable in both of the plots is same.Therefore, we might want to remove the space between the plots while joining to get only one X-axis. This can be done by using theme function.ExampleConsider the below data frame − Live Demoset.seed(123) x

How to create gridlines that matches with Y-axis values in the plot created by using plot function in R?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 08:09:05

388 Views

When we create a plot in R and draw gridlines then the gridlines are drawn on the basis of the values provided inside the grid function, therefore, it may or may not match with the Y-axis labels. But it can be done, we just need to set the values inside the grid function to NULL.ExampleConsider the below plot − Live Demox

How to create a column with binary variable based on a condition of other variable in an R data frame?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 08:00:43

9K+ Views

Sometimes we need to create extra variable to add more information about the present data because it adds value. This is especially used while we do feature engineering. If we come to know about something that may affect our response then we prefer to use it as a variable in our data, hence we make up that with the data we have. For example, creating another variable applying conditions on other variable such as creating a binary variable for goodness if the frequency matches a certain criterion.ExampleConsider the below data frame − Live Demoset.seed(100) Group

How to find the raise to the power of all values in an R vector?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 07:49:38

1K+ Views

Often, we need to find the power of a value or the power of all values in an R vector, especially in cases when we are dealing with polynomial models. This can be done by using ^ sign as we do in Excel. For example, if we have a vector x then the square of all values in x can be found as x^2.Example Live Demox1

How to change the position of axes titles to top for X-variable and to right for Y-variable in R?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 07:46:52

297 Views

The default position of axes titles in any software or programming language for any 2D graph is bottom for X-axis and left for Y-axis but we might to change the position of these titles to top and right respectively. This can be done by using scale_x_continuous(position="top") and scale_y_continuous(position="right") functions of ggplot2 package.ExampleConsider the below data frame − Live Demoset.seed(101) x

How to find similar words in vector of strings in R?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 07:43:16

1K+ Views

Sometimes strings in a vector of strings have spelling errors and we want to extract the similar words to avoid that spelling error because similar words are likely to represent the correct and incorrect form of a word. This can be done by using agrep with lapply function.Example 1 Live Demox1

How to change the scale of Y-axis if the histogram bars are higher than the axis size in R?

Nizamuddin Siddiqui
Updated on 09-Sep-2020 07:31:33

2K+ Views

When we create a histogram using hist function in R, often the Y-axis labels are smaller than the one or more bars of the histogram. Therefore, the histogram does not look appealing and it becomes a little difficult to match the Y-axis values with the bars size.To solve this problem, we can use ylim argument of hist function in which the range can be supplied to plot on the Y-axis labels.ExampleConsider the below data and its histogram − Live Demoset.seed(101) x

How to find the number of values in a column of an R data frame that are not zero?

Nizamuddin Siddiqui
Updated on 08-Sep-2020 14:57:37

338 Views

If an R data frame has numerical columns then it is also possible that there exist zeros in few or all columns and we might be interested in finding the number of non-zero values in a column. This will help us to compare the columns based on the number on non-zero values and it can be done by using colSums.ExampleConsider the below data frame − Live Demox1

Advertisements