
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 2038 Articles for R Programming

1K+ Views
To check whether a string is a subset of another string we can use grepl function.Example> Company Job grepl(Job, Company, fixed = TRUE) [1] TRUEHere we are getting TRUE because Tutor is a subset of TutorialsPoint.> grepl(Company, Job, fixed = TRUE) [1] FALSEHere we are getting FALSE because TutorialsPoint is not a subset of Tutor.

443 Views
We can use options(scipen=999) to do this.Example> x t.test(x, mu=2000)One Sample t-testdata: x t = -14.212, df = 9, p-value = 1.801e-07 alternative hypothesis: true mean is not equal to 200095 percent confidence interval −151.3501 659.0499sample estimates −mean of x 405.2Here p-value is in scientific notation. Now we can deactivate it as follows −> options(scipen=999) > t.test(x, mu=2000)One Sample t-testdata: x t = -14.212, df = 9, p-value = 0.0000001801 alternative hypothesis: true mean is not equal to 200095 percent confidence interval −151.3501 659.0499sample estimates −mean of x 405.2If we want to activate scientific notation again then it be ... Read More

215 Views
Reordering of columns can be done by using square brackets.Example> df = data.frame(matrix(rnorm(20), nrow=5)) > df X1 X2 X3 X4 1 -0.3637644 2.0770246 0.48763128 -0.09019256 2 -3.1758515 2.3173075 0.86846761 0.38396459 3 1.1844641 0.3412267 1.90986295 -1.03493074 4 -0.5953466 1.7211738 -0.90686896 -0.71215313 5 -0.8732530 0.3256303 0.02312328 -0.36993899Let’s say we want to change the order of columns as X3, X2, X4, and X1 then it can be done as shown below −> df[,c(3,2,4,1)] X3 X2 X4 X1 1 0.48763128 2.0770246 -0.09019256 -0.3637644 2 0.86846761 2.3173075 0.38396459 -3.1758515 3 1.90986295 0.3412267 -1.03493074 1.1844641 4 -0.90686896 1.7211738 -0.71215313 -0.5953466 5 0.02312328 0.3256303 -0.36993899 -0.8732530

788 Views
There are three ways to find the index of an element in a vector.Example> x x [1] 8 10 9 6 2 1 4 7 5 3Using which> which(x == 6)[[1]] [1] 4Here we found the index of 6 in vector x.Using match> match(c(4,8),x) [1] 7 1Here we found the index of 4 and 8 in vector x.Using which with %in% > which(x %in% c(2,4)) [1] 5 7Here we found the index of 2 and 4 in vector x.

342 Views
This can be done simply by using sample function.Example> df = data.frame(matrix(rnorm(20), nrow=5)) > df X1 X2 X3 X4 1 -0.3277833 -0.1810403 0.2844406 -2.9676440 2 0.8262923 0.4334449 0.4031084 -1.9278049 3 -0.1769219 -0.1583660 -0.2829540 -0.1962654 4 1.0357773 0.9326049 0.3250011 -1.8835882 5 -1.0682642 -0.6589731 -0.4783144 -0.2945062Let’s say we want to select 3 rows randomly then it can be done as follows −> df[sample(nrow(df), 3), ] X1 X2 X3 X4 2 0.8262923 0.4334449 0.4031084 -1.9278049 1 -0.3277833 -0.1810403 0.2844406 -2.9676440 5 -1.0682642 -0.6589731 -0.4783144 -0.2945062

183 Views
We can do this by defining the newname as shown below −> Samp Samp sample.1.100..10. 1 47 2 63 3 57 4 16 5 53 6 7 7 54 8 2 9 13 10 14 > colnames(Samp) Samp Sampled Values 1 47 2 63 3 57 4 16 5 53 6 7 7 54 8 2 9 13 10 14 Since we only have one column in the data frame, so it is sufficient to use the object name.

373 Views
The easiest way to add zeros before numbers is by using paste0 functionExample> ID Gender Lens data data ID Gender Lens 1 25499 1 0.8 2 25500 2 1.2 3 25501 2 1.0 4 25502 1 2.0 5 25503 2 1.8 6 25504 1 1.4Let’s say we want to add 00 before every ID.It can be done by using paste0 function as follows −> IDs newdata newdata IDs Gender Lens 1 0025499 1 0.8 2 0025500 2 1.2 3 0025501 2 1.0 4 0025502 1 2.0 5 0025503 2 1.8 6 0025504 1 1.4

3K+ Views
We can do this by using aggregate function or with the help tapplyExample> x x Category Frequency 1 Graduation 12 2 Graduation 19 3 Post-Graduation 15 4 Graduation 20 5 PhD 25 6 Post-Graduation 13 7 PhD 14Using aggregate> aggregate(x$Frequency, by=list(Group=x$Category), FUN=sum) Group x 1 Graduation 51 2 PhD 39 3 Post-Graduation 28 Using tapply > tapply(x$Frequency, x$Category, FUN=sum) Graduation PhD Post-Graduation 51 39 28