Deal with Warning Message in R Histogram Using Bins

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

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

Save R Data Frame as TXT File

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

3K+ Views

If we want to use a data frame created in R in the future then it is better to save that data frame as txt file because it is obvious that data creation takes time. This can be done by using write.table function. For example, if we have a data frame df then we can save it as txt file by using the code write.table(df,"df.txt",sep="\t",row.names=FALSE)Consider the below data frame −Example Live Demoset.seed(111) x1

Create String Vector with Numbers at the End in R

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:15:08

759 Views

If we want to create a categorical vector with all unique values representing strings with numbers at the end then paste0 function can help us in the same. For example, if we want to create a vector for ID up to 10 as ID1, ID2, ID3, ID4, ID5, ID6, ID7, ID8, ID9, and ID10 then it can be done as paste0("ID",seq(1:10)).Example Live Demox1

Replace NA Values in R Data Frame with Column Mean

Nizamuddin Siddiqui
Updated on 18-Oct-2020 14:05:48

8K+ Views

In the whole world, the first step people teach to impute missing values is replacing them with the relevant mean. That means if we have a column which has some missing values then replace it with the mean of the remaining values. In R, we can do this by replacing the column with missing values using mean of that column and passing na.rm = TRUE argument along with the same.Consider the below data frame −Example Live Demoset.seed(121) x

Combine Year, Month, and Day Columns in R Data Frame

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

8K+ Views

Sometimes date variable is recorded in three different columns representing year, month, and day instead of a single column as date. Therefore, we need to combine these three columns and create a single column. This can be done by using paste function and define the values with as.Date.Consider the below data frame −Example Live DemoYear

Create Subset of a Matrix in R Using Row Names

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

703 Views

When we create a matrix in R, the row names and column names are not defined but we can define them separately. If we want to take a subset of rows of a matrix then row numbers can be used within single square brackets but if we want to do it with the names then we need to specify those names.Example Live DemoM1

Replicate a Vector to Create Matrix in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:44:04

2K+ Views

The matrix can be created by using matrix function in R and if we want to create a matrix by replicating a vector then we just need to focus on the replication. For example, if we have a vector V and we want to create matrix by replicating V two times then the matrix can be created as matrix(replicate(2,V),nrow=2).Example1 Live DemoV1

Create Random Sample with Values 0 and 1 in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:30:01

2K+ Views

It is known that the random sample can be created by using sample function in R. If we want to create a random sample with values 0 and 1 only then there are three different ways to pass them inside the sample function −Creating a vector of 0 and 1Using 0:1Using c(1,2) directly inside the function.Also, we can set replace argument to TRUE or FALSE based on our requirement.Examples Live Demox1

Align Bars of a Barplot with X-Axis using ggplot2 in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:16:21

2K+ Views

The bar plot is created with geom_bar function but there always exist some space between the bars and the X-axis labels. If we want to reduce that space or completely remove it we need to use scale_y_continuous function by defining expand argument for former and scale_y_continuous(expand=c(0,0)) for latter.Example Live DemoConsider the below data frame −set.seed(888) x

Multiply a Matrix with a Vector in R

Nizamuddin Siddiqui
Updated on 17-Oct-2020 14:13:41

6K+ Views

When we multiply a matrix with a vector the output is a vector. Suppose we have a matrix M and vector V then they can be multiplied as M%*%V. To understand the step-by-step multiplication, we can multiply each value in the vector with the row values in matrix and find out the sum of that multiplication.Example1 Live DemoM1

Advertisements