data.frame中的字符串替换。多次出现

时间:2015-09-08 11:07:49

标签: r string dataframe

对于以下示例data.fame

df <- data.frame(n=as.integer(seq(from = 1, to = 5, by = 1)),
                 s1=as.character(rep(x = "abc|efg|hij|klm", times = 5)), 
                 s2=as.character(rep(x = "ABC - EFG - HIJ - KLM", times = 5)), 
                 stringsAsFactors=FALSE) 

df

  n              s1                    s2
1 1 abc|efg|hij|klm ABC - EFG - HIJ - KLM
2 2 abc|efg|hij|klm ABC - EFG - HIJ - KLM
3 3 abc|efg|hij|klm ABC - EFG - HIJ - KLM
4 4 abc|efg|hij|klm ABC - EFG - HIJ - KLM
5 5 abc|efg|hij|klm ABC - EFG - HIJ - KLM

我需要更换“|” s1列中的字符,带有单个字符“;”对于列s2,将字符串“ - ”替换为“|”为了obtian以下data.frame

  n              s1                 s2
1 1 abc;efg;hij;klm ABC |EFG |HIJ |KLM
2 2 abc;efg;hij;klm ABC |EFG |HIJ |KLM
3 3 abc;efg;hij;klm ABC |EFG |HIJ |KLM
4 4 abc;efg;hij;klm ABC |EFG |HIJ |KLM
5 5 abc;efg;hij;klm ABC |EFG |HIJ |KLM

感谢您的帮助

1 个答案:

答案 0 :(得分:4)

我们可以在列's''和's2'上使用gsub,匹配模式|-并将其替换为;和{{1} }。

|

如果要替换多个模式,则另一个选项是来自df$s1 <- gsub('[|]', ';', df$s1) df$s2 <- gsub('-\\s+', '|', df$s2) df # n s1 s2 #1 1 abc;efg;hij;klm ABC |EFG |HIJ |KLM #2 2 abc;efg;hij;klm ABC |EFG |HIJ |KLM #3 3 abc;efg;hij;klm ABC |EFG |HIJ |KLM #4 4 abc;efg;hij;klm ABC |EFG |HIJ |KLM #5 5 abc;efg;hij;klm ABC |EFG |HIJ |KLM 的{​​{1}}。我们使用mgsub遍历第2和第3列,通过将library(qdap)lapply指定为mgsub来对各列进行pattern

replacement