R Programming Articles

Page 119 of 174

How to extract the first n values of all elements of a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 6K+ Views

To extract the first n values of all elements of a list in R, we can follow the below steps −First of all, create a list.Then, use head function with sapply function to extract the first n values of all elements in the list.Example 1Create the listLet’s create a list as shown below −List1

Read More

Find the column name that contains value greater than a desired value in each row of an R data frame.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 09-Nov-2021 1K+ Views

To find the column name that contains value greater than a desired value in each row of an R data frame, we can use apply function along with lapply function.For Example, if we have a data frame called df and we want to extract column names for each row having values greater than 5 then we can use the command given below −lapply(apply(df,1, function(x) which(x5)),names)Example 1Following snippet creates a sample data frame −x1

Read More

How to extract the last value of all elements of a list in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 5K+ Views

To extract the last value of all elements of a list in R, we can follow the below stepss −First of all, create a list.Then, use tail function with sapply function to extract the last value of all elements in the list.Example 1Create the listLet’s create a list as shown below −List

Read More

How to repeat a whole data.table object in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 937 Views

To repeat a whole data.table object in R, we can follow the below steps −First of all, create a data.table object.Then, use rep function to repeat the data.table object.ExampleCreate the data.table objectLet’s create a data.table object as shown below −library(data.table) x

Read More

How to find the position of maximum of each numerical column if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 160 Views

To find the position of maximum of each numerical column 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 maximum of each numerical column if some columns are categorical.Example 1Create the data frameLet’s create a data frame as shown below −Level

Read More

How to find the cosine of each value in columns if some columns are categorical in R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 395 Views

To find the cosine of each value in columns 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 cosine of each value in columns if some columns are categorical.ExampleCreate the data frameLet’s create a data frame as shown below −Level

Read More

How to create a binary random variable in R with given probability?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 4K+ Views

To create a binary random variable in R with given probability, we can use the rbinom function with sample size argument n, success size argument size, and probability argument prob. To understand, how it can be done check out the below examples.Example 1Create the vector using rbinom function with n = 500, size = 1, and prob = 0.05 as shown below −x1

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 274 Views

To find the column median 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 median if some columns are categorical.Example 1Create the data frameLet’s create a data frame as shown below −Group

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 649 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

Read More

How to create a replacement column with multiple conditions and NA in R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 08-Nov-2021 575 Views

To create a replacement column with multiple conditions and NA in R data frame, we can follow the below steps −First of all, create a data frame.Then, use nested ifelse function to create a replacement column with multiple conditions.ExampleCreate the data frameLet’s create a data frame as shown below −x

Read More
Showing 1181–1190 of 1,740 articles
« Prev 1 117 118 119 120 121 174 Next »
Advertisements