Found 33676 Articles for Programming

How to find the range for 95% of all values in an R vector?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:21:52

1K+ Views

The range for 95% of all values actually represents the middle 95% values. Therefore, we can find the 2.5th percentile and 97.5th percentile so that the range for middle 95% can be obtained. For this purpose, we can use quantile function in R. To find the 2.5th percentile, we would need to use the probability = 0.025 and for the 97.5th percentile we can use probability = 0.0975.Example Live Demox1

How to convert NA’s in sequence to a single NA in an R vector?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:19:31

123 Views

Sometimes values are missing in a sequence and R program records them as NA (Not Available). In this type of situation, we might want to replace consecutive NA records with single NA value. This can be done by using is.na along with diff function as shown in the below examples.Example Live Demox1

How to create side by side histograms in base R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:17:24

2K+ Views

To create side by side histograms in base R, we first need to create a histogram using hist function by defining a larger limit of X-axis with xlim argument. After that we can create another histogram that has the larger mean and smaller standard deviation so that the bars do not clash with each other and add=T argument must also be added inside the second hist function.Example Live Demohist(rnorm(5000,mean=5,sd=2.1),col="green",xlim=c(1,20))OutputExamplehist(rnorm(5000,mean=15,sd=1.25),col="red",add=T)Output

How to identify duplicate values in a column of matrix in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:15:50

448 Views

We can easily identify duplicate values in a matrix by using duplicated function but it does not specify that the first occurrence is also duplicated. Therefore, we need to use it with OR sign | and the argument fromLast = TRUE of duplicated function so that the first occurrence of the duplicated values will be also identified as duplicate.Example Live DemoM1

How to compare two columns in an R data frame for an exact match?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:12:05

7K+ Views

Sometimes analysis requires the user to check if values in two columns of an R data frame are exactly the same or not, this is helpful to analyze very large data frames if we suspect the comparative values in two columns. This can be easily done with the help of ifelse function.ExampleConsider the below data frame − Live Demox1

How to create a scatterplot with regression line using ggplot2 with 0 intercept and slope equals to 1 in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:08:44

317 Views

To create a regression line with 0 intercept and slope equals to 1 using ggplot2, we can use geom_abline function but we need to pass the appropriate limits for the x axis and y axis values. For example, if we have two columns x and y in a data frame df and both have ranges starting from -1 to 1 then the scatterplot with regression line with 0 intercept and slope equals to 1 can be created as −ggplot(df,aes(x,y))+geom_point()+geom_abline()+lims(x=c(-1,1),y=c(-1,1))ExampleConsider the below data frame − Live Demox

How to convert a time series object to a vector in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:07:11

2K+ Views

To convert a time series object into a vector, we just need to read that object with as.numeric and store it in some other object or in the same object. For example if we have a time series object x then it can be converted to a vector by using x

How to convert multiple columns in an R data frame into a single numerical column along with a column having column names as factor?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 06:04:55

1K+ Views

When we receive data from any source, it is highly likely that it would not be a perfect data set for the intended analysis, therefore, we need to perform some cleaning or mining based on the characteristics of the data. For example, if we have a column name of a data frame as factor levels of a numerical variable then we might want to convert that data frame in such a way that numerical values are stored in a single column and the column names are stored in another column that will represent a factor so that we can apply ... Read More

How to perform one-way anova with unequal sample sizes in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:59:43

1K+ Views

To perform the one-way anova with sample sizes having different sizes we can use aov function. Suppose we have a categorical column defined as Group with four categories and a continuous variable Response both stored in a data frame called df then the one-way anova can be performed as −aov(Response~Group,data=df)ExampleConsider the below data frame − Live DemoGroup

How to generate standard normal random numbers in R?

Nizamuddin Siddiqui
Updated on 07-Dec-2020 05:54:52

4K+ Views

A standard normal distribution is the type of distribution that has mean equals to zero with standard deviation 1. If we want to generate standard normal random numbers then rnorm function of R can be used but need to pass the mean = 0 and standard deviation = 1 inside this function.Example Live Demornorm(10, 0, 1)Output[1] 0.6936607 -0.7967657 -2.7544428 0.2688767 0.5278463 -1.5387568 [7] 1.1716632 -1.5033895 0.8112929 -1.0101065Example Live Demornorm(50, 0, 1)Output[1] 2.58246666 -0.53083341 -0.57343343 1.08172756 1.30341849 -0.07440422 [7] -0.41869305 -0.96227706 -0.46899119 1.55428279 0.09162738 -0.96027221 [13] -0.84735327 -1.74949782 0.58541758 0.23117630 0.47402479 -0.72453853 [19] 0.07171564 1.13088794 0.18735157 0.25091758 -1.34728315 -0.39768159 [25] -0.38109955 -0.34019286 -1.51778561 ... Read More

Advertisements