Remove Row Names or Column Names from a Matrix in R

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:50:58

23K+ Views

To remove the row names or column names from a matrix, we just need to set them to NULL, in this way all the names will be nullified. For example, if we have a matrix M that contain row names and column names then we can remove those names by using the command colnames(M)

Replicate a Matrix by Rows in R

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

3K+ Views

The replication of matrix by rows means that repeating a matrix one or more times but row-wise. For example, if we have a matrix that contains only one row and three columns then the replication of that matrix three times will repeat that one row three times. This can be done by using rep function along with matrix function as shown in the below example.Example Live DemoM

Extract Website Name from Links in R

Nizamuddin Siddiqui
Updated on 16-Oct-2020 15:25:38

234 Views

If we have a list of website links and we want to extract the website name from those links then it is a time-consuming task because we would need to copy each name one-by-one. Therefore, it is better to extract them using a function in R and save time. To extract the website name from the website link, we can use suffix_extract function of urltools package. This will extract the host, subdomain, domain and suffix. And it is known that the domain values are the website names.Loading urltools package −library(urltools)Website links stored in a vector −Web_LinksRead More

Create Horizontal Line for a Range of Values in 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

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

Find Row-wise Frequency of Zeros in 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

Represent All Values of X-Axis or Y-Axis on Graph in R Using ggplot2

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

Find the Name of the Author of a Package in R

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

441 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

Find Variance of Row Elements in a Matrix using 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

Create Predictive Linear Regression Line 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

Advertisements