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
• 410 points
60 views

1 answer to this question.

0 votes

You can use the reshape function

reshape(data, idvar = "name", timevar = "numbers", direction = "wide")
answered Apr 17, 2018 by nirvana
• 3,060 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,720 points
230 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
• 42,430 points
44 views
0 votes
1 answer
0 votes
1 answer

How to get attribute data from tag using R?

Hi, Use html_attr()/html_attrs() function to extract the attribute ...READ MORE

answered Sep 30 in Data Analytics by anonymous
• 31,840 points
18 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,170 points
27 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,690 points
135 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,690 points
64 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
59 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,060 points

edited Apr 12, 2018 by nirvana 3,583 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,060 points
1,718 views