我知道有人问过如何在文本中输入字符串,但直到今天我对regex
都不熟悉。
我合并了一组数据文件并将文件名作为列。我还需要从文件名中提取X和Y坐标,并将其放在两列中。
示例:
name1_name2_20151028_0_R02X139Y310_1 expected output is
X Y
139 310
我试过了:
pattern <- "^name1_name2_(\\d+)_0_R(\\d+)X(\\d+)Y(\\d+)_1.*txt$"
当我试图为grep运行时,并没有成功,所以我不能再继续了。
任何帮助将不胜感激。感谢
答案 0 :(得分:3)
让我们把它分成两步:
1. Create a regular expression to extract the information
gsub("(.*X)([0-9]+)(Y)([0-9]+)(.*)", "\\2, \\4", df$col)
2. Split the information into two columns by the comma
library(splitstackshape)
df=cSplit(df, "col", ",")