Reshape data from long to wide format in R

0 votes
I want to rearrange the following data frame:
set.seed(45)
dat1 <- data.frame(
    name = rep(c("firstName", "secondName"), each=4),
    numbers = rep(1:4, 2),
    value = rnorm(8)
    )

dat1
       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 reshape it in such a way that each unique "name" variable is a row name, with the "values" as observations along that row and the "numbers" as column names. 

     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

Any help is highly appreciated!

Jun 14, 2018 in Data Analytics by Sahiti
• 6,370 points
2,751 views

1 answer to this question.

0 votes

Use reshape function:

reshape(dat1, idvar = "name", timevar = "numbers", direction = "wide")
answered Jun 14, 2018 by CodingByHeart77
• 3,750 points

Related Questions In Data Analytics

0 votes
1 answer
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,839 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,136 views
0 votes
1 answer

How to convert tables to a data frame in R ?

> trial.table.df <- as.data.frame(trial.table) //assuming that trial.table ...READ MORE

answered Apr 20, 2018 in Data Analytics by zombie
• 3,790 points
7,627 views
0 votes
1 answer

Reshape data from long to wide format

You can use the reshape function reshape(data, idvar ...READ MORE

answered Apr 17, 2018 in Data Analytics by nirvana
• 3,130 points
771 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
718 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
403 views
0 votes
1 answer

Big Data transformations with R

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

answered Dec 18, 2017 in Data Analytics by Sudhir
• 1,570 points
1,049 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
1,003 views
0 votes
2 answers

How to use group by for multiple columns in dplyr, using string vector input in R?

data = data.frame(   zzz11def = sample(LETTERS[1:3], 100, replace=TRUE),   zbc123qws1 ...READ MORE

answered Aug 6, 2019 in Data Analytics by anonymous
14,119 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