Found 33676 Articles for Programming

How to create horizontal line for a range of values in a plot created by using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:22:49

625 Views

To display a particular part of independent variable in a plot, we might want to use a horizontal line. This will make the plot look different and get the attention of the viewer. To create a horizontal line in a plot, we can use geom_line function but we need to pass the values in a data frame format for which we want to create the horizontal line.Consider the below data frame −Example Live Demox

How to remove underscore from column names of an R data frame?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:20:06

3K+ Views

When we import data from outside sources then the header or column names might be imported with underscore separated values and this is also possible if the original data has the same format. Therefore, to make the headers shorter and look better we would prefer to remove the underscore sign and this can be easily done with the help of gsub function.Consider the below data frame −Example Live Demox_1

How to find the row-wise frequency of zeros in an R data frame?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:13:03

1K+ Views

In data analysis, we need to be very cautious about repeated values because they might be inputted purposely to create bias in the data and this value could be a zero as well. It happens in situations when we have missing data and the data collector replaces missing values with zeros which is a wrong practice. To find the row-wise frequency of zeros in an R data frame, we can use rowSums function for zero values by using the syntax −rowSums(“data_frame_name”==0)Consider the below data frame −Example Live Demoset.seed(189) x1

How to represent all values of X-axis or Y-axis on the graph in R using ggplot2 package?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:03:52

7K+ Views

If we have many unique elements or repeated in a column of an R data frame and create a graph using that column, either on X-axis or Y-axis then R automatically choses the axes labels, this might not display all the unique values of the column in the plot. Therefore, we can use scale_x_continuous function or scale_y_continuous function with labels depending on our requirement to display the column values.Consider the below data frame −Example Live Demox

How to find the name of the author of a package in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:01:28

440 Views

There can be multiple authors of a package in R and we might want to use their name if we are using their package in our publication for research, books, courses or any other type of content. Therefore, it is required to find out all the authors who contributed to a particular package and this can be done by using citation function with package name as shown in the below examples.Examplecitation("ggplot2")To cite ggplot2 in publications, please use −H. Wickham. ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York, 2016.A BibTeX entry for LaTeX users is@Book{,    author = {Hadley Wickham}, ... Read More

How to find the variance of row elements of a matrix in R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 14:56:49

1K+ Views

Finding the variance of columns is a common task in data analysis but often data is provided in wide format instead of long format, therefore, the cases are represented vertically and the variables are aligned horizontally and this data could be available in matrix or any other form. Therefore, the variance can be easily found by using apply function.Example Live DemoM1

How to create a predictive linear regression line for a range of independent variable in base R?

Nizamuddin Siddiqui
Updated on 16-Oct-2020 14:51:13

310 Views

If we want to create a regression line inside scatterplot then lines function can be used with the linear model function lm but if we want to do it for a particular range of independent variable then this range needs to be defined and passed within the lines function. Check out the below example of linear regression model that considers a range of independent variable for prediction.Consider the below vectors −Examplestrsplit(x6,"[*]")OutputDefining the range of x −Range_of_x

How to separate strings in R that are joined with special characters?

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

183 Views

When we deal with text data it is difficult to make it clean and one of the most of basic problem with this type of data is that the values are separated with some unique characters such as special characters. For this purpose, we can use strsplit function that makes it easy to do the separation among text values. Check out the examples below to understand how it can be done.Example Live Demox1

How to check whether a string is in lowercase or uppercase in R?

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

4K+ Views

We can use str_detect function to check whether a single string or a vector of strings is in lowercase or uppercase. Along with str_detect function, we need to use either upper or lower to check whether the string is in lowercase or uppercase and the output will be returned in TRUE or FALSE form, if the string will be in lowercase and we pass lower with str_detect function then the output will be TRUE and vice-versa.Example Live Demox1

How to find the rank of a vector elements in R from largest to smallest?

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

1K+ Views

To find the rank of a vector of elements we can use rank function directly but this will result in ranks from smallest to largest. For example, if we have a vector x that contains values 1, 2, 3 in this sequence then the rank function will return 1 2 3. But if we want to get ranks from largest to smallest then it would be 3 2 1 and it can be done in R as rank(-x).Example Live Demox1

Advertisements