R: removing the last three dots from a string

后端 未结 3 1617
我在风中等你
我在风中等你 2021-01-03 04:49

I have a text data file that I likely will read with readLines. The initial portion of each string contains a lot of gibberish followed by the data I need. Th

3条回答
  •  轮回少年
    2021-01-03 05:19

    This will get you most of the way there, and it will have no problems with numbers that include commas:

    # First, use a regex to eliminate the bad pattern.  This regex
    # eliminates any three-character combination of periods, commas,
    # and big dots (•), so long as the combination is followed by 
    # 0-2 spaces and then a digit.
    aa.sub <- as.matrix(
      apply(aa, 1, function (x) 
        gsub('[•.,]{3}(\\s{0,2}\\d)', '\\1', x, perl = TRUE)))
    
    # Second: it looks as though you want your data split into columns.
    # So this regex splits on spaces that are (a) preceded by a letter, 
    # digit, or space, and (b) followed by a digit.  The result is a 
    # list, each element of which is a list containing the parts of 
    # one of the strings in aa.
    aa.list <- apply(aa.sub, 1, function (x) 
      strsplit(x, '(?<=[\\w\\d\\s])\\s(?=\\d)', perl = TRUE))  
    
    # Remove the second element in aa.  There is no space before the 
    # first data column in this string.  As a result, strsplit() split
    # it into three columns, not 4.  That in turn throws off the code
    # below.
    aa.list <- aa.list[-2]
    
    # Make the data frame.
    aa.list <- lapply(aa.list, unlist)  # convert list of lists to list of vectors
    aa.df   <- data.frame(aa.list)      
    aa.df   <- data.frame(t(aa.df), row.names = NULL, stringsAsFactors = FALSE) 
    

    The only thing remaining is to modify the regex for strsplit() so that it can handle the second string in aa. Or perhaps it's better just to handle cases like that manually.

提交回复
热议问题