Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pivot_wider where values are characters?

I want to use pivot_wider or spread where the values are character strings.

Here's an example:

a <- c("a", "a", "b", "c", "c", "c")
x <- c("x", "y", "y", "x", "y", "z")
m <- cbind(a,x)
> m
  a   x  
[1,] "a" "x"
[2,] "a" "y"
[3,] "b" "y"
[4,] "c" "x"
[5,] "c" "y"
[6,] "c" "z"

The output I want is:

      V1 V2   V3   V4
one    a  x    y <NA>
two    b  y <NA> <NA>
three  c  x    y    z

However, my old go to, spread, doesn't work:

> as.data.frame(m) %>% spread(key = a, value = x)
Error: Each row of output must be identified by a unique combination of keys.
Keys are shared for 5 rows:
* 1, 2
* 4, 5, 6

nor does pivot_wider:

 as.data.frame(m) %>%
  group_by(a) %>%
  mutate(namer = n()) %>%
  ungroup() %>%
  pivot_wider(#id_cols = a,
              names_from = namer,
              values_from = x,
              values_fill = list(namer = "none"),
              names_prefix = "City")

Any suggestions?

@akrun thanks for the answer, there's quite a bit going on there, here's what I've narrowed it down to:

m <- data.frame(a, x) 
m %>% group_by(a) %>% 
   mutate(rn = str_c("V", row_number()+1)) %>% 
   ungroup %>% 
   pivot_wider(names_from = rn, values_from = x) 

works. But,

m %>% 
   group_by(a) %>% 
   mutate(rn = n()) %>% 
   ungroup %>% 
   pivot_wider(names_from = rn, values_from = x, names_prefix = "V") 

does not. So it seems the problem was that I was using numbers as names, and converting them to characters using names_prefix. Is that your understanding also?

like image 581
Ahsen Majid Avatar asked Nov 15 '25 15:11

Ahsen Majid


1 Answers

We can create the data.frame instead of a matrix (cbind creates a matrix)

library(dplyr)
library(tidyr)
library(stringr)
library(english)
m %>% 
    group_by(a) %>%
    mutate(rn = str_c("V", row_number()+1)) %>% 
    ungroup %>%
    rename(V1 = a) %>%
    pivot_wider(names_from = rn, values_from = x) %>%   
    mutate(rn = as.character(english(row_number()))) %>%
    column_to_rownames('rn')
#      V1 V2   V3   V4
#one    a  x    y <NA>
#two    b  y <NA> <NA>
#three  c  x    y    z

data

m <- data.frame(a, x)
like image 72
akrun Avatar answered Nov 18 '25 06:11

akrun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!