0

i have this data

cluster<-c(12,12,12,13,13,13,13,13)
names<-c("james","john", "jim","jeff","luke","sicklo","greaw","love")
data1<-data.frame(cluster,names)
data1

I want the following output after converting from long to wide frame with starting names as "name__0"

cluster<-c(12,13)
names__0<-c("james","jeff")
names__1<-c("john","luke")
names__2<-c("jim","siclo")
names__3<-c("NA","greaw")
names__4<-c("NA","love")
data2<-data.frame(cluster,names__0,names__1,names__2,names__3,names__4)
data2
markus
  • 25,843
  • 5
  • 39
  • 58
Sam Mwenda
  • 150
  • 8
  • One option without additional packages: `reshape(transform(data1, timevar = ave(cluster, cluster, FUN = seq_along)), idvar = "cluster", timevar = "timevar", direction = "wide", sep = "__") ` – markus Nov 17 '20 at 16:34
  • If you want the counter to start at 0, replace the `FUN` part by `FUN = function(x) seq_along(x) - 1` – markus Nov 17 '20 at 16:36
  • The main issue that I request you assist to solve, is to have the counter start at zero.If you could provide the full code, I would forever be thankful. – Sam Mwenda Nov 17 '20 at 16:40

0 Answers0