Server Side Programming Articles - Page 1658 of 2646

How to find the unique values in a column of an R data frame?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 08:48:12

11K+ Views

Categorical variables have multiple categories but if the data set is large and the categories are also large in numbers then it becomes a little difficult to recognize them. Therefore, we can extract unique values for categorical variables that will help us to easily recognize the categories of a categorical variable. We can do this by using unique for every column of an R data frame.ExampleConsider the below data frame −> x1 x2 x3 x4 df df    x1 x2  x3     x4 1  A  5 India     a 2  A  5 India     b 3  A ... Read More

How to extract unique combinations of two or more variables in an R data frame?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 08:41:56

10K+ Views

An R data frame can have a large number of categorical variables and these categorical form different combinations. For example, one value of a variable could be linked with two or more values of the other variable. Also, one categorical variable can have all unique categories. We can find this unique combination for as many variables as we want and it can be done with the help of unique function.ExampleConsider the below data frame −> x1 x2 x3 x4 df df x1 x2 x3 x4 1 1 A a 5 2 2 A b 5 3 3 A c 10 ... Read More

How to create a data frame with one or more columns as a list in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 08:37:40

154 Views

Creating a data frame with a column as a list is not difficult but we need to use I with the list so that the list elements do not work as an individual column. Here, you will find the common method to create a list which is incorrect if we want to insert that list in our data, also the correct method is mentioned at the end.The incorrect way −Example> x1 x2 df df      x1 c.1..1. c.2..2. c.3..3. c.4..4. c.5..5. c.6..6. c.7..7. c.8..8. c.9..9. 1     1    1       2       3 ... Read More

How to create a bar graph using ggplot2 without horizontal gridlines and Y-axes labels in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 08:12:38

441 Views

A bar graph plotted with ggplot function of ggplot2 shows horizontal and vertical gridlines. If we are interested only in the bar heights then we might prefer to remove the horizontal gridlines. In this way, we can have X-axis that helps us to look at the different categories we have in our variable of interest and get rid of the unnecessary information. This can be done by setting breaks argument to NULL in scale_y_discrete function.ExampleConsider the below data frame −> x y df library(ggplot2)Creating the plot with all gridlines −> ggplot(df, aes(x, y))+ + geom_bar(stat='identity')OutputCreating the plot without horizontal gridlines ... Read More

How to convert a data frame to data.table in R?

Nizamuddin Siddiqui
Updated on 11-Aug-2020 08:10:10

1K+ Views

Since operations with data.table are sometimes faster than the data frames, we might want to convert a data frame to a data.table object. The main difference between data frame and data.table is that data frame is available in the base R but to use data.table we have to install the package data.table. We can do this with the help setDT function in the data.table package.ExampleConsider the below data frame −> set.seed(1) > x1 x2 x3 x4 x5 df df x1 x2 x3 x4 x5 1  -0.1264538 1.7189774 2 6 9.959193 2   0.6836433  1.5821363 3 4 7.477968 3  -0.3356286 ... Read More

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

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

343 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

830 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

6K+ 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

2K+ 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

524 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

Advertisements