Found 33676 Articles for Programming

How to convert row index number or row index name of an R data frame to a vector?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 13:05:34

499 Views

We might want to extract row index irrespective of its type (whether numeric or string) to do some calculations if it is incorrectly set as a row index. It happens during the data collection process or incorrect processing of data. Also, since row indexes are helpful to access row we must have proper names to them instead of values that might makes confusion. For example, if a data frame has row indexes as 43, 94, etc. then it might be confusing. Therefore, we should convert row indexes to a vector or a column if required.ExampleConsider the below data frame (Here, ... Read More

How to create a scatterplot in R using ggplot2 with transparency of points?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 13:00:03

311 Views

A scatterplot is used to observe the relationship between two continuous variables. If the sample size is large then the points on the plot lie on each other and does not look appealing. Also, the interpretation of such type of scatterplots is not an easy task, therefore, we can increase the transparency of points on the plot to make it more appealing. We can do this by using alpha argument in geom_point of ggplot2.ExampleConsider the below data frame −> set.seed(123) > x y df library(ggplot2) > ggplot(df, aes(x, y))+geom_point()Output> ggplot(df, aes(x, y))+geom_point(alpha=0.10)Output> ggplot(df, aes(x, y))+geom_point(alpha=0.05)OutputRead More

How to find the mean of each variable using dplyr by factor variable with ignoring the NA values in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:56:27

409 Views

If there are NA’s in our data set for multiple values of numerical variables with the grouping variable then using na.rm = FALSE needs to be performed multiple times to find the mean or any other statistic for each of the variables with the mean function. But we can do it with summarise_all function of dplyr package that will result in the mean of all numerical variables in just two lines of code.ExampleLoading dplyr package −> library(dplyr)Consider the ToothGrowth data set in base R −> str(ToothGrowth) 'data.frame': 60 obs. of 3 variables: $ len : num 4.2 11.5 7.3 5.8 ... Read More

How to join two data frames with the same row order using dplyr in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:50:47

195 Views

When we have one common column in two data frames then joining of those data frames might be used to create a bigger data frame. This will help us to analyze a combined data set with many characteristics. We can do this by using inner_join function of dplyr package.ExampleConsider the below data frames −> set.seed(111) > x1 R1 df1 df1 x1 R1  1 1  78  2 2  84  3 3  83  4 4  47  5 5  25  6 1  59  7 2  69  8 3  35  9 4  72 10 5  26 11 1  49 12 2  45 13 3  74 14 4   8 15 5 100 16 1  96 17 2  24 18 3  48 19 4  95 20 5   7 > x1 R2 df2 df2 x1 R2 1 1 21 2 2 15 3 1 1 4 2 9 5 1 63 6 2 40 7 1 25 8 2 35 9 1 71 10 2 52Loading dplyr package −> library(dplyr)Merging two data frames −> inner_join(df2,df1) Joining, by = "x1" x1 R2 R1 1 1 21 78 2 1 21 59 3 1 21 49 4 1 21 96 5 2 15 84 6 2 15 69 7 2 15 45 8 2 15 24 9 1 1 78 10 1 1 59 11 1 1 49 12 1 1 96 13 2 9 84 14 2 9 69 15 2 9 45 16 2 9 24 17 1 63 78 18 1 63 59 19 1 63 49 20 1 63 96 21 2 40 84 22 2 40 69 23 2 40 45 24 2 40 24 25 1 25 78 26 1 25 59 27 1 25 49 28 1 25 96 29 2 35 84 30 2 35 69 31 2 35 45 32 2 35 24 33 1 71 78 34 1 71 59 35 1 71 49 36 1 71 96 37 2 52 84 38 2 52 69 39 2 52 45 40 2 52 24

How to convert multiple numerical variables to factor variable in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:45:17

1K+ Views

Sometimes the data type for a variable is not correct and it is very common that a factor variable is read as a numeric variable, especially in cases where factor levels are represented by numbers. If we do not change the data type of a factor variable then the result of the analysis will be incorrect. Therefore, if a factor variable has a different data type than factor then it must be converted to factor data type. To convert multiple variables to factor type, we can create a vector that will have the name of all factor variables then using ... Read More

How to create an empty matrix in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:39:17

5K+ Views

An empty matrix can be created in the same way as we create a regular matrix in R but we will not provide any value inside the matrix function. The number of rows and columns can be different and we don’t need to use byrow or bycol argument while creating an empty matrix because it is not useful since all the values are missing. In R, one column is created by default for a matrix, therefore, to create a matrix without a column we can use ncol =0.Example> M1 M1      [, 1]  [1, ] NA  [2, ] NA ... Read More

How to plot means inside boxplot using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:30:27

479 Views

When we create a boxplot, it shows the minimum value, maximum value, first quartile, median, and the third quartile but we might want to plot means as well so that the comparison between factor levels can be made on the basis of means also. To create this type of plot, we first need to find the group-wise means then it can be used with geom_text function of ggplot2.ExampleConsider the CO2 data in base R −> head(CO2, 20) Plant Type Treatment conc uptake 1 Qn1 Quebec nonchilled 95 16.0 2 Qn1 Quebec nonchilled 175 30.4 3 Qn1 Quebec nonchilled 250 34.8 ... Read More

How to create a scatterplot in R with legend position inside the plot area using ggplot2?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:27:10

309 Views

Legends help us to differentiate the values of the response variable while creating the scatterplot. In this way, we can understand how one level of a factor variable affects the response variable. The legend is preferred to be positioned at left bottom, top right, top left, and bottom right. We can use theme function to position the legends.ExampleConsider the below data frame −> set.seed(99) > x1 x2 F df library(ggplot2)Creating the plot with different legend positions −Consider the below data frame −> ggplot(df, aes(x=x1, y=x2, colour=F)) + geom_point(aes(colour=F)) + + theme(legend.justification = c(1, 0), legend.position = c(1, 0))Output> ggplot(df, aes(x=x1, ... Read More

How to create a subset for a factor level in an R data frame?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:24:52

4K+ Views

In data analysis, we often deal with factor variables and these factor variables have different levels. Sometimes, we want to create subset of the data frame in R for specific factor levels to analyze the data only for that particular level of the factor variable. This can be simply done by using subset function.ExampleConsider the below data frame −> set.seed(99) > Factor Percentage df df   Factor Percentage 1   India 48 2   China 33 3     USA 44 4      UK 22 5  Canada 62 6   India 32 7   China 13 8     ... Read More

How to convert a vector into matrix in R?

Nizamuddin Siddiqui
Updated on 12-Aug-2020 12:20:02

11K+ Views

To convert a vector into matrix, just need to use matrix function. We can also define the number of rows and columns, if required but if the number of values in the vector are not a multiple of the number of rows or columns then R will throw an error as it is not possible to create a matrix for that vector.Here, we will read vectors by their names to make it easy but you can change their names if you want. There are four vectors of different lengths that are shown in these examples −Examples > Vector1 Vector1 [1] ... Read More

Advertisements