Nizamuddin Siddiqui has Published 2303 Articles

How to change the order of bars in bar chart in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:21:14

1K+ Views

This can be done by setting the levels of the variable in the order we want.Example> data data ggplot(data, aes(x = Class)) + geom_bar()Setting the levels in increasing order> data

How to reorder the columns in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:19:01

274 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 ... Read More

How to find the index of an element in a vector in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:15:42

847 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 ... Read More

How to randomly select a fixed number of rows without replacement from a data frame in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:14:37

394 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 ... Read More

How to rename a single column in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:11:56

214 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.

How to add zeros before numbers in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:09:36

426 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

How to sum a variable by factor levels in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:08:10

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 ... Read More

How to extract columns of a data frame in R using dplyr package?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 14:06:29

348 Views

Example> x y z df library(dplyr) > df %>% select(y, z) y z 1 1.486720e-06 7.888609e-31 2 1.338302e-04 7.888609e-29 3 4.431848e-03 3.904861e-27 4 5.399097e-02 1.275588e-25 5 2.419707e-01 3.093301e-24 6 3.989423e-01 5.939138e-23 7 2.419707e-01 9.403635e-22 8 5.399097e-02 1.262774e-20 9 4.431848e-03 1.467975e-19 10 1.338302e-04 1.500596e-18 11 1.486720e-06 1.365543e-17 12 ... Read More

How to concatenate strings in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 13:00:44

357 Views

Concatenation can be done by using paste function in R.Example> paste("I", "Love", "R", sep=" ") [1] "I Love R"If we want to specify characters between words then it can be done by using collapse argument as follows −> x x [1] "I" "Love" "R" > paste(x, collapse="-") [1] "I-Love-R"

How to find mode(s) of a vector in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Jul-2020 12:58:40

382 Views

Just like mean and median there is no in-built function in R to find the mode.We can make use of the following user created function for this purpose> Modes

Advertisements