替换 ^ 和 |矩阵中的符号

发布于 2024-10-30 17:01:09 字数 669 浏览 0 评论 0原文

我有下表:

column1  column2
1        aaa^bbb
2        aaa^bbb|ccc^ddd

我想要一个输出文件,如下所示:

column1   column2     column3
1         aaa         bbb
2         aaa         bbb
3         ccc         ddd

您介意让我知道是否有聪明的方法可以做到这一点吗?

更新:

我试图做两件事;

对于 ^,我想将上下文分隔到第 2 列和第 3 列。

对于 |,我想将其分隔到下一行,但在第 1 列中保留相同的数字(第 1 列对于第 2 行和第 3 行是相同的。抱歉我这里写错

了,输入如下:

column1  column2 
x        aaa^bbb 
y        aaa^bbb|ccc^ddd 

输出如下:

column1   column2     column3 
x         aaa         bbb 
y         aaa         bbb 
y         ccc         ddd 

I have the following table:

column1  column2
1        aaa^bbb
2        aaa^bbb|ccc^ddd

I would like to have a output file as follows:

column1   column2     column3
1         aaa         bbb
2         aaa         bbb
3         ccc         ddd

Could you mind to let me know if there are smart way of doing this?

Update:

I was trying to do two things;

For ^, I want to separate the context to the column 2 and column 3.

For |, I want to separate it to the next row, but keeping the same number in column1 (the column1 is the same for row 2 and 3. Sorry that I make a mistake here.

To rewrite, input is as follows:

column1  column2 
x        aaa^bbb 
y        aaa^bbb|ccc^ddd 

Output is as follows:

column1   column2     column3 
x         aaa         bbb 
y         aaa         bbb 
y         ccc         ddd 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-11-06 17:01:09

完成您想要的操作的最简单方法就是使用 strsplit。例如,

> x = c("aaa^bbb", "aaa^bbb|ccc^ddd")
> ## Split the vector on ^ OR |.
> ## Since ^ and | are special characters
> ## we need to escape them: \\^ and \\|
> ## Split by column.
> new_x = unlist(strsplit(x, "\\|"))
> ## Split by row
> new_x = unlist(strsplit(new_x, "\\^"))
> new_x
 [1] "aaa" "bbb" "aaa" "bbb" "ccc" "ddd"

> ## Change the vector back into a matrix
> dim(new_x) = c(2,3)
> ## Transpose to get correct shape
> t(new_x)
     [,1]  [,2] 
[1,] "aaa" "bbb"
[2,] "aaa" "bbb"
[3,] "ccc" "ddd"

您可能可以结合拆分步骤,但我对您的数据格式没有足够的了解,无法确信它始终有效。

The easiest way to do what you are after, is just use strsplit. For example,

> x = c("aaa^bbb", "aaa^bbb|ccc^ddd")
> ## Split the vector on ^ OR |.
> ## Since ^ and | are special characters
> ## we need to escape them: \\^ and \\|
> ## Split by column.
> new_x = unlist(strsplit(x, "\\|"))
> ## Split by row
> new_x = unlist(strsplit(new_x, "\\^"))
> new_x
 [1] "aaa" "bbb" "aaa" "bbb" "ccc" "ddd"

> ## Change the vector back into a matrix
> dim(new_x) = c(2,3)
> ## Transpose to get correct shape
> t(new_x)
     [,1]  [,2] 
[1,] "aaa" "bbb"
[2,] "aaa" "bbb"
[3,] "ccc" "ddd"

You could probably combine the splitting step, but I don't have enough knowledge to your data format to be confident that it will always work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文