Found 2038 Articles for R Programming

How to perform chi square test for goodness of fit in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:50:53

3K+ Views

The chi square test for goodness of fit is a nonparametric test to test whether the observed values that falls into two or more categories follows a particular distribution of not. We can say that it compares the observed proportions with the expected chances. In R, we can perform this test by using chisq.test function. Check out the below examples to understand how it is done.Example1Live Demo> x1 x1Output[1] 9 4 1 9 6 6 1 6 0 0 5 8 8 3 7 8 0 3 3 9 6 0 3 8 2 0 8 5 9 1 3 ... Read More

How to replace a sub-string with the reverse of that sub-string in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:48:52

75 Views

The chartr function in base R helps us to replace old strings with new strings and hence it can be also used to replace a subs-string with the reverse of that substring. For example, if we have a vector say x that contains tutorialpsoint and we want to convert it to tutorialspoint then it can be done as chartr("tutorialpsoint ", " tutorialspoint ", x).Example1Live Demo> x1 x1Output[1] "IDNIA"Example> chartr("DN", "ND", x1)Output[1] "INDIA" Example2Live Demo> x2 x2Output[1] "IDNIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNONESIA" [7] "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" "IDNONESIA" [13] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" [19] "IDNONESIA" "IDNONESIA" "IDNIA" ... Read More

How to create a vector of lists in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:46:58

368 Views

If we have many lists but we want to use the values in the lists as a vector then we first need to combine those lists and create a vector. This can be done by using unlist function along with the combine function c to create the vector. For example, if we have two lists defined as List1 and List2 and we want to create a vector V using these lists then it can be created as:V x1 x1Output$a [1] -0.6972237 -1.5013768 -0.2451809 -0.2365569 -1.6304919 -1.1704378 [7] 1.1617054 -0.2349498 -1.2582229 0.4112065 $b [1] 2 0 2 6 0 0 ... Read More

How to create a histogram with main title in the outer margin of plot window in base R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:43:53

2K+ Views

The main title of a histogram in base R can be inserted by using title function and if we want to have it in the outer margin then outer argument must be set to TRUE. If the outer argument does not fulfil our requirement then we can use par function to adjust the outer margin area and create the histogram. Check out the below example to understand how it works.Example> x hist(x) > title('Normal Distribution',outer=TRUE)OutputExample> par(oma=c(0,0,2,0)) > hist(x) > title('Normal Distribution',outer=TRUE)Output

How to find the group-wise median in an R data.table object?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:42:17

612 Views

When the assumptions of parametric analysis are not satisfied then we move on to non-parametric analysis and non-parametric analysis often deals with the calculation of median because the data is not normally distributed. If we want to find the group-wise median and the data is stored in a data.table object then lapply function can be used as shown in the below examples.ExampleLoading data.table package:> library(data.table)Consider the below data.table object:Example> Group x1 x2 x3 x4 dt1 dt1OutputGroup x1 x2 x3 x4 1: B 0.515370827 6.174187 542.9350 50.28300 2: B 0.522858146 6.976872 510.5568 49.71331 3: A 1.055456751 3.192242 476.7693 48.88280 4: A ... Read More

How to convert a matrix into a matrix with single column in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:40:53

944 Views

If we have a matrix then we might want to convert it to matrix with single column for some analytical purpose such as multiplying with a vector that has the length equal to the total number of elements as in the matrix. Thus, the matrix can be converted to a single column matrix by using matrix function itself but for this we would need to nullify the column names and row names.Example1Live Demo> M1 M1Output [, 1] [, 2] [, ... Read More

How to create smooth density curves without filling densities in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:36:48

340 Views

The density curves can be created by using stat_density function of ggplot2 package but it fills the curve with density hence it becomes difficult to recognize the curves. We can remove these densities by using geom="line" inside the stat_density function so that only the density curves will be plotted.ExampleConsider the below data frame:Live Demo> G Response df dfOutputG Response 1 C 1.0229016 2 C 1.0058160 3 B 0.8831558 4 B 0.7729167 5 C 0.9130468 6 D 0.8431893 7 B 1.5003581 8 A 0.9687335 9 B 1.1139661 10 A 0.9211660 11 A 1.1790619 12 D 0.6349671 13 A 1.2616918 14 A ... Read More

How to check if some specific columns of an R data frame are equal to a column or not?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:35:11

298 Views

If we have a large amount of data in a data frame and we suspect that some of the data columns are repeated or some of them are equal to a particular column then we can use sapply function in base R to figure it out. In this way, we can remove duplicated columns that does not suppose to help in our data analysis objective.Example1Consider the below data frame:Live Demo> set.seed(354) > x1 x2 x3 x4 x5 df1 df1Outputx1 x2 x3 x4 x5 1 4 5 4 4 6 2 6 4 8 7 5 3 5 6 4 7 ... Read More

How to convert a date or date vector to POSIXct in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:33:09

4K+ Views

To convert a date or date vector to POSIXct, we can use as.POSIXct function but we also need to pass the appropriate date format inside the function. For example, if we have a date "2020-11-14" then it can be converted to POSIXct by using as.POSIXct("2020-11-14", format="%Y-%m-%d").Example1Live Demo> date1 as.POSIXct(date1, format="%Y-%m-%d")Output[1] "2020-04-01 IST"Example2Live Demo> date2 date2Output[1] "2020-02-12" "2020-06-01" "2020-04-01" "2020-05-01" "2020-01-21" [6] "2020-01-21" "2020-06-01" "2020-04-27" "2020-05-11" "2020-06-01" [11] "2020-01-21" "2020-03-31" "2020-05-01" "2020-02-12" "2020-01-21" [16] "2020-05-01" "2020-03-31" "2020-04-01" "2020-05-01" "2020-01-21" [21] "2020-05-01" "2020-04-11" "2020-05-11" "2020-04-01" "2020-03-31" [26] "2020-04-11" "2020-04-01" "2020-03-31" "2020-04-01" "2020-04-11" [31] "2020-05-11" "2020-06-01" "2020-03-31" "2020-04-27" "2020-01-21" [36] "2020-01-21" "2020-04-01" "2020-06-01" ... Read More

How to create a horizontal bar plot using barplot function in R?

Nizamuddin Siddiqui
Updated on 21-Nov-2020 05:31:01

181 Views

To create a bar plot in base R, we can directly use barplot function but the table of frequencies should be passed inside this function. If we want to create the barplot in horizontal manner then horiz=TRUE argument must be added. For example, if we have a vector x that contains repeating values then the horizontal bar plot of x can be created by using barplot(table(x),horiz=TRUE).Example1> x barplot(table(x),horiz=TRUE)OutputExample2> y barplot(table(y),horiz=TRUE)OutputExample3> z barplot(table(z),horiz=TRUE)Output

Advertisements