[R] Splitting data frame into columns with dplyr

Giorgio Garziano giorgio.garziano at ericsson.com
Thu Oct 1 15:42:51 CEST 2015


library(dplyr)
df <- data.frame(z = rep(c("A", "B")), x = 1:6, y = 7:12) %>%
arrange(z)

temp <- reshape(df, v.names = c("x", "y"), idvar = c("x", "y"), timevar = "z", direction = "wide")
lA <- na.omit(temp[,c("x.A", "y.A")])
lB <- na.omit(temp[,c("x.B", "y.B")])
df.long <- as.data.frame(cbind(lA,lB))
colnames(df.long) <- c("A.x", "A.y", "B.x", "B.y")
df.long

  A.x A.y B.x B.y
1   1   7   2   8
2   3   9   4  10
3   5  11   6  12


Reference:

http://blog.wildintellect.com/blog/reshape


--
GG




	[[alternative HTML version deleted]]



More information about the R-help mailing list