Found 2038 Articles for R Programming

How to change the axes labels using plot function in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 08:03:29

220 Views

In a plot, the axes labels help us to understand the range of the variables for which the plot is created. While creating a plot in R using plot function, the axes labels are automatically chosen but we can change them. To do this, firstly we have to remove the axes then add each of the axes with the labels we want and then create the box for the plot.ExampleConsider the below data −> x y plot(x, y)OutputChanging the axes labels for X and Y axes −> plot(x, y, axes=FALSE)+ + axis(side = 1, at = c(2, 5, 10))+ + ... Read More

How to get row index or column index based on their names in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 08:00:43

560 Views

We might prefer to use row index or column index during the analysis instead of using their numbers, therefore, we can get them with the help of grep function. While dealing with a large data set it becomes helpful because large data sets have large number of rows and columns so it is easier to recall them with their indexes instead of numbers. Specifically, column indexes are needed, on the other hand, rows are required in special cases only such as analysing a particular case.ExampleConsider the below data frame −> set.seed(1) > x1 x2 x3 x4 x5 df head(df, 20) ... Read More

How to change the Y-axis values in a bar plot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 07:49:09

5K+ Views

Bar plot is frequently used to analyze the number of times a level of factor variable occurs in a data set and the Y-axis values are crucial to the bar plot. Sometimes these values are not in the form we want, therefore, we want to replace them with the new ones. This can be done with the help of breaks argument of scale_y_continuous function in ggplot2.ExampleConsider the below data frame −> set.seed(1) > x df library(ggplot2)Creating the plot without specifying the Y-axis values −> ggplot(df, aes(x))+ + geom_bar()OutputPlotting with new Y-axis values −> ggplot(df, aes(x))+ + geom_bar()+ + scale_y_continuous(breaks=c(0, 2, ... Read More

How to extract initial, last, or middle characters from a string in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 07:46:59

1K+ Views

In Text analysis, we might want to extract characters from a single string or from a vector of strings. This extraction might be required to create a new string with some specific words required for further analysis. We can do this with the help of str_sub function of stringr package.ExampleConsider the below string −> x1 library(stringr) > str_sub(x1, 1, 8) [1] "Removing" > str_sub(x1, 1, 23) [1] "Removing harmful things" > str_sub(x1, 29, 37) [1] " the road" > str_sub(x1, 30, 37) [1] "the road" > str_sub(x1, -58, -51) [1] "Removing" > str_sub(x1, -58, -1) [1] "Removing harmful things from ... Read More

How to count the number of rows for a combination of categorical variables in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 07:36:38

367 Views

When we have two categorical variables then each of them is likely to have different number of rows for the other variable. This helps us to understand the combinatorial values of those two categorical variables. We can find such type of rows using count function of dplyr package.ExampleConsider the CO2 data in base R −> head(CO2, 20) > head(CO2, 20)       Plant    Type    Treatment    conc     uptake 1     Qn1     Quebec   nonchilled     95      16.0 2     Qn1     Quebec   nonchilled    175   ... Read More

How to randomize an already created vector in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 07:17:01

536 Views

Some vectors are randomly created and some are not randomly created in R but we can do randomization for both of these types of vectors. Randomization ensures unbiasedness therefore it is necessary especially when the vector is created with an objective that tends to change the result of the analysis. The randomization in R can be simply done with the help of sample function.Randomization of vectors that are not randomly created −> x1 x1 [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ... Read More

How to create a vector with repeated values in R?

Nizamuddin Siddiqui
Updated on 14-Sep-2023 22:00:00

26K+ Views

There are two methods to create a vector with repeated values in R but both of them have different approaches, first one is by repeating each element of the vector and the second repeats the elements by a specified number of times. Both of these methods use rep function to create the vectors.ExampleConsider the below examples −> x1 x1 [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 [39] 4 ... Read More

How to replace NA’s to a value of selected columns in an R data frame?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 06:49:34

500 Views

In data analysis, finding some NA values in a data frame is very common but all the NA values do not create problems if the column that contain NA values is not useful for the analysis. We can replace all NA values to 0 or to any other for the columns that are useful.ExampleConsider the below data frame −> set.seed(99) > x1 x2 x3 x4 x5 df df   x1   x2   x3   x4    x5 1  NA   NA   25    NA 2  5     2   24    f    2 3  NA   ... Read More

How to count the number of words in a string in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 06:29:10

417 Views

The number of words in a sentence could be used for text analysis, therefore, we are required to count them. This can be for a single sentence or for multiple sentences. We can find the number of words in a sentence or in multiple sentences using strsplit with sapply.ExampleConsider the below sentences read as vectors −> x1 x1 [1] "Data Science is actually the Statistical analysis" > sapply(strsplit(x1, " "), length) [1] 7 > x2 x2 [1] "China faced trouble even after controlling COVID-19" > sapply(strsplit(x2, " "), length) [1] 7 > x3 x3 [1] "Corona virus has changed everything ... Read More

How to change plot area margins using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 06:25:51

1K+ Views

While creating plots using ggplot2, the plot area is of square shape but we can change our plot area by setting plot.margin in theme function. This is helpful when we want to decrease the plot area and also when the data points are less.ExampleConsider the below data frame −> set.seed(1) > x y df library(ggplot2)Creating the scatterplot without changing the plot area margins −> ggplot(df,aes(x,y))+ + geom_point()> ggplot(df,aes(x,y))+ + geom_point()+ + theme(plot.margin = unit(c(1,1,1,1), "cm"))> ggplot(df,aes(x,y))+ + geom_point()+ + theme(plot.margin = unit(c(2,2,2,2), "cm"))

Advertisements