Steve Steve - 2 months ago 8
R Question

How to reshape data from long to wide format?

I'm having trouble rearranging the following data frame:

dat1 <- data.frame(
name = rep(c("firstName", "secondName"), each=4),
numbers = rep(1:4, 2),
value = rnorm(8)
)


I want to reshape it so that each unique "name" variable is a rowname, with the "values" as observations along that row and the "numbers" as colnames. Sort of like this:

1 2 3 4
firstName value value value value
secondName value value value value


I've looked at
melt
and
cast
and a few other things, but none seem to do the job.

Answer

Using reshape function:

reshape(dat1, idvar = "name", timevar = "numbers", direction = "wide")
Comments