简体   繁体   English

根据列名R的共性堆叠列

[英]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. 好的,所以我想自动获取名称相似的列,例如x1,x2,...,xn或y_1,y_2,y_3,...,y_n以基于对进行堆积。 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). 然后我希望将所有成对的x1,x2,x3和y1,y2,y3变成两列x,y,然后在那之后有两个id列(将重复堆栈)。 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. 如果在spread之后一直保留k的额外id信息,则可以避免模棱两可的id错误。 Here I called that extra info k2 . 在这里,我称该额外信息为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() 然后您可以在末尾删除k2 ,而无需split() %>% lapply()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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