0

My dataset consists of information about the degree(s) students obtained upon graduation. So, if a student graduates with two degrees, he/she will appear twice. For example:

StudentID    DegreeType      Date        MajorName
    1            BS         04/20/16      Biology
    1            BA         04/20/16      Math

Some students just appear once because they've only completed one degree.

I'm trying to change the dataset from "long" to "wide" format but cannot get what I'm looking for. I've used dcast and reshape but to no avail. I need something like this

StudentID    DegreeType1    DegreeType2    Date1     Date2    MajorName1    MajorName2

Any suggestions would be great.

user122514
  • 397
  • 5
  • 13

1 Answers1

0

dcast function is not in reshape package,in reshape package,you can use cast function.

StudentID=c(1,1)
DegreeType=c("BS","BA")
Date=c("04/20/16","04/20/16")
MajorName = c("Biology","Math")
df <- data.frame(StudentID,DegreeType,Date,MajorName)
zz <- melt(df,id=c("StudentID"))
cc <- cast(zz,StudentID~variable,print)
cc
> cc
     StudentID DegreeType_X1 DegreeType_X2  Date_X1  Date_X2 MajorName_X1 MajorName_X2
1         1            BS            BA 04/20/16 04/20/16      Biology         Math
chunjin
  • 240
  • 1
  • 8