R Programming Articles

Page 137 of 174

Find the row means for columns starting with a string in an R data frame.

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

To find the row means for columns starting with specific string in an R data frame, we can use mutate function of dplyr package along with rowMeans function.For Example, if we have a data frame called df that contains three columns say x1_x2, x1_x3, x1_x2 and we want to find the row means for columns x1_x2 and x1_x3 then, we can use the below command −df%%mutate(X1_Cmbn=select(.,starts_with("x1_")) %% rowMeans())Example 1Following snippet creates a sample data frame −Grp1_x

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 8K+ 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 636 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

Read More

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

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

Read More

How to replace 0 with NA in an R matrix?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 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]

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 544 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 603 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

Read More

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

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 1K+ 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

Read More

How to create transparent boxplot in R?

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

Be default, the boxplot created in base R or by using ggplot2 are not transparent in nature. If we want to create a transparent boxplot then we can use bwplot function from lattice package.For Example, if we have a vector called X then we can create transparent boxplot of X by using the below command −bwplot(x)Example 1To create transparent boxplot use the snippet given below −library(lattice) bwplot(rnorm(1000))OutputIf you execute the above given snippet, it generates the following Output −Example 2To create transparent boxplot add the following code to the above snippet −library(lattice) bwplot(rpois(1000, 5))OutputIf you execute all the above given ...

Read More

Find the column name with the largest value for each row in an R data frame.

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

To find the column name that has the largest value for each row in an R data frame, we can use colnames function along with apply function.For Example, if we have a data frame called df then we can find column name that has the largest value for each row by using the command as follows −df$Largest_Column

Read More
Showing 1361–1370 of 1,740 articles
« Prev 1 135 136 137 138 139 174 Next »
Advertisements