Found 2038 Articles for R Programming

How to create a dotted vertical line using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 10-Nov-2021 12:54:26

7K+ Views

To create a vertical line using ggplot2, we can use geom_vline function of ggplot2 package and if we want to have a dotted vertical line then linetype will be set to 3 inside the same function. To draw the line, we will have to provide xintercept because the line will start on X-axis.Check out the below example to understand how it works.ExampleFollowing snippet creates a sample data frame −x

How to subset matrix row values based on different columns?

Nizamuddin Siddiqui
Updated on 10-Nov-2021 08:27:30

79 Views

Suppose we have a matrix called M that contains three columns and we want to subset row values of M but from different columns that means the subset will not have values only from a single column.Hence, before creating the subset we first need to find the column numbers that can be used for the creation of subset and this can be done with the help of sample function if column numbers are not known. After that we can use cbind function with single square brackets for subsetting.Check out the Examples given below to understand how it can be done.Example ... Read More

How to find the row products for each row in an R matrix?

Nizamuddin Siddiqui
Updated on 10-Nov-2021 08:16:49

110 Views

To find the row products for each row in an R matrix, we can use rowProds function of matrixStats package.For Example, if we have a matrix called MATRIX then we can find the row products for each row in MATRIX by using the command given below −rowProds(MATRIX)Example 1Following snippet creates a sample matrix −M1

How to create pie chart in base R with labels?

Nizamuddin Siddiqui
Updated on 10-Nov-2021 08:07:16

136 Views

A pie chart is a circular representation of data which is created for either nominal data or ordinal data. The slices in the pie chart depends on the magnitude of the data values. If we want to create a pie chart in base R with then pie function can be used along with labels argument.Check out the Examples given below to understand how it can be done.ExampleTo create a pie chart in base R with labels, use the following command −x

Convert a numeric column to binary factor based on a condition in R data frame

Nizamuddin Siddiqui
Updated on 10-Nov-2021 08:00:27

3K+ Views

To convert a numeric column to binary factor based on a condition in R data frame, we can use factor function along with ifelse function.For Example, if we have a data frame called df that contains a numerical column say Num and we want to convert it to a binary factor if Num is less than 100 then it will be Minor otherwise Major then we can use the below given command −df$Num_Factor

How to standardize data.table object column by group in R?

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:51:51

388 Views

To standardize data.table object column by group, we can use scale function and provide the grouping column with by function.For Example, if we have a data.table object called DT that contains two columns say G and Num where G is a grouping column and Num is a numerical column then we can standardize Num by column G by using the below given command −DT[,"Num":=as.vector(scale(Num)),by=G]Example 1Consider the below data.table object −library(data.table) Grp

Replace each value in a column with the largest value based on a condition in R data frame.

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:45:51

132 Views

Suppose we have three columns say X, Y, and Z in an R data frame called df and we want to replace values in columns X and Y with the same value if the values are greater than values in Z and if they are less than the values in Z then we can replace with Z values.Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −x1

How to randomly replace values in an R data frame column?

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:41:44

2K+ Views

The random replacement values in an R data frame column can be done with the help of sample function along with nrow function and single square subsetting.For Example, if we have a data frame called df that contains a columns say X and we want to randomly replace 5 values in X with 1.5 then we can use the below given command −df$X[sample(nrow(df),5)]

How to multiply corresponding values from two data.table objects in R?

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:33:11

387 Views

To multiply corresponding values from two data.table objects in R, we can follow the below steps −First of all, create two data.table objects.Then, use mapply function to multiply corresponding values from those two data.table objects.ExampleCreate the first data.table objectLet’s create a data.table object as shown below −library(data.table) x1

How to find the percentage of zeros in each column of a matrix in R?

Nizamuddin Siddiqui
Updated on 10-Nov-2021 07:24:15

97 Views

To find the percentage of zeros in each column of a matrix in R, we can follow the below steps −First of all, create a matrix.Then, use colSums function along with nrow function to find the percentage of zeros in each column.Example 1Create the matrixLet’s create a matrix as shown below −M1

Advertisements