+ Reply to Thread
Results 1 to 7 of 7

Thread: regular expressions

  1. #1
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    regular expressions




    I have the following two strings, which are N and W coordinates:

    Code: 
    N <- c("59.22.07", "58.01.32", "57.26.49")
    
    W <- c("02.26.45", "03.50.19", "07.24.15")
    What I need are the two strings below. Presume this will need a regular expression, my knowledge of which is limited at present.

    Code: 
    # needs last period removing
    N <- c("59.2207", "58.0132", "57.2649")
    
    # needs last period removing, plus a - at start
    W <- c("-02.2645", "-03.5019", "-07.2415")

  2. #2
    Beep
    Points: 64,668, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardFrequent PosterActivity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,442
    Thanks
    269
    Thanked 2,228 Times in 1,906 Posts

    Re: regular expressions

    A regex probably would work but to honest I'd probably just split the strings using strsplit and then create a function to combine the things the way you want them and then use sapply to use that function on each part of the list from strsplit.
    Code: 
    > sapply(strsplit(N, "\\."), function(x){paste0(x[1], ".", x[2], x[3])})
    [1] "59.2207" "58.0132" "57.2649"
    > sapply(strsplit(W, "\\."), function(x){paste0("-", x[1], ".", x[2], x[3])})
    [1] "-02.2645" "-03.5019" "-07.2415"
    Morte a tutti i raptors
    001100010010011110100001101101110011

  3. The Following User Says Thank You to Dason For This Useful Post:

    Layo909 (01-30-2013)

  4. #3
    ggplot2orBust
    Points: 37,451, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    4,032
    Thanks
    1,437
    Thanked 795 Times in 706 Posts

    Re: regular expressions

    I'm curious on how to do this with a negative look ahead. I tried:

    Code: 
    gsub("(!?\\.)", "", N)
    gsub("([\\.]?!)", "", N)
    But was not successful.
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  5. #4
    ggplot2orBust
    Points: 37,451, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    4,032
    Thanks
    1,437
    Thanked 795 Times in 706 Posts

    Re: regular expressions

    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  6. #5
    ggplot2orBust
    Points: 37,451, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    User with most referrers
    trinker's Avatar
    Location
    Buffalo, NY
    Posts
    4,032
    Thanks
    1,437
    Thanked 795 Times in 706 Posts

    Re: regular expressions

    Great responses:

    Code: 
    N <- c("59.22.07", "58.01.32", "57.26.49")
    
    gsub("(.*)\\.", "\\1", N)
    gsub("(.*)0", "\\1", N)#works with the 0s as well
    "If you torture the data long enough it will eventually confess."
    -Ronald Harry Coase -

  7. The Following User Says Thank You to trinker For This Useful Post:

    bugman (01-25-2013)

  8. #6
    Points: 9,891, Level: 66
    Level completed: 61%, Points required for next Level: 159

    Posts
    187
    Thanks
    30
    Thanked 2 Times in 2 Posts

    Re: regular expressions

    Quote Originally Posted by Dason View Post
    A regex probably would work but to honest I'd probably just split the strings using strsplit and then create a function to combine the things the way you want them and then use sapply to use that function on each part of the list from strsplit.
    Code: 
    > sapply(strsplit(N, "\\."), function(x){paste0(x[1], ".", x[2], x[3])})
    [1] "59.2207" "58.0132" "57.2649"
    > sapply(strsplit(W, "\\."), function(x){paste0("-", x[1], ".", x[2], x[3])})
    [1] "-02.2645" "-03.5019" "-07.2415"
    I was expecting 'paste0' to throw an error - this is a a new one for me. So paste0 simply saves having to type sep=''. Is that right?

  9. #7
    Beep
    Points: 64,668, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Discussion EnderPosting AwardCommunity AwardFrequent PosterActivity Award
    Dason's Avatar
    Location
    Ames, IA
    Posts
    11,442
    Thanks
    269
    Thanked 2,228 Times in 1,906 Posts

    Re: regular expressions


    Quote Originally Posted by Layo909 View Post
    I was expecting 'paste0' to throw an error - this is a a new one for me. So paste0 simply saves having to type sep=''. Is that right?
    Yup. paste0 was introducted with 2.15 basically to save a lot of people a little bit of typing. At this point they can't change the default to paste but since a lot of people thought it would be nicer if the default separator was just no spaces they added paste0.
    Morte a tutti i raptors
    001100010010011110100001101101110011

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats