Problem
I'm working with a data frame similar to the extract generated below:
set.seed(1)
df <- data.frame(columnA1 = 1:10,
columnB1 = 1:10,
columnB99 = runif(n = 10))
I would like to create a set of columns that would contain custom flags corresponding to the values derived from columns that have 1 in the column name.
Approach
My present approach is summarised below:
require(dplyr); require(magrittr)
df %<>%
mutate_each(funs(ifelse(. == 1, "val1",
ifelse(. == 10, "val10", NA))),
contains("1"))
this generates the required values, however, does 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 also tried:
df %<>%
mutate_each(funs(flg = ifelse(. == 1, "val1",
ifelse(. == 10, "val10", NA))),
contains("1"))
but it generates the same result. Following this discussion, I'm guessing that I'm making mistakes in providing the suffix within the funs
.
Comments Follow-up
For example the code:
df %<>%
mutate_each(funs(ifelse(. == 1, "val1", NA),
ifelse(. == 10, "val10", NA)),
contains("1"))
head(df, 10)
would create the additional columns but the results are not fully 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