R Programming Articles

Page 31 of 174

How to find the sum by two factor columns in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 2K+ Views

To find the sum by two factor columns, we can use aggregate function. This is mostly required when we have frequency/count data for two factors. For example, if we have a data frame called df that contains two factor columns say f1 and f2 and one numerical column say Count then the sum of Count by f1 and f2 can be calculated by using the command aggregate(Count~f1+f2,data=df,sum).ExampleConsider the below data frame −x1

Read More

How to divide each value in a data frame by column total in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To divide each value in a data frame by column total, we can use apply function and define the function for the division. For example, if we have a data frame called df that contains five columns then we can divide each value of these columns by column total using the command apply(df,2,function(x){x/sum(x)})ExampleConsider the below data frame −x1

Read More

How to find mean for x number of rows in a column in an R matrix?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 188 Views

To find the mean for x number of rows in a column, we can use colMeans function by accessing the column and providing the number of rows. For example, if we have a matrix called M that contains 20 rows and 5 columns then we can find the mean of column 5 for 5 number of rows can use the command colMeans(matrix(M[,5],nrow=5))ExampleConsider the below data frame −M1

Read More

How to separate string and a numeric value in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

To separate string and a numeric value, we can use strplit function and split the values by passing all type of characters and all the numeric values. For example, if we have a data frame called df that contains a character column Var having concatenated string and numerical values then we can split them using the below command −strsplit(df$Var,split="(?

Read More

How to delete matrix rows if a particular column value satisfies some condition in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 972 Views

To delete matrix rows if a particular column satisfies some condition, we can use subsetting with single square brackets and take the subset of the matrix based on the condition. For example, if we have a matrix M and want to delete rows if column first of M do not contain value 5 then we can use the command M[M[,1]==5,].ExampleConsider the below matrix −M1

Read More

How to find the correlation between corresponding columns of two matrices in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 907 Views

To find the correlation between corresponding columns of two matrices, we can use mapply function but we will have to read the matrices using as.data.frame function. For example, if we have two matrices called M_1 and M_2 and each of these matrices contains 5 columns then the correlation between corresponding columns of these matrices can be found by using the command mapply(cor,as.data.frame(M_1),as.data.frame(M_2))ExampleConsider the below matrices −M1

Read More

How to find the standard deviation for rows in an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 1K+ Views

To find the standard deviation for rows in an R data frame, we can use mutate function of dplyr package and rowSds function of matrixStats package. For example, if we have a data frame called df that contains two columns x and y then we can find the standard deviation for rows using the below command −df%>%mutate(STDEV=rowSds(as.matrix(.[c("x","y")])))ExampleConsider the below data frame −x1

Read More

How to replace space between two words with underscore in an R data frame column?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 3K+ Views

To replace space between two words with underscore in an R data frame column, we can use gsub function. For example, if we have a data frame called df that contains character column x having two words having a single space between them then we can replace that space using the command df$x

Read More

How to find the fractional power of a negative number in R?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 382 Views

To find the fractional power of a negative number, we can find the power separately for the numerator and denominator where denominator will have the numerator 1. For example, if we have a vector called x that contains a single value -10 then the fractional power 15/7 of x can be found by using the command ((x)^15)^(1/7)Examplex1

Read More

How to test for significant relationship between two categorical columns of an R data frame?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 11-Mar-2026 723 Views

To test for the significance of proportion between two categorical columns of an R data frame, we first need to find the contingency table using those columns and then apply the chi square test for independence using chisq.test. For example, if we have a data frame called df that contains two categorical columns say C1 and C2 then the test for significant relationship can be done by using the command chisq.test(table(df$C1,df$C2))Examplex1

Read More
Showing 301–310 of 1,740 articles
« Prev 1 29 30 31 32 33 174 Next »
Advertisements