Was it helpful?

Question

How to re-arrange data from long format to wide format in R?

R ProgrammingServer Side ProgrammingProgramming

This can be done by using reshape function.

Example

> data <- data.frame(
   name = rep(c("firstName", "LastName"), each=5),
   salarygroup = rep(1:5, 2),
   Errors = sample(21:60,10,replace=TRUE)
)
> data

name salarygroup Errors

1 firstName 1 58
2 firstName 2 50
3 firstName 3 47
4 firstName 4 29
5 firstName 5 36
6 LastName  1 34
7 LastName  2 40
8 LastName  3 54
9 LastName  4 38
10 LastName 5 41
> reshape(data, idvar = "name", timevar = "salarygroup", direction = "wide")

name Errors.1 Errors.2 Errors.3 Errors.4 Errors.5

1 firstName 58 50 47 29 36
6 LastName 34 40 54 38 41
raja
Published on 06-Jul-2020 18:01:26
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top