Home > Software design >  R: Reshape rows to columns and fill with NA
R: Reshape rows to columns and fill with NA

Time:10-26

I have a dataframe with 0-3 rows depending on the underlying data. Here is an example with 2 rows:

df <- tibble(ID = c(1, 1), v = c(1, 2))
     ID     v
  <dbl> <dbl>
1     1     1
2     1     2

I now want to convert each row of v into a separate column. As I have 3 rows at maximum, the result should look like this:

     ID v1       v2    v3
  <dbl> <dbl> <dbl> <dbl>
1     1 NA        1     2

Whats the best way to achieve this? Thanks!

CodePudding user response:

Perhaps this helps

library(dplyr)
library(tidyr)
library(stringr)
df %>%
   mutate(nm = str_c("v", 2:3)) %>% 
  complete(ID, nm = str_c("v", 1:3)) %>%
  pivot_wider(names_from = nm, values_from = v)

CodePudding user response:

Update: Op request, see comments:

df %>% 
  group_by(ID) %>% 
  summarise(cur_data()[seq(max_n),]) %>% 
  arrange(!is.na(v), v) %>% 
  mutate(row = row_number()) %>%
  pivot_wider(names_from = row,
              values_from = v,
              names_glue = "v_{.name}")
     ID   v_1   v_2   v_3
  <dbl> <dbl> <dbl> <dbl>
1     1    NA     1     2

First answer: Maybe something like this:

What we are doing here is:

  1. define the max of your group (in this case it is 3) then fill up each group to max of 3 with adding NA
  2. For naming add a row_number() column and use pivot_wider with it'S arguments:
library(dplyr)
library(tidyr)

max_n <- 3

df %>% 
  group_by(ID) %>% 
  summarise(cur_data()[seq(max_n),]) %>% 
  mutate(row = row_number()) %>% 
  pivot_wider(names_from = row,
              values_from = v,
              names_glue = "v_{.name}")

     ID   v_1   v_2   v_3
  <dbl> <dbl> <dbl> <dbl>
1     1     1     2    NA
  • Related