Hi! Maybe this would do the trick:
--- snip --- library(reshape2) # Use 'reshape2' library(dplyr) # Use 'dplyr' datatransfer<-data %>% mutate(letter2=letter) %>% dcast(id+letter~letter2, value.var="weight") --- snip --- Or did I misunderstood something? Best, Kimmo 2019-01-06, 13:16 +0000, Andras Farkas via R-help wrote: > Hello Everyone, > > would you be able to assist with some expertise on how to get the > following done in a way that can be applied to a data set with > different dimensions and without all the line items here? > > we have: > > id<-c(1,1,1,2,2,2,2,3,3,4,4,4,4,5,5,5,5)#length of unique IDs may > differ of course in real data set, usually in magnitude of 10000 > letter<- > c(sample(c("A","B","C","D","E"),3),sample(c("A","B","C","D","E"),4),s > ample(c("A","B","C","D","E"),2), > > sample(c("A","B","C","D","E"),4),sample(c("A","B","C","D","E"),4))#nu > mber of unique "letters" is less than 4000 in real data set and they > are no duplicates within same ID > weight<-c(sample(c(1:30),3),sample(c(1:30),4),sample(c(1:30),2), > sample(c(1:30),4),sample(c(1:30),4))#number of unique > weights is below 50 in real data set and they are no duplicates > within same ID > > > data<-data.frame(id=id,letter=letter,weight=weight) > > #goal is to get the following transformation where a column is added > for each unique letter and the weight is pulled into the column if > the letter exist within the ID, otherwise NA > #so we would get datatransform like below but without the many steps > described here > > datatransfer<-data.frame(data,apply(data[2],2,function(x) > ifelse(x=="A",data$weight,NA))) > datatransfer<- > data.frame(datatransfer,apply(datatransfer[2],2,function(x) > ifelse(x=="B",data$weight,NA))) > datatransfer<- > data.frame(datatransfer,apply(datatransfer[2],2,function(x) > ifelse(x=="C",data$weight,NA))) > datatransfer<- > data.frame(datatransfer,apply(datatransfer[2],2,function(x) > ifelse(x=="D",data$weight,NA))) > datatransfer<- > data.frame(datatransfer,apply(datatransfer[2],2,function(x) > ifelse(x=="E",data$weight,NA))) > > colnames(datatransfer)<-c("id","weight","letter","A","B","C","D","E") > much appreciate the help, > > thanks > > Andras > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.