替换 ^ 和 |矩阵中的符号 [英] Replacing ^ and | sybmbols in a matrix
问题描述
我有下表:
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?
我想做两件事;
对于 ^,我想将上下文与第 2 列和第 3 列分开.
For ^, I want to separate the context to the column 2 and column 3.
对于|,我想把它分开到下一行,但在column1中保持相同的数字(column1对于第2行和第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.
重写,输入如下:
column1 column2
x aaa^bbb
y aaa^bbb|ccc^ddd
输出如下:
column1 column2 column3
x aaa bbb
y aaa bbb
y ccc ddd
推荐答案
最简单的方法就是使用 strsplit
.例如,
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.
这篇关于替换 ^ 和 |矩阵中的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!