How to create an empty matrix in R?

The default for matrix is to have 1 column. To explicitly have 0 columns, you need to write matrix(, nrow = 15, ncol = 0) A better way would be to preallocate the entire matrix and then fill it in mat <- matrix(, nrow = 15, ncol = n.columns) for(column in 1:n.columns){ mat[, column] <- … Read more

Removing NA in dplyr pipe [duplicate]

I don’t think desc takes an na.rm argument… I’m actually surprised it doesn’t throw an error when you give it one. If you just want to remove NAs, use na.omit (base) or tidyr::drop_na: outcome.df %>% na.omit() %>% group_by(Hospital, State) %>% arrange(desc(HeartAttackDeath)) %>% head() library(tidyr) outcome.df %>% drop_na() %>% group_by(Hospital, State) %>% arrange(desc(HeartAttackDeath)) %>% head() If … Read more

Subset of rows containing NA (missing) values in a chosen column of a data frame

Never use ==’NA’ to test for missing values. Use is.na() instead. This should do it: new_DF <- DF[rowSums(is.na(DF)) > 0,] or in case you want to check a particular column, you can also use new_DF <- DF[is.na(DF$Var),] In case you have NA character values, first run Df[Df==’NA’] <- NA to replace them with missing values.