r - Forcing mutate_each to create new column names for a subset of columns evaluate via ifelse -


problem

i'm working data frame similar extract generated below:

set.seed(1) df <- data.frame(columna1 = 1:10,                  columnb1 = 1:10,                  columnb99 = runif(n = 10)) 

i create set of columns contain custom flags corresponding values derived columns have 1 in column name.

approach

my present approach summarised below:

require(dplyr); require(magrittr) df %<>%     mutate_each(funs(ifelse(. == 1, "val1",                             ifelse(. == 10, "val10", na))),                 contains("1")) 

this generates required values, however, not create additional columns:

> head(df, n = 10)    columna1 columnb1  columnb99 1      val1     val1 0.26550866 2      <na>     <na> 0.37212390 3      <na>     <na> 0.57285336 4      <na>     <na> 0.90820779 5      <na>     <na> 0.20168193 6      <na>     <na> 0.89838968 7      <na>     <na> 0.94467527 8      <na>     <na> 0.66079779 9      <na>     <na> 0.62911404 10    val10    val10 0.06178627 

comments / attempt 1

i tried: df %<>%     mutate_each(funs(flg = ifelse(. == 1, "val1",                             ifelse(. == 10, "val10", na))),                 contains("1")) 

but generates same result. following this discussion, i'm guessing i'm making mistakes in providing suffix within funs.


comments follow-up

for example code:

df %<>%     mutate_each(funs(ifelse(. == 1, "val1", na),                      ifelse(. == 10, "val10", na)),                 contains("1")) head(df, 10) 

would create additional columns results not satisfactory:

> head(df, 10)    columna1 columnb1  columnb99 columna1_ifelse columnb1_ifelse columna1_ifelse_ifelse columnb1_ifelse_ifelse 1         1        1 0.26550866            <na>            <na>                     na                     na 2         2        2 0.37212390            <na>            <na>                     na                     na 3         3        3 0.57285336            <na>            <na>                     na                     na 4         4        4 0.90820779            <na>            <na>                     na                     na 5         5        5 0.20168193            <na>            <na>                     na                     na 6         6        6 0.89838968            <na>            <na>                     na                     na 7         7        7 0.94467527            <na>            <na>                     na                     na 8         8        8 0.66079779            <na>            <na>                     na                     na 9         9        9 0.62911404            <na>            <na>                     na                     na 10       10       10 0.06178627           val10           val10                     na                     na 

you can create additional columns when using single function in funs argument if supply named vector vars or ... argument within mutate_each. here's example using setnames:

mutate_each(df, funs(ifelse(. == 1, "val1",                             ifelse(. == 10, "val10", na))),                 setnames(contains("1"), c("x", "y"))) #   columna1 columnb1  columnb99     x     y #1         1        1 0.26550866  val1  val1 #2         2        2 0.37212390  <na>  <na> #3         3        3 0.57285336  <na>  <na> #4         4        4 0.90820779  <na>  <na> #5         5        5 0.20168193  <na>  <na> #6         6        6 0.89838968  <na>  <na> #7         7        7 0.94467527  <na>  <na> #8         8        8 0.66079779  <na>  <na> #9         9        9 0.62911404  <na>  <na> #10       10       10 0.06178627 val10 val10 

this described in another q&a.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -