R Programming Articles

Page 127 of 174

How to divide all columns by one column and keeping original data in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 4K+ Views

To divide all columns of data frame in R by one column and keeping the original data, we can use mutate_at function of dplyr package along with list function.For example, if we have a data frame called df that contains five columns say x, y, z, a, and b then we can divide all columns by b and keep the original data by using the below given command −df%>%mutate_at(vars(x:b),list(All_by_b=~./b))Example 1Following snippet creates a sample data frame −x1

Read More

Create histogram with horizontal boxplot on top in base R.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 793 Views

To create a histogram with horizontal boxplot on top in base R, we first need to define the layout of the plotting area with layout function and par function margin (mar) then the boxplot will be created and after that the histogram will be created. While creating the boxplot and histogram we need to make sure that the ylim for boxplot and xlim for histogram are same.Check out the below Example to understand how it can be done.ExampleTo create a histogram with horizontal boxplot on top in base R, use the following snippet −x

Read More

How to increase the width of lines for histogram like plots in base R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 580 Views

The histogram like plot in base R is the plots of vertical lines instead of points in a vector or column of a data frame. If we increase the width of these lines then the plot becomes more like a histogram as the increment in widths make the vertical lines look like bars.To increase the width of lines for histogram like plots in base R, we can use lwd argument.Check out the below example to understand the difference between point chart and histogram like plot in base R.ExampleUse the code given below to increase the width of lines for histogram ...

Read More

How to increase the X-axis labels font size using ggplot2 in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 34K+ Views

To increase the X-axis labels font size using ggplot2, we can use axis.text.x argument of theme function where we can define the text size for axis element. This might be required when we want viewers to critically examine the X-axis labels and especially in situations when we change the scale for X-axis.Check out the below given example to understand how it can be done.ExampleFollowing snippet creates a sample data frame −x

Read More

How to create a group column in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 1K+ Views

Suppose we have a data frame called df that contains two columns say X and Y then we can create a group column based on X and Y by converting df into a data.table object and creating list of values in X and Y with list function.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −x1

Read More

How to multiply two matrices in R if they contain missing values?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 893 Views

If we want to multiply two matrices if they contain missing values then we first need to convert the missing values to zeros and then the multiplication can be easily done. If we do not do so then the Output of the multiplication will have NAs at all positions.Check out the Examples given below to understand the correct of multiplication if NAs are present in the matrices.Example 1Following snippet creates a sample matrix −M1

Read More

How to hide outliers in base R boxplot?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 396 Views

To hide outliers in base R boxplot, we can use range argument inside boxplot function but we will have to play with range argument. The range argument can take many values therefore, we would need to find the correct one that removes all the outliers. To understand how it works check out the Example given below −ExampleTo hide outliners in base R boxplot, use the following snippet −x

Read More

How to remove line numbers from data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 2K+ Views

To remove line numbers from data.table object in R, we can set row.names to FALSE and print the data.table object.For Example, if we have a data.table object called DT then we can remove line numbers from DT by using the command given below −print(DT,row.names=FALSE)Example 1To load data.table object and create an object use the following snippet to create a data frame −library(data.table) x1

Read More

Find the value in an R data frame column that exist n times.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 222 Views

To find the value in an R data frame column that exist n times, we first need to tabulate the column with factor then extracting the levels of the column after that reading them with as.numeric.Check out the Examples given below to understand how it can be done.Example 1Following snippet creates a sample data frame −x

Read More

How to round matrix values in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 05-Nov-2021 4K+ Views

To round matrix values, we can use round function.For Example, if we have a matrix called M and we want to round the value in M to 2 decimal places by using the below command −M

Read More
Showing 1261–1270 of 1,740 articles
« Prev 1 125 126 127 128 129 174 Next »
Advertisements