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
• 420 points
751 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,130 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
2,359 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
• 52,350 points
735 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, 2019 in Data Analytics by anonymous
• 33,030 points
852 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 Sahiti
• 6,370 points
687 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 14, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
2,738 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 14, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
990 views
0 votes
1 answer

Reshaping/Deduping long data to wide in R

Using pivot_wider and rename library(dplyr) library(tidyr) repl <- c("1st_transaction" = "type_1", "2nd_transaction" = ...READ MORE

answered Mar 17, 2023 in Others by Kithuzzz
• 38,000 points
388 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,130 points

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

.SD in data.table in R

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

answered Apr 13, 2018 in Data Analytics by nirvana
• 3,130 points
7,092 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP