Programming Articles - Page 1765 of 3363

How to select a column of a matrix by column name in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:21:42

3K+ Views

When we create a matrix in R, its column names are not defined but we can name them or might import a matrix that might have column names. If the column names are not defined then we simply use column numbers to extract the columns but if we have column names then we can select the column by name as well as its name.Example1M1

How to draw a violin plot in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:19:46

498 Views

A violin plot is similar to a boxplot but looks like a violin and shows the distribution of the data for different categories. It shows the density of the data values at different points. In R, we can draw a violin plot with the help of ggplot2 package as it has a function called geom_violin for this purpose.ExampleConsider the below data frame −set.seed(1) x

How to create dotted vertical lines in a plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:17:29

779 Views

In any plot, the vertical lines are generally used to show the thresholds for something, for example, range of the variable under consideration. The package ggplot2 provides geom_vline function to create vertical lines on a plot and we have linetype argument of this function which can be used to draw dotted vertical lines.ExampleConsider the below data frame −set.seed(9) x

What is the difference between order and rank function in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:14:28

1K+ Views

The rank function gives the rank of the values in a vector if the vector is sorted but in the same sequence as the original vector and the order function gives the position of the original value in the vector but in the sequence of the sorting in ascending order. The rank function is mostly used for ranking when we deal with ordinal variables, hence, we should use it whenever ranking of values is required, on the other hand, order is frequently used for ordering all numerical values.Examplesset.seed(100) x1

How to find the functions inside a package in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:13:45

4K+ Views

There are so many packages in R and each of these packages have different objectives, thus, the number of functions in these packages are large enough to solve the problems in analysis. A package might have fifteen functions and the other might have hundred, it totally depends on the necessity. We can find the functions inside a package by using lsf.str function but we need to load the package prior to knowing the functions inside.Example1library(BSDA) lsf.str("package:BSDA") CIsim : function (samples = 100, n = 30, mu = 0, sigma = 1, conf.level = 0.95, type = "Mean") Combinations : function ... Read More

How to extract a string that lies between two strings in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:12:30

602 Views

If we have a long string then we might want to extract a part of string that lies between two strings. For example, if we have a string “E-learning changing the education system in the world” and we want to extract the string “the education system” brave then we must be very careful about passing the strings in string function, you get to know this in examples. The extraction is not difficult with gsub function but we have to make sure that we are using the correct syntax, otherwise, the result will become obnoxious.Examplesx1

How to find the number of runs in a sequence in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:11:24

660 Views

Sometimes data is recorded as a sequence of numerical values or strings and we might to find the frequency for each of the sequences. This helps us to check the variation in the runs but we must make sure the total frequency is equal to the total number values, otherwise our calculation of frequency would be incorrect. To find the number of runs, we can use rle function in R that stands for Run Length Encoding.Examplesx1

How to add title at the top of multi-plots created by using gridExtra in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:10:08

8K+ Views

The gridExtra package works as an alternative of par(mfrow) with ggplot2, therefore, we can create multiple plots using ggplot2 and gridExtra on a single plot window. Now, if we want to give a title to all of the plots or we can say if want to give a main title to multi-plots, the top argument will be used to make the title lie on the top of the title. Similarly, we can use bottom, left, and right on the basis of our requirement but we would also need grid package for this purpose.ExampleConsider the below data frame −set.seed(123) x1Read More

How to get the US states name abbreviation in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:07:43

522 Views

There are fifty states in United States, few of them have short names but most of the states have a lengthy name. Therefore, if we are dealing with data that has states name of United States then it will be a little complicated to access the states by using their name, hence it is preferred to use abbreviation. We can get the state name abbreviation with the help of state.abb function.Examplesstate.abb[which(state.name=="New York")] [1] "NY" state.abb[which(state.name=="California")] [1] "CA" state.abb[which(state.name=="Texas")] [1] "TX" state.abb[which(state.name=="Florida")] [1] "FL" state.abb[which(state.name=="Washington")] [1] "WA" state.abb[which(state.name=="Michigan")] [1] "MI" state.abb[which(state.name=="New Jersey")] [1] "NJ" state.abb[which(state.name=="Arizona")] [1] "AZ" state.abb[which(state.name=="Pennsylvania")] [1] "PA" state.abb[which(state.name=="Alaska")] ... Read More

What are the restrictions on creating a vector in R?

Nizamuddin Siddiqui
Updated on 24-Aug-2020 12:04:54

198 Views

There are four main restrictions on creating a vector in R. We must remember these restrictions while creating any type of vector −A vector name cannot have % sign.A vector name cannot start with a number.A vector can start with a dot but it should not have a number after it.A vector cannot start with underscore.ExamplesVectors with % sign −x1%

Advertisements