在每个字符处分割字符串

问题描述 投票:0回答:1

我有一个庞大的数据集,例如:

customer_id     customer_math
  15251           10001010
  10101           11111111
  84787           10101010

我想在每个字符处拆分customer_math以接收这样的df:

customer_id     2012   2013   2014   2015  2016  2017 2018 2019
  15251           1      0     0       0    1     0     1    0
  10101           1      1     1       1    1     1     1    1   
  84787           1      0     1       0    1     0     1    0

我尝试过但失败了。

您能帮我吗?

非常感谢您的支持!

r strsplit
1个回答
0
投票

这里是一种可能的解决方案:

out <- strsplit(as.character(df$customer_math), "(?=.)", perl=TRUE)
data.frame(df, do.call(rbind, out))
© www.soinside.com 2019 - 2024. All rights reserved.