How can I simplify or perform the following operations using dplyr
:
Run a function on all
data.frame
names, likemutate_each(funs())
for values, e.g.names(iris) <- make.names(names(iris))
Delete columns that do NOT exist (i.e. delete nothing), e.g.
iris %>% select(-matches("Width")) # ok iris %>% select(-matches("X")) # returns empty data.frame, why?
Add a new column by name (string), e.g.
iris %>% mutate_("newcol" = 0) # ok x <- "newcol" iris %>% mutate_(x = 0) # adds a column with name "x" instead of "newcol"
Rename a data.frame colname that does not exist
names(iris)[names(iris)=="X"] <- "Y" iris %>% rename(sl=Sepal.Length) # ok iris %>% rename(Y=X) # error, instead of no change
iris %>% mutate_( 'x' = 0)
– IRTFMx <- "Sepal.Length"; iris %>% rename_(.dots = setNames(x,"sl"))
works but that can not be used for (4) because a missing colnames throws an error – ckluss