Programming Articles - Page 894 of 3363

How to convert first letter into capital in single column data frame in R using dplyr?

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:33:59

722 Views

To convert first letter into capital in single column data frame in R, we can follow the below steps −First of all, create a data frame with string column.Then, use sub function along with mutate function of dplyr package to convert first letter into capital in string column.ExampleCreate the data frameLet’s create a data frame as shown below −Names

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

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:22:11

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

Create a ggplot2 graph without axes labels, axes titles and ticks in R.

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:15:31

203 Views

Sometimes we want to graphs that looks like graphs on a white paper having no axes labels, axes titles, and ticks, therefore, we can use theme_classic function of ggplot2 package.For Example, if we have a data frame called df that contains two columns say x and y then we can create the scatterplot between x and y using ggplot2 that looks like printed on white paper by using the below given command −ggplot(df,aes(x,y))+geom_point()+theme_classic(base_size=0)ExampleFollowing snippet creates a sample data frame −x

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

Nizamuddin Siddiqui
Updated on 09-Nov-2021 05:30:22

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

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

Nizamuddin Siddiqui
Updated on 08-Nov-2021 12:16:21

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

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

Nizamuddin Siddiqui
Updated on 08-Nov-2021 12:08:52

905 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

How to find the number of columns where all row values are equal in data.table object in R?

Nizamuddin Siddiqui
Updated on 08-Nov-2021 12:05:34

413 Views

To find the number of columns where all row values are equal in data.table object in R, we can follow the below steps −First of all, create a data.table object.Then, use sum function along with length and apply function to find the number of columns where all row values are equal.Example 1Create the data.table objectLet’s create a data.table object as shown below −library(data.table) x

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

Nizamuddin Siddiqui
Updated on 08-Nov-2021 12:01:35

145 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

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

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

380 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

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

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

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

Advertisements