0

I need to reshape my data frame according to the particular column numbers. My data frame has 2 columns and 1127 rows and it is like that;

 Class    Gluc
 1         3.7
 1         3.8
 1         3.5
 1         3.9
 4         4.2
 4         5.1
 4         5.8
 4         4.9
 4         5.7
 9         6.6 
 9         6.9
 9         8.1
 9         8.8
 11        7.4
 11        8.3
 11        9.8

I want to do that the numbers in column 1 (Class) will be column names and glucose values will be align under these new column numbers according to their class number. Like this;

 1     4     9     11
3.7   4.2   6.6   7.4
3.8   5.1   6.9   8.3
3.5   5.8   8.1   9.8
3.9   4.9   8.8
      5.7

I used "group_by" and "mutate" functions but they didn't work and also they create a list. I want a data frame not a list. If someone help me I will be very appreciated..

  • Data frames expect rectangular data (all columns should have the same number of rows). Do you want to fill the missing values in the last row with NA for example? Why do you need a data.frame with this shape? – MrFlick May 22 '23 at 17:56
  • 1
    This might be helpful: https://stackoverflow.com/questions/7196450/create-a-data-frame-of-unequal-lengths – MrFlick May 22 '23 at 17:57
  • @MrFlick while it is correct what you are hypothesizing. I think OP needs a simple reshaping (for example `with pivot_wider()` – TarJae May 22 '23 at 19:46
  • @MrFlick I need this data frame because I want to find cmax , tpeak.. for each 'class' number. For example 3.9 for class 1, 5.7 for class 4.. I had used "pivot_wider" function before but it gave me a 'list' and I couldn't find what I want. – user21528954 May 23 '23 at 07:25
  • You don’t need to reshape for that purpose. If you use group_by from dplyr you would be able to do those calculations with the data in its original structure. – MrFlick May 23 '23 at 12:02

1 Answers1

0

We could do it with pivot_wider() to get it work smooth we first group by Class and add row numbers:

library(dplyr)
library(tidyr)

df %>%
  group_by(Class) %>% 
  mutate(id = row_number()) %>% 
  pivot_wider(names_from = Class, values_from = Gluc) %>% 
  select(-id)
   `1`   `4`   `9`  `11`
  <dbl> <dbl> <dbl> <dbl>
1   3.7   4.2   6.6   7.4
2   3.8   5.1   6.9   8.3
3   3.5   5.8   8.1   9.8
4   3.9   4.9   8.8  NA  
5  NA     5.7  NA    NA 
TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thank you for your solution but it is a list not a data frame and I cannot convert it to data frame – user21528954 May 23 '23 at 07:18
  • what is a list my output or your original data? – TarJae May 23 '23 at 07:27
  • the type of the outcome is a 'list'. So can not find cmax, tpeak etc in this type. If you have a solution to find them (cmax, tpeak) in a 'list', I will be very appreciated.. – user21528954 May 23 '23 at 07:38
  • 1
    The type of outcome is `1] "tbl_df" "tbl" "data.frame"` and not a list. Try this: `library(dplyr) library(tidyr) df %>% group_by(Class) %>% mutate(id = row_number()) %>% pivot_wider(names_from = Class, values_from = Gluc) %>% select(-id) %>% mutate(across(everything(), ~max(., na.rm = TRUE), .names= "max_{.col}"))` – TarJae May 23 '23 at 07:53