Reshape data from long to wide format

0 votes

I want to rearrange the following data frame:

set.seed(45)
data <- data.frame(
    name = rep(c("FirstName", "SecondName"), each=4),
    numbers = rep(1:4, 2),
    value = rnorm(8)
    )

data
       name  numbers      value
1  FirstName        1  0.3407997
2  FirstName        2 -0.7033403
3  FirstName        3 -0.3795377
4  FirstName        4 -0.7460474
5  SecondName       1 -0.8981073
6  SecondName       2 -0.3347941
7  SecondName       3 -0.5013782
8  SecondName       4 -0.1745357

I want to re arrange in such a way that each unique"name" variable is a row name, with the values as "observations" along that row. Also, I want the "numbers" as columns. You can refer below for example:

     name          1          2          3         4
1  FirstName  0.3407997 -0.7033403 -0.3795377 -0.7460474
5 SecondName -0.8981073 -0.3347941 -0.5013782 -0.1745357

Can somebody please help?

Apr 17, 2018 in Data Analytics by BHARANI
• 400 points
37 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can use the reshape function

reshape(data, idvar = "name", timevar = "numbers", direction = "wide")
answered Apr 17, 2018 by nirvana
• 3,040 points

Related Questions In Data Analytics

0 votes
1 answer

How to import data from a Json file into R?

Easy and simple: library("rjson") json_file <- "file_name" json_data <- fromJSON(file=json_file)) or ...READ MORE

answered Apr 26, 2018 in Data Analytics by DeepCoder786
• 1,700 points
89 views
0 votes
1 answer

How to download the historical data from two random picked stocks?

assign the output of sample into a variable. my_picks <- ...READ MORE

answered Nov 5, 2018 in Data Analytics by Kalgi
• 35,750 points
9 views
0 votes
1 answer
0 votes
1 answer

How to Share the files from local system Mac Book to Edureka VM

Hey Satya, Hope you're doing great. Would you please ...READ MORE

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,610 points
26 views
0 votes
1 answer

Get list of rownames as values after aggregating a dataframe

You can use reshape2 library: library(reshape2) data <- read.table(text="type ...READ MORE

answered Apr 17, 2018 in Data Analytics by darklord
• 6,140 points
11 views
0 votes
1 answer

Reshape data from long to wide format in R

Use reshape function: reshape(dat1, idvar = "name", timevar = ...READ MORE

answered Jun 13, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
32 views
0 votes
1 answer

Reshape dataframe without “timevar” from long to wide format in R

Assuming that the data is in the ...READ MORE

answered Jun 13, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
18 views
0 votes
1 answer

Big Data transformations with R

Dear Koushik, Hope you are doing great. You can ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
27 views
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,040 points

edited Apr 12, 2018 by nirvana 1,297 views
0 votes
1 answer

.SD in data.table in R

.SD stands for "Subset of Data.table". The ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,040 points
690 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.