-3

I'm already struggling for two days just to transfer a selection of rows to a new column in r. My dataset looks like this: My starting point is "big_data". This is the data I have in a dataframe. The output should be "blub" in a dataframe. In blub the cdolumns should have names representing the value of i. data mutatie

Finally I came up with:

code I tried so many things already, but he doesn't want to give me the output as in the picture above. Is there someone who can help me whit this?

B de Blup
  • 9
  • 2
  • 3
    Does this answer your question? [How to reshape data from long to wide format](https://stackoverflow.com/questions/5890584/how-to-reshape-data-from-long-to-wide-format) Additionally, it would be easier to answer, when you would provide the data as a data.frame. – hannes101 Oct 24 '21 at 12:13

1 Answers1

0

You can use pivot_wider, from the tidyr package. For example:

library(tidyr)

blub <- pivot_wider(
  data = big_data,
  id_cols = id,
  names_from = i,
  values_from = Outstanding_LCY,
  names_glue = "kolom{.value}"
)
Mossa
  • 1,656
  • 12
  • 16
  • Dear Tobi, you are the best! This is exactly were I was looking for. Thank you so much! After running your code I added "blub<-data.frame(blub)" to make it again a dataframe, but he named the columns as X1, X2, X3 en X4. Since i is not always 4, is there a way to rename these dynamically in e.g. kolom1, kolom2.... kolomk? – B de Blup Oct 24 '21 at 12:30