Found 2038 Articles for R Programming

How to check if two matrices are equal in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:58:00

2K+ Views

When we matrices of larger size and the data is expected to from the same distribution or from same sources then we might expect that the matrices are equal. In this type of situations, we would like to check whether the two matrices are equal or not. This can be done with the help of all.equal function as shown in the below examples.Example Live DemoM1

How to display 0 at Y-axis using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:48:33

851 Views

To display 0 at Y-axis, we can set the limits for Y-axis using scale_y_continuous function of ggplot2 package. For example, if we have two columns say x and y in an R data frame called df then the scatterplot with displaying 0 at Y-axis can be created by using the below commandggplot(df,aes(x,y))+geom_point()+scale_y_continuous(limits=c(0,”upperlimit”))Consider the below data frame −Example Live Demox

How to create boxplot using ggplot2 without whiskers in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:46:03

749 Views

To create boxplot using ggplot2 without whiskers, we need to use coef argument inside geom_boxplot function. For example, if we have data frame called df and there exists one categorical variable x and one response variable y then the boxplots for categories without whiskers can be created by using ggplot(df,aes(x,y))+geom_boxplot(coef=0).Consider the below data frame −Example Live Demox

How to create a sequence of values between two vectors in R using corresponding elements?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:42:38

387 Views

If we have two vectors say x and y, x contains 1, 6 and y contains 5,10 then the sequence of values between these two vectors will be 1, 2, 3, 4, 5 and 6, 7, 8, 9, 10. Here the sequence is created by using the corresponding elements in x and y. To do this in R, we can use mapply function as shown in the below examples.Example Live Demox1

How to find the first quartile for a data frame column in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:34:39

699 Views

The first quartile is the value that exists at the 25th percentile, that means there are 25% of values in the data that lie below first quartile. When we find the summary of data frame the output returns this value but if we want to extract only the first quartile then quantile function can be used by specifying the percentage using 0.25.Consider the below data frame −Example Live Demox

How to find the length of the largest string in an R data frame column?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:27:05

666 Views

The length of the largest string can be found with the help of max function combined with nchar function. For this purpose, we first need to access the appropriate column that contains string values. Suppose, we have a data frame called df that contains a string column defined as CHAR then the length of the largest string will be found by using the command max(nchar(df$CHAR)).Consider the below data frame −Example Live Demox

How to find the sum of values based on key in other column of an R data frame?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:23:34

545 Views

If we have a column that is key that means we want to use that column as an independent variable and find the statistical values such as sum, mean, standard deviation, range, etc. for the dependent variable. This can be done with the combination of with and tapply function as shown in the below examples.Consider the below data frame −Example Live Demox1

How to extract unique values in multiple columns in an R data frame using a single line code?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:20:16

2K+ Views

To extract unique values in multiple columns in an R data frame, we first need to create a vector of the column values but for that we would need to read the columns in matrix form. After that we can simply unique function for the extraction. To understand how it works check out the below examples.Consider the below data frame −Example Live Demox1

How to split comma separated values in an R vector?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:17:14

10K+ Views

The splitting of comma separated values in an R vector can be done by unlisting the elements of the vector then using strsplit function for splitting. For example, if we have a vector say x that contains comma separated values then the splitting of those values will be done by using the command unlist(strsplit(x,",")).Example Live Demox1

What is the difference between na.omit and na.rm in R?

Nizamuddin Siddiqui
Updated on 06-Feb-2021 08:14:36

2K+ Views

The na.omit performs any calculation by considering the NA values but do not include them in the calculation, on the other hand, na.rm remove the NA values and then perform any calculation. For example, if a vector has one NA and 5 values in total then their sum using na.omit will be calculated by excluding NA and by using na.rm it will be calculated by removing NA.Consider the below data frame −Example Live Demox1

Advertisements