Found 2038 Articles for R Programming

How to create a boxplot using ggplot2 for single variable without X-axis labels in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:18:03

3K+ Views

The important part of a boxplot is Y−axis because it helps to understand the variability in the data and hence, we can remove X−axis labels if we know the data description. To create a boxplot using ggplot2 for single variable without X−axis labels, we can use theme function and set the X−axis labels to blank as shown in the below example.Example Live DemoConsider the below data frame −y

How to create the random sample by defining the probabilities for each unit in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:16:52

102 Views

The random sample can be created by using sample function, this random sample gives equal chance for each unit to be selected in the sample, hence it is called simple random sample. If we want to have a sample where each unit has different chance of being selected in the sample then we need to use the argument prob as shown in the below examples.Example1 Live Demox1

How to remove dot and number at the end of the string in an R vector?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:15:02

5K+ Views

To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space. After that the vector will be passed as shown in the below examples.Example1 Live Demox1

How to create correlation matrix plot in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:14:45

283 Views

To create a correlation matrix plot, we can use ggpairs function of GGally package. For example, if we have a data frame called df that contains five columns then the correlation matrix plot can be created as ggpairs(df). A correlation matrix plot using ggpairs display correlation value as well as scatterplot and the distribution of variable on diagonal.Example Live DemoConsider the below data frame −set.seed(212) x

How to convert list elements into a single string in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:13:37

3K+ Views

To convert list elements into a single string, we can use paste function but firstly we would need to unlist the elements. Also, since we want to create a single string double quotes will be required at the end of the output. For example, if we have a list that contains 5 elements say 1, 2, 3, 4, 5 then conversion of these elements into a single string would be "12345".Example1 Live DemoList1

How to deal with the error “Error in int_abline---plot.new has not been called yet” in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:12:38

4K+ Views

The above error means plot is not being created yet hence abline function cannot be used to draw anything on the plot. Therefore, a plot needs to be created first to use abline function for creating a line or any other thing. Mostly, abline is used to create regression line on the plot, thus we need to create a scatterplot first before using abline.Example Live DemoConsider the below data frame −x

How to replace a complete column in an R data frame?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:09:42

306 Views

To replace a complete column in an R data frame, we can set the original one to new values by using delta operator. For example, if we have a data frame called df that contains a column x which 500 has values from normal distribution then to replace it with the normal distribution having a mean of 25 can be done as df$x

How to change the partial plot background using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 07:08:00

663 Views

To change the partial plot background, we can create a rectangle using geom_rect function by defining both the axes values and alpha for transparency, the color will be changed by using fill argument. The value of alpha will completely hide the grey background and we can play with its value based on our need.Example Live DemoConsider the below data frame −x

How to extract the closest value to a certain value in each category in an R data frame?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 10:47:38

1K+ Views

In Data Analysis, we often deal with the comparison of values and this comparison could be also done after finding the closest value to a certain value that might be threshold. For this purpose, we can use filter function of dplyr package along with abs and min function, the abs and min function are required to create the formula for finding the closest value.Consider the below data frame −Example Live DemoCategory

How to convert NaN to NA in an R data frame?

Nizamuddin Siddiqui
Updated on 10-Feb-2021 06:54:19

9K+ Views

To convert NaN to NA in an R data frame, we can set the NaN values to NA values by using single square brackets. Firstly, we would need to access the column that contains NaN values then NaN values will be accessed using is.nan then we can set those values to NA as shown in the below examples.Consider the below data frame −Example Live Demox1

Advertisements