2

I'm struggeling to achieve the following (although I already read this vignette):

require(dplyr)
require(lazyeval)

# data looks like
df <- data.frame(col1 = c("entry1", "entry2"), col2 = c("0x12", "0xA1"))
#     col1 col2
# 1 entry1 0x12
# 2 entry2 0xA1

# must be something like this:
dots <- list(df %>%
  select(col2) %>%
  distinct(col2))

df %>%
  rowwise() %>% 
  mutate_(.dots = dots)
# target output
#     col1 col2 0x12 0xA1
# 1 entry1 0x12    1  N/A
# 2 entry2 0xA1  N/A    1

So I want to generate a new column which is named after the cell entry and set this to one. This is unlike all other examples I've found so far, where the input columns are dynamically selected (e.g. here) or they're not doing it with dataframe (but a data.table). If the N/As are a 0 it won't do any harm and save me a postprocessing step.

Community
  • 1
  • 1
Boern
  • 7,233
  • 5
  • 55
  • 86
  • 2
    I guess this can be done using `spread` i.e. `library(tidyr);df%>% mutate(ind=1, col3=col2) %>% spread(col3, ind)` – akrun Sep 10 '15 at 12:25
  • Your guess was right - thanks so much ! Do you want to post that as answer so you get some points for your efforts? – Boern Sep 10 '15 at 12:43

1 Answers1

3

This could be easily solved by reshaping into wide format with spread from library(tidyr). We create a column of 1 ('ind') and a duplicate of 'col2' ('col3') and then use spread to get the expected output.

library(tidyr)
df%>% 
  mutate(ind=1, col3=col2) %>% 
  spread(col3, ind)
#     col1 col2 0x12 0xA1
#1 entry1 0x12    1   NA
#2 entry2 0xA1   NA    1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 2
    just in case somebody wonders how to reverse that: use `gather(data, key, value, ..., na.rm = FALSE, convert = FALSE)` – Boern Sep 16 '15 at 11:14