拆分或分开不均匀/不相等的字符串,没有分隔符 [英] Split or separate uneven/unequal strings with no delimiter

查看:17
本文介绍了拆分或分开不均匀/不相等的字符串,没有分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定数据帧df:

x <- c("X1", "X2", "X3", "X4", "X5")
y <- c("00L0", "0", "00012L", "0123L0", "0D0")
df <- data.frame(x, y)

如何利用 tidyr::separatey 字符串的每个字符放入单独的列(每个字符串位置一列)?

How can I leverage tidyr::separate to put each character of the y strings into a separate column (one column per string position)?

所需的输出:

x <- c("X1", "X2", "X3", "X4", "X5")
m1 <- c(0, 0, 0, 0, 0)
m2 <- c(0, NA, 0, 1, "D")
m3 <- c("L", NA, 0, 2, 0)
mN <- c(NA, NA, NA, NA, NA)
df <- data.frame(x, m1, m2, m3, mN)

理论上 mN 可以达到 m100(100 列)或更高.

Where mN could theoretically go up to m100 (100 columns), or higher.

推荐答案

这有效.它用空格而不是 NA 填充,但如果您愿意,您可以事后更改.(fill = 'right' 仅在分割字符向量时有效,而不是明确的位置.)

This works. It fills with blanks rather than NAs, but you can change that post-hoc if you prefer. (fill = 'right' only works when splitting on a character vector, not explicit positions.)

maxchar = max(nchar(as.character(df$y)))
tidyr::separate(df, y, into = paste0("y", 1:maxchar), sep = 1:(maxchar - 1))

#    x y1 y2 y3 y4 y5 y6
# 1 X1  0  0  L  0         
# 2 X2  0                  
# 3 X3  0  0  0  1  2  L   
# 4 X4  0  1  2  3  L  0   
# 5 X5  0  D  0      

这篇关于拆分或分开不均匀/不相等的字符串,没有分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆