Found 2038 Articles for R Programming

Find the column and row names in the R data frame based on condition.

Nizamuddin Siddiqui
Updated on 01-Nov-2021 08:28:55

7K+ Views

To find the column names and row names in an R data frame based on a condition, we can use row.names and colnames function. The condition for which we want to find the row names and column names can be defined inside these functions as shown in the below Examples.Example 1Following snippet creates a sample data frame −x1

How to find the groupwise order of values in an R data frame?

Nizamuddin Siddiqui
Updated on 01-Nov-2021 08:22:21

433 Views

To find the groupwise order of values in an R data frame, we can use mutate function of dplyr package along with rank function and grouping will be done with the help of group_by function.For Example, if we have a data frame called df that contains two columns say Group and DV then we can find the groupwise order of DV values by using the command given below −df%%group_by(Group)%%mutate(Order=rank(DV))Example 1Following snippet creates a sample data frame −Group

How to create correlation matrix plot without variables labels in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 04:06:03

366 Views

To create correlation matrix plot without variables labels in R, we can use tl.pos argument set to n.For Example, if we have a correlation matrix say M then we can create the correlation matrix plot without variables labels by using the below command −corrplot(M,tl.pos='n')ExampleFollowing snippet creates a sample data frame −x

Extract columns with a string in column name of an R data frame.

Nizamuddin Siddiqui
Updated on 01-Nov-2021 07:58:40

4K+ Views

To extract columns with a particular string in column name of an R data frame, we can use grepl function for column names and then subset the data frame with single square brackets.For Example, if we have a data frame called df and we want to extract columns that has X in their names then we can use the command mentioned below −df[grepl("X",colnames(df))]Example 1Following snippet creates a sample data frame −Students_Score

How to replace 0 with NA in an R matrix?

Nizamuddin Siddiqui
Updated on 01-Nov-2021 07:39:43

3K+ Views

To replace 0 with NA in an R matrix, we can use subsetting with single square brackets and then set zeros to NA.For Example, if we have a matrix called M that contains some zeros then we can replace 0 with NA by using the command mentioned below −M[M==0]

How to create bar plot with gradient colors using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 04:01:29

4K+ Views

To create bar plot with gradient colors using ggplot2, we can use scale_fill_gradient function where we can set the lower and higher color values.For Example, if we have a data frame called df that contains two columns say Cat and Count then we can create the bar plot with gradient colors by using the below command −ggplot(df,aes(Cat,Count,fill=Cat))+geom_bar(stat="identity")+scale_fill_gradient(low="blue",high="red")ExampleFollowing snippet creates a sample data frame −x

Create ggplot2 graph with darker axes labels, lines and titles in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 04:00:20

548 Views

To create a ggplot2 graph with darker axes labels, darker lines, and dark titles, we can use theme_classic function of ggplot2 package with base_size argument set to a larger value.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 with darker axes labels, darker lines, and dark titles by using the below command −ggplot(df,aes(x,y))+geom_point()+theme_classic(base_size=22)ExampleFollowing snippet creates a sample data frame −x

Find the column number with largest value for each row in an R matrix.

Nizamuddin Siddiqui
Updated on 01-Nov-2021 07:11:25

374 Views

To check which column has the largest value for each row in an R matrix, we can use apply function.For Example, if we have a matrix called M then we can find column that has the largest value for each row by using the command given below −apply(M,1,which.max)Example 1Consider the matrix given below −M1

Find the column index of least value for each row of an R matrix

Nizamuddin Siddiqui
Updated on 01-Nov-2021 07:00:30

345 Views

To find the column index of least value for each row in an R matrix, we can use apply function.For Example, if we have a matrix called M then we can find column that has the least value for each row by using the command as follows −apply(M,1,which.min)Example 1Consider the matrix given below −M1

How to convert days in a week to number in R data frame column?

Nizamuddin Siddiqui
Updated on 01-Nov-2021 06:43:51

925 Views

To convert days in a week to number in R data frame column, we would need to convert the column into a factor by defining the weekdays as the levels and then read that column as integer.If we provide the correct sequence of weekdays during conversion then Monday will be converted to 1. Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −Week_Days

Advertisements