Programming Articles - Page 875 of 3363

How to create a column of first non-zero value in each row of an R data frame?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 05:35:13

743 Views

To create a column of first non-zero value in each row of an R data frame, we can follow the below steps −First of all, create a data frame with some zero values.Then, use apply function and a custom function to find the first non-zero in each row of the data frame.ExampleCreate the data frameLet’s create a data frame as shown below −x

How to unsplit a split data.table object in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 05:33:26

313 Views

To unsplit a split data.table object in R, we can follow the below steps −First of all, create a data.table object.Then, use split function to split the data.table object.After that, use do.call function along with rbind function unsplit the data frame.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) Grp

How to standardize multiple columns not all in data.table object in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 05:27:52

298 Views

To standardize multiple columns not all in data.table object in R, we can follow the below steps −First of all, create a data.table object.Then, subset the columns with single square brackets and use lapply, list and scale function to standardize those columns.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) x

How to create a matrix with equal rows in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 08:26:43

805 Views

If we have a single row for a matrix then creation of a matrix with equal rows can be easily done with the help of rep function and if we do not have the row then we would need to pass the row value inside rep function.Check out the below examples to understand how to create a matrix with equal rows if one row is known.Example 1Consider the below vector −Row_1

How to impute missing values by random value for a single column in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 08:19:48

775 Views

To impute missing values by random value for a single column in R, we can use impute function from Hmisc package.For example, if we have a data frame called that contains a column say C which has some missing values then we can use the below given command to fill those missing values randomly −df$C

How to create scatterplot using data frame columns in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 08:17:33

3K+ Views

To create scatterplot using data frame columns, we need to convert the data frame columns into a variable and the value for each column will be read in a new column against each column name. This can be done with the help of melt function in reshape2 package.After that we can use ggplot function to create the scatterplot with new data frame as shown in the below example.ExampleFollowing snippet creates a sample data frame −x1

How to extract first n values from each element in an R list?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 08:14:08

924 Views

When we large number of values in each element of a list in R, we might want to have a look at some top values to understand the data characteristics. For this purpose, we can extract first n values from each element in an R list by using lapply function along with head function as shown in the below given examples.Example 1Following snippet creates a list −List1

How to multiply each element of a larger vector with a smaller vector in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 08:11:59

230 Views

To multiply each element of a larger vector with a smaller vector, we can perform outer product calculation with the help of %o% operator.For example, if we have two vectors say x and y where x is of shorter length than y then we can multiply each element of y with each element of x by using the command given below −x%o%yCheck out the below examples to understand how it works.Example 1To multiply each element of a larger vector with a smaller vector, use the code given below −x1

How to extract unique rows by categorical column of a data.table object in R?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:59:00

386 Views

If we have categorical data in a data.table object and some values are duplicate then we might want to extract unique rows from that object.To extract unique rows by categorical column of a data.table object, we can use unique function and define the columns with by argument as shown in the below examples. To understand how the extraction is done check out the below examples.Example 1Following snippet creates a data.table object −library(data.table) grp

How to extract diagonal elements of a matrix in R without using diag function?

Nizamuddin Siddiqui
Updated on 11-Nov-2021 07:55:52

2K+ Views

The diagonal elements of a matrix occur at the position where column and row indices are same hence, we can make use of those indices to extract diagonal elements of a matrix if we do not want to use diag function.For example, if we have a matrix called M then diagonal elements of M can be extracted by using the command given below − M[row(M)==col(M)]Check out the below examples to understand how it works.Example 1Following snippet creates a matrix −M1

Advertisements