将字符串向量转换为整齐格式

时间:2017-12-01 15:28:29

标签: r

以下是网站网址和一些文字的向量,其中每个网址和文字由空格分隔:

keyof

我试图转换为整洁的格式:

T

使用:

declare function foo4<T>(x: keyof T);
let t4 = foo4<Bar>('t');

但这会返回:

v <- c("url www.site1.com this is the text of the site" , "url www.site2.com this is the text of the other site" )

是否需要使用替代正则表达式来实现所需的tibble格式?

3 个答案:

答案 0 :(得分:2)

v <- c("url www.site1.com this is the text of the site" , "url www.site2.com this is the text of the other site" )
df = data.frame(v)
tidyr::separate(df, v, into = c("literally_just_url", "url", "text"),
                sep = " ", extra = "merge")
#   literally_just_url           url                               text
# 1                url www.site1.com       this is the text of the site
# 2                url www.site2.com this is the text of the other site

答案 1 :(得分:2)

如下:

library(tidyverse)

tibble(v = v) %>% 
  mutate_at("v", str_replace, pattern = "^url ", replacement = "") %>% 
  separate(v, c("url", "text"), sep = " ", extra = "merge")

答案 2 :(得分:1)

这个怎么样,

df %>% 
extract(v, into = c('url', 'text'),  regex = "url\\s+(\\S+)\\s+([A-Za-z ]+)")

正则表达式的说明:使用url\\s匹配网址后跟空格。接下来是一个或多个字母数字字符,没有要匹配的空格(\\S+)。其次是另一个空格\\s。最后是带有空格([A-Za-z ]+)

的文本的其余部分