Found 2038 Articles for R Programming

How to find the position of one or more values in a vector into another vector that contains same values in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:33:10

590 Views

Finding the position of one of more values that are common in two vectors can be easily done with the help of match function. The match function will match the values in first and second vector then return the index or position of these common values in second vector.Example Live Demoset.seed(145) x1

How to plot values with log scales on x and y axis or on a single axis in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:30:33

3K+ Views

We can plot numerical values in R with many scales and that includes log scale as well. Also, it is possible to plot the values with log scales on both the axes. In base R, the best way to do this is defining the axes values with decimal representation as shown in the below examples with well-defined log.Consider the below vector −Example Live Demoset.seed(555) x

How to set a level of a factor column in an R data frame to NA?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:28:27

637 Views

In data analysis, we often face inappropriate data and hence the data analysis becomes difficult. An example of inappropriate data is reading missing values with a different value by naming them as Missing or Not Available. It can be done by using below syntax −Syntaxlevels(“data_frame_name”$”Column_name”)[levels(“data_frame_name”$”Column_name”=="Missing"]

How to create a subset based on levels of a character column in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:22:03

1K+ Views

In R programming, mostly the columns with string values can be either represented by character data type or factor data type. For example, if we have a column Group with four unique values as A, B, C, and D then it can be of character or factor with four levels. If we want to take the subset of these columns then subset function can be used. Check out the example below.Consider the below data frame −Exampleset.seed(888) Grp

How to check whether a data frame exists or not in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:17:31

2K+ Views

Sometimes we keep writing codes in the programming console and suddenly we need to use something that was used in the upper side of programming console then recalling it becomes a little ambiguous if we forget about it. In this case, we might want to check whether something exists or not and that something could be a data frame in R programming. For this purpose, we can use the below syntax −Syntaxexists("data_frame_name")&&is.data.frame(get("data_frame_name "))Consider the below data frame −Example Live Demoset.seed(101) x1

How to find the rank of a matrix in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:14:24

7K+ Views

The rank of a matrix is defined as the maximum number of linearly independent vectors in rows or columns. If we have a matrix with dimensions R x C, having R number of rows and C number of columns, and if R is less than C then the rank of the matrix would be R. To find the rank of a matrix in R, we can use rankMatrix function in Matrix package.Loading Matrix package −library(Matrix)Example Live DemoM1

How to find the sum of corresponding elements in multiple vectors even if they contain NA’s in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:05:29

228 Views

If we have multiple vectors then the sum of the corresponding elements can be found by using rowSums functions and the vectors can be combined by using cbind so that R can easily read the corresponding elements. But if there are NA values in one or more vectors then we also need to add na.rm=TRUE argument.Example Live Demoset.seed(100) x1

How to renumber rows if they are unordered in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:03:32

372 Views

When we create a sample using inbuilt or imported data set then the numbering for selected rows as in the original data set, therefore, the numbering becomes unordered. To change this unordered numbering to a sequence say starting from one to the total number of rows in the sample, we can use 1:nrow(“sample_object_name”).Consider the below data frame −Example Live Demoset.seed(999) x

How to create two plots using ggplot2 arranged in a vertical manner in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:54:43

495 Views

The two plots created by using ggplot2 can be arranged in a vertical manner with the help gridExtra package, we simply needs to use grid.arrange function to do so. For example, if we have two plots created by using ggplot2 and saved in objects p1 and p2 then they can be vertically arranged as grid.arrange(p1,p2)Consider the below data frame −Exampleset.seed(151) x

How to create barplot from data frame in R using rows as categories?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 14:51:45

6K+ Views

If we have small number of rows then we might want to create bar plot for rows instead of using columns as categories. This can be done by using barplot function but we need to convert the data frame to matrix and take the transpose of it. For example, if we have a data frame data_frame with 4 rows and 4 columns, then the barplot with rows as categories can be created as barplot(t(as.matrix(data_frame)),beside=TRUE)Consider the below data frame −Example Live Demox1

Advertisements