Reputation: 7752
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.
Upvotes: 2
Views: 1068
Reputation: 887108
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
Upvotes: 3