Q:

reshaping columns in a data frame

Say I have the following data frame:
ID<-c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3, 4,4,4,4,4,5,5,5,5,5)
Score<- sample(1:20, 25, replace=TRUE)
days<-rep(c("Mon", "Tue", "Wed", "Thu", "Fri"), times=5)
t<-cbind(ID, Score, days)

I would like to reshape it so that the new columns are ID and the actual weekday names, (meaning 6 columns) and the Score values are distributed according to their ID and day name. Something like this:
 
I found that reshape package might do. Tried (melt and cast) but it did not produce the result I wanted, but something like in this post: Melt data for one column

A:

A base R solution that uses the built-in reshape command.
set.seed(12345)
t <- data.frame(id = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5),
                score = sample(x = 1:20,size = 25,replace = TRUE),
                days = rep(x = c("Mon","Tue","Wed","Thu","Fri"),times = 5))

t.wide <- reshape(data = t,
                  v.names = "score",
                  timevar = "days",
                  idvar = "id",
                  direction = "wide")
names(t.wide) <- gsub(pattern = "score.",replacement = "",x = names(t.wide),fixed = TRUE)
t.wide
   id Mon Tue Wed Thu Fri
1   1  15  18  16  18  10
6   2   4   7  11  15  20
11  3   1   4  15   1   8
16  4  10   8   9   4  20
21  5  10   7  20  15  13

A:

You can use reshape2 to do this, but you need a data.frame to do that. Using cbind produces a matrix. (And converts all your numerical variables to characters in this case, as matrices can only hold one data type). 
I've changed your code to produce a dataframe, which is already in long format (one row per observation).
set.seed(123)
ID<-c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3, 4,4,4,4,4,5,5,5,5,5)
Score<- sample(1:20, 25, replace=TRUE)
days<-rep(c("Mon", "Tue", "Wed", "Thu", "Fri"), times=5)
dat<-data.frame(ID, Score, days)

Changing it to wide using reshape2 is then quite straightforward:
library(reshape2)

res <- dcast(ID~days,value.var="Score",data=dat)

> res
  ID Fri Mon Thu Tue Wed
1  1  16   3   2  12   6
2  2  19  13  12   7  19
3  3  19  19  17   8  15
4  4  15   3   8   1  20
5  5   3  11  18   8  15

A:

You could also use unstack if your data are complete (same number of days per id).
Here's an example (using the data from TARehman's answer):
unstack(t, score ~ days)
#   Fri Mon Thu Tue Wed
# 1  10  15  18  18  16
# 2  20   4  15   7  11
# 3   8   1   1   4  15
# 4  20  10   4   8   9
# 5  13  10  15   7  20

Here's the clean-up for the column ordering, and for adding in the ID column:
cbind(ID = unique(t$id), unstack(t, score ~ days)[c("Mon", "Tue", "Wed", "Thu", "Fri")])
##   ID Mon Tue Wed Thu Fri
## 1  1  15  18  16  18  10
## 2  2   4   7  11  15  20
## 3  3   1   4  15   1   8
## 4  4  10   8   9   4  20
## 5  5  10   7  20  15  13

