简体   繁体   中英

Stacking columns based on commonality of column name R

Ok, so I would like to, in an automatic way, take columns with similarities in their name, eg, x1, x2, ...,xn or y_1, y_2, y_3, ..., y_n to get stacked based on pairs. Lets first make some data:

set.seed(1)
data <- purrr::rerun(3, x = runif(10), y = rnorm(10)) %>%
dplyr::bind_cols() %>%
dplyr::mutate(id1 = letters[1:10], id2 = LETTERS[1:10])

Then I would like all pairs of x1, x2, x3, and y1, y2, y3, to be turned into two columns x, y, and then have the two id columns after that (which will be repeated stacks). Is there an easy way of doing this? This is my current attempt:

data %>%
gather('k', 'v', -id1, -id2) %>%
mutate(k = str_remove(k, '[0-9]')) %>%
split(.$k) %>%
lapply(function(x) spread(x, 'k', 'v'))

but it gives me the following error:

Error: Duplicate identifiers for rows (1, 11, 21), (2, 12, 22), (3, 13, 23), (4, 14, 24), (5, 15, 25), (6, 16, 26), (7, 17, 27), (8, 18, 28), (9, 19, 29), (10, 20, 30)

which I'm not sure how to escape.

Does this looks like your desired output?

# A tibble: 30 x 4
   id1   id2       x       y
   <chr> <chr> <dbl>   <dbl>
 1 a     A     0.266 -0.820 
 2 a     A     0.482  0.919 
 3 a     A     0.913 -0.415 
 4 b     B     0.372  0.487 
 5 b     B     0.600  0.782 
 6 b     B     0.294 -0.394 
 7 c     C     0.573  0.738 
 8 c     C     0.494  0.0746
 9 c     C     0.459 -0.0593
10 d     D     0.908  0.576 
# … with 20 more rows

If you hold onto the extra id information in k until after you spread , you can avoid the ambiguous id error. Here I called that extra info k2 .

data %>%
  gather('k', 'v', -id1, -id2) %>%
  mutate(k2 = str_replace(k, "\\D", ""),
         k = str_replace(k, "\\d", "")) %>%
  spread('k', 'v') %>%
  select(-k2)

Then you can drop k2 at the end and no need for split() %>% lapply()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM