用R中的多个字符串替换相同的文本

时间:2017-07-17 23:22:00

标签: r text data-manipulation

以下是我的示例数据:

....[a bunch of promises].then(activityF).then(activityG).then(activityH).then(function(doc){console.log(doc);});

我想用“type”中的每个字符串替换xxx。现在我使用gsub函数如下,但它一次只能替换一个查询。

root <- c("how to manage xxx","how to run xxx","how to operate xxx")
type <- c("resturant","grocery store","retail store")

结果应该是:

kw <- gsub("xxx", "123", root)

2 个答案:

答案 0 :(得分:5)

// services/auth.service.ts import { Injectable, NgZone } from '@angular/core'; import {Router} from '@angular/router'; import { tokenNotExpired } from 'angular2-jwt'; // We want to avoid any 'name not found' // warnings from TypeScript declare var Auth0Lock: any; @Injectable() export class AuthenticationService { constructor( private _router: Router, private _zone: NgZone) {} lock = new Auth0Lock('gpnhOqi20kE7pOM61DYI6BuI93ZjgA4j', 'jasont8.auth0.com'); login() { this.lock.show((error: string, profile: Object, id_token: string) => { if (error) { console.log(error); } // We get a profile object for the user from Auth0 localStorage.setItem('profile', JSON.stringify(profile)); // We also get the user's JWT localStorage.setItem('id_token', id_token); }); } logout() { // To log out, we just need to remove // the user's profile and token localStorage.removeItem('profile'); localStorage.removeItem('id_token'); } loggedIn() { return tokenNotExpired(); } } 魔术:

regmatches<-

如果您需要创建新值,则需要首先重复原始regmatches(root, regexpr("xxx",root)) <- type root #[1] "how to manage resturant" "how to run grocery store" #[3] "how to operate retail store" 向量:

root

答案 1 :(得分:2)

data.frame(x = unlist(lapply(type, function(x) gsub("xxx",x,root))))
#                             x
#1      how to manage resturant
#2         how to run resturant
#3     how to operate resturant
#4  how to manage grocery store
#5     how to run grocery store
#6 how to operate grocery store
#7   how to manage retail store
#8      how to run retail store
#9  how to operate retail store