r - convert long df to wide df with duplicates/triplicates -


consider have df,

df <- data.frame(x=c(1:12), y=rep(0:2, 4), z=rep(letters[1:2], 6)) df$y <- as.factor(df$y) df <- arrange(df, y, z) df      x y z 1   1 0 2   7 0 3   4 0 b 4  10 0 b 5   5 1 6  11 1 7   2 1 b 8   8 1 b 9   3 2 10  9 2 11  6 2 b 12 12 2 b 

how can df_wide this:

z  0  1  2  1  5  3  7  11 9 b  4  2  6 b  10 8  12 

do.call(cbind, lapply(split(df, df$y), function(a)     setnames(object = data.frame(a$x,                                  row.names = paste0(as.character(a$z), 1:nrow(a))),              nm = a$y[1]))) #    0  1  2 #a1  1  5  3 #a2  7 11  9 #b3  4  2  6 #b4 10  8 12 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -