Found 2038 Articles for R Programming

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

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:48:29

545 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))Example Live Demox1

How to replace a particular value in R data frame with a new value?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:43:11

943 Views

To replace a particular value in R data frame with a new value, we can use ifelse function where the new value will be placed after the condition and if the column values do not match the condition then the same column will be placed. For example, if we have a data frame called df that contains a column x having 20 values and some of them are 5 and if we want to replace 5 with 2 then we can use the command df$x

How to create boxplot in base R with higher width of the box lines?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:38:19

196 Views

To create boxplot in base R with higher width of the box lines, we can use the boxlwd argument inside boxplot function. For example, if we have a vector called x then we can create the boxplot with higher width of the box lines using the command −boxplot(x,boxlwd=5)Example Live Demox

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

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:35:04

230 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)Example Live Demox1

How to find the sum of every n values if missing values exists in the R data frame?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:32:42

571 Views

To find the sum of every n values in R data frame columns if there exist missing values, we can use rowsum function along with rep function that will repeat the sum for rows and na.rm=TRUE to exclude the rows with missing values. For example, if we have a data frame called df that contains 4 columns each containing twenty values with some missing values then we can find the row sums for every 5 rows by using the command rowsum(df,rep(1:5,each=4),na.rm=TRUE).Example Live Demox1

How to check if an R matrix column contains only duplicate values?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:25:41

137 Views

To check if an R matrix column contains only duplicate values, we can use dim function for the dimension of the column after accessing the matrix column with table function. For example, if we have a matrix called M having five columns then we can check whether first column contains only duplicate values using the command dim(table(M[,1]))==1ExampleConsider the below data frame − Live DemoM1

How to find the number of levels in R for a factor column?

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:21:09

694 Views

To find the number of levels in R for a factor column, we can use length function along with unique function. For example, if we have a data frame called df that contains a factor column X then we can find the number of levels in the factor column using the command −length(unique(df$X))ExampleConsider the below data frame − Live Demox1

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

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:17:38

2K+ 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

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

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:11:49

735 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 − Live Demox1

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

Nizamuddin Siddiqui
Updated on 17-Mar-2021 07:05:10

677 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 − Live DemoM1

Advertisements