Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save inkhorn/3956475 to your computer and use it in GitHub Desktop.
Save inkhorn/3956475 to your computer and use it in GitHub Desktop.
Crazy data reshaping script
# Here's where I extract the database IDs and repeat them 50 times to make the column long enough for
# my new long-form dataset (596,100 rows)
client.data.new = rep(client.data[,1],50)
for (i in 2:32){
# for each column in the first 31 after the ID column, find the 49 matching columns
# to the right and stack them using melt
stacked.data = melt(client.data, id.vars="CnBio_ID", measure.vars=seq(i,(i+(31*49)),31), value.name=names(client.data)[i])
# Once we have one new stacked column, we add it on to the right of whatever has already been built in the
# new long-form dataset.
client.data.new = data.frame(client.data.new, stacked.data[,3])
}
# I don't know why the "value.name" argument didn't give me the right variable names for my
# new long-form dataset, but it was easy enough to use this last line of code to fix that!
names(client.data.new) = names(client.data)[1:32]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment