Nizamuddin Siddiqui has Published 1958 Articles

How to split a vector into smaller vectors of consecutive values in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 10-Nov-2021 13:09:00

845 Views

To split a vector into smaller vectors of consecutive values, we can use split function with cumsum function.For example, if we have a vector called X that contains some consecutive values then we split X into smaller vectors of consecutive values by using the command given below −split(x, cumsum(c(1, diff(x)!=1)))Example ... Read More

How to find the mean based on single group value in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 10-Nov-2021 13:07:53

1K+ Views

To find the mean based on single group value in an R data frame, we can use mean function with subsetting through single square brackets.For example, if we have a data frame called df that contains a categorical column say C having three groups Low, Medium, High and a numerical ... Read More

How to fill a data.table row with missing values in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 10-Nov-2021 13:01:30

555 Views

Instead of filling missing values, we sometimes need to replace the data with missing values. This might be required in situations when missing values are coded with a number or the actual values are not useful or sensible for the data study. Also, we might want to replace the values ... Read More

How to subset matrix row values based on different columns?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

157 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 ... Read More

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

232 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 ... Read More

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

4K+ 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 ... Read More

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

566 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 ... Read More

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

268 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 ... Read More

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

Nizamuddin Siddiqui

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 ... Read More

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

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

610 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) x1Read More

Advertisements