Tidyr 单独函数的正则表达式 [英] Regular expression on separate function of Tidyr

查看:29
本文介绍了Tidyr 单独函数的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用 tidyr 分隔两列.

I need separate two columns with tidyr.

该列的文本如下:I am Sam.我的意思是文本总是只有两个空格,文本可以有所有其他符号:[a-z][0-9][!\ºª, etc...].

The column have text like: I am Sam. I mean the text always have only two white spaces, and the text can have all other symbols: [a-z][0-9][!\ºª, etc...].

问题是我需要把它分成两列:第一列I am,第二列:Sam.

The problem is I need split it in two columns: Column one I am, and column two: Sam.

我找不到与第二个空格分开的正则表达式.

I can't find a regular expression two separate with the second blank space.

你能帮我吗?

推荐答案

我们可以使用 tidyr 中的 extract.我们匹配一个或多个字符并将其放入一个捕获组 ((.*)) 后跟一个或多个空格 (\\s+) 和另一个包含仅使用非空白字符 (\\S+) 将原始列分成两列.

We can use extract from tidyr. We match one or more characters and place it in a capture group ((.*)) followed by one or more space (\\s+) and another capture group that contains only non-white space characters (\\S+) to separate the original column into two columns.

library(tidyr)
extract(df1, Col1, into = c("Col1", "Col2"), "(.*)\\s+(\\S+)")
#   Col1 Col2
#1  I am  Sam
#2 He is  Sam

数据

df1 <- data.frame(Col1 = c("I am Sam", "He is Sam"), stringsAsFactors=FALSE)

这篇关于Tidyr 单独函数的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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