As a result of scraping a web page, I wound up with a data frame where the variables were in one column, and their accompanying values are in another. A simple version looks like this:
df <- data.frame(is_employed = c("Hobbies", "Has Previous Experience"), false = c("squash", "false"))
In order to get it in the form I wanted, I first converted to it to a matrix and then back to a data frame, like this:
mat <- as.matrix(df)
mat <- rbind(colnames(mat), mat)
colnames(mat) <- c("variable", "value")
df2 <- as.data.frame(mat)
I can live with that, although I was wondering if there is a better method. Anyways, the problem occurs when I want to spread() the data. If I just call spread() on df2, it doesn't produce the desired result. Instead, I have to add a dummy column and then delete it at the end, like so:
library("deplyr")
library("tidyr")
df3 <- df2 %>%
mutate(n = 1) %>%
spread(variable, value)%>%
select(-n)
Again, I can live with it, but I was wondering whether I missed something. Is it possible to call spread() on a two-column data frame?