Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

r - Rearrange words in strings depending on conditions

I have this data:

df<- data.frame("position" = c("ante", "ex", "post", "post ante pre", "post pre", "ante post pre", "ex pre", "ante pre")) 

Now I want to move the word "pre" so that it's the first word in the string, but only for the strings containing two words and the word "pre", so row numbers 1, 2, 3, 4 and 6 should not be affected.

This should be the result:

df <- data.frame("position" = c("ante", "ex", "post", "post ante pre", "pre post", "ante post pre", "pre ex", "pre ante"))

I guess I can start by writing a grepl statement to only select the rows containing the word "pre" but after that I'm a bit lost.

question from:https://stackoverflow.com/questions/65830302/rearrange-words-in-strings-depending-on-conditions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use regex for this:

First I edited your example so that the starting and desired results are different (assuming this is your desired result here based on what you wrote)

library(dplyr)
library(stringr)


df <- data.frame("position" = c("ante", "ex", "post", "post pre ante", "post pre", "ante post pre", "ex pre", "pre ante")) 


df
#>        position
#> 1          ante
#> 2            ex
#> 3          post
#> 4 post pre ante
#> 5      post pre
#> 6 ante post pre
#> 7        ex pre
#> 8      pre ante
df2 <- data.frame("position" = c("ante", "ex", "post", "post pre ante", "pre post", "ante post pre", "pre ex", "pre ante"))
df2
#>        position
#> 1          ante
#> 2            ex
#> 3          post
#> 4 post pre ante
#> 5      pre post
#> 6 ante post pre
#> 7        pre ex
#> 8      pre ante


Then using regex:

df3 <- df %>%
  mutate(position = str_replace(position,'^([^\s]+) {1}(?=pre$)(pre)','\2 \1'))

df3
#>        position
#> 1          ante
#> 2            ex
#> 3          post
#> 4 post pre ante
#> 5      pre post
#> 6 ante post pre
#> 7        pre ex
#> 8      pre ante

identical(df2, df3)
#> [1] TRUE

Slight edit: I think the lookahead was unnecessary so we can reduce this to:

df3 <- df %>%
  mutate(position = str_replace(position,'^([^\s]+) {1}(pre)$','\2 \1'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...