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
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
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
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
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
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 − Live DemoM1
To filter rows by excluding a particular value in columns of the data frame, we can use filter_all function of dplyr package along with all_vars argument that will select all the rows except the one that includes the passed value with negation. For example, if we have a data frame called df and we want to filter rows by excluding value 2 then we can use the commanddf%>%filter_all(all_vars(.!=2))ExampleConsider the below data frame − Live Demox1
By default, R considers the vector or the data frame column values. But if we want to set the range for boxplot in base R, we can use ylim argument within the boxplot function. For example, if we have a vector called x that contains values starting from 21 to 50 and we want to have the range in the boxplot starting from 1 to 100 then we can use the commandboxplot(x,ylim=c(1,100))Example Live Demox
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="(?
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 − Live DemoM1