Nizamuddin Siddiqui has Published 2307 Articles

How to convert a matrix column into list in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 05:04:43

3K+ Views

To convert a matrix column into list can be done by using the apply function. We will have to read the columns of the matrix as list by using as.list function. For example, if we have a matrix called M then the columns in M can be converted into list ... Read More

How to extract vector using different index for columns in an R matrix?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 05:03:52

170 Views

Suppose we have a matrix and a vector containing indices of equal size as the matrix then we can extract the vector from matrix using the index vector. For this purpose, we can use cbind function as shown in the below examples.Example1Live Demo> M1 M1Output      [, 1] [, ... Read More

How to aggregate matrix columns by row names in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 05:02:39

908 Views

To aggregate matrix columns by row names, we can use colSums with sapply and transpose the output. For example, if we have a matrix called M then the aggregate matrix columns by row names can be done using t(sapply(by(M, rownames(M), colSums), identity)).Example1Consider the below matrix −Live Demo> M1 rownames(M1) M1Output  ... Read More

How to find the location of a string in an R data frame?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 04:58:51

1K+ Views

To find the location of a numerical value in an R data frame we use which function and if the value is string then the same function will be used but we need to pass the value appropriately. For example, if we have a data frame called df that contains ... Read More

How to find the correlation for data frame having numeric and non-numeric columns in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 04:58:20

2K+ Views

To find the correlation for data frame having numeric and non-numeric columns, we can use cor function with sapply and use complete.obs for pearson method. For example, if we have a data frame called then we can use the below command to find the correlation coefficient −cor(df[, sapply(df, is.numeric)], use="complete.obs", ... Read More

How to check if a matrix in R is in binary form?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 04:57:24

193 Views

A binary matrix contains values in form of twos such as 0/1, 1/2, Yes/No etc. If we have a matrix that has some values and we expect that there are only two values in the whole matrix then we can check whether only those two values exist in the matrix ... Read More

How to find the index of n number of maximums in each row of a matrix in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 04:56:32

76 Views

If a matrix has multiple columns and values in each row are different then there will be number of maximums equal to the number of columns. Suppose, we want to extract the index of two maximums in each row in a matrix called M then we can use the below ... Read More

How to create a bar plot with bars for missing values in R?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 06-Mar-2021 04:55:24

774 Views

To create a bar plot in R, we can use barplot function but if there exist some missing values in the data then we can use ggplot2 package. For example, if we have a data frame having two vectors say x and y, x containing categorical values with NA as ... Read More

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

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 05-Mar-2021 07:25:18

203 Views

In data science, we often face the problem of missing values and we need to define a way to replace them with an appropriate value or we can complete remove them. If we want to replace the missing then we also need to know how many missing values are there. ... Read More

How to standardize only numerical columns in an R data frame if categorical columns also exist?

Nizamuddin Siddiqui

Nizamuddin Siddiqui

Updated on 05-Mar-2021 07:25:01

464 Views

The standardization of a numerical column can be easily done with the help of scale function but if we want to standardize multiple columns of a data frame if categorical columns also exist then mutate_if function of dplyr package will be used. For example, if we have a data frame ... Read More

Advertisements