Found 2038 Articles for R Programming

How to find the column standard deviation if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Updated on 08-Nov-2021 11:38:52

106 Views

To find the column standard deviation if some columns are categorical in R data frame, we can follow the below steps −First of all, create a data frame.Then, use numcolwise function from plyr package to find the column standard deviation if some columns are categorical.Example 1Create the data frameLet’s create a data frame as shown below −Group

How to remove dollar sign in R data frame?

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

944 Views

To remove dollar sign in R data frame, we can follow the below steps −First of all, create a data frame.Then, use gsub function along with lapply function to remove dollar sign.ExampleCreate the data frameLet’s create a data frame as shown below −Product

Convert a single column matrix into a diagonal matrix in R.

Nizamuddin Siddiqui
Updated on 08-Nov-2021 11:44:42

460 Views

A diagonal matrix is a type of square matrix that contains zero at non-diagonal elements starting from left-upper to right-bottom.To convert a single column matrix into a diagonal matrix in R, we can use diag function along with matrix function and use ncol argument where we can put the number of columns equal to the number of values in the single column matrix.Check out the Examples given below to understand how it can be done.Example 1Following snippet creates a sample matrix −M1

How to find the number of changes when tossing a coin in R?

Nizamuddin Siddiqui
Updated on 08-Nov-2021 11:27:29

58 Views

To find the number of changes when tossing a coin in R, we can follow the below steps −First of all, create a vector using rbinom function.Then, use rle function to find the table of changes.After that, use length in the output of rle.Example 1Create the vectorLet’s create a vector as shown below −x1

How to find the column means if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Updated on 08-Nov-2021 11:21:39

89 Views

To find the column means if some columns are categorical in R data frame, we can follow the below steps −First of all, create a data frame.Then, use numcolwise function from plyr package to find the column means ifsome columns are categorical.Example 1Create the data frameLet’s create a data frame as shown below −Grp

How to create a column of log(1+x) in data frames stored in R list?

Nizamuddin Siddiqui
Updated on 08-Nov-2021 11:13:15

192 Views

To create a column of log(1+x) in data frames stored in R list, we can follow the below steps −First of all, create a list of data frames.Then, use lapply function to create a column of log(1+x) in data frames stored in the list.ExampleCreate the list of data framesUsing data.frame function to create data frames and list function to create the list of those data frames −df1

How to convert a vector into a diagonal matrix in R?

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

3K+ Views

A diagonal matrix is a type of square matrix that contains zero at non-diagonal elements starting from left-upper to right-bottom. To convert a vector into a diagonal matrix in R, we can use diag function along with matrix function and use ncol argument where we can put the number of columns equal to the number of values in the vector. Check out the Examples given below to understand how it can be done.Example 1Following snippet a sample list −V1

How to multiply vector values in sequence with matrix columns in R?

Nizamuddin Siddiqui
Updated on 08-Nov-2021 11:04:12

777 Views

To multiply vector values in sequence with matrix columns in R, we can follow the below steps −First of all, create a matrix.Then, create a vector.After that, use t function for transpose and multiplication sign * to multiply vector values in sequence with matrix columns.ExampleCreate the data frameLet’s create a data frame as shown below −M

Convert list with varying number of elements into a data frame.

Nizamuddin Siddiqui
Updated on 08-Nov-2021 11:18:23

467 Views

To convert list with varying number of elements into a data frame in R, we can use stri_list2matrix function of stringi package along with as.data.frame function.For Example, if we have a list called LIST that contains varying number of elements then we can convert it into a data frame by using the below mentioned command −as.data.frame(t(stri_list2matrix(LIST)))Example 1Following snippet creates a sample list −List1

How to combine two rows in R matrix by addition?

Nizamuddin Siddiqui
Updated on 08-Nov-2021 10:59:26

749 Views

To combine two rows in R matrix by addition, we can follow the below steps −First of all, create a matrix.Then, using plus sign (+) to add two rows and store the addition in one of the rows.After that, remove the row that is not required by subsetting with single square brackets.ExampleCreate the matrixLet’s create a matrix as shown below −M

Advertisements