R: removing the last three dots from a string

后端 未结 3 1620
我在风中等你
我在风中等你 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条回答
  •  旧时难觅i
    2021-01-03 05:15

    This does the trick, though not especially elegant...

    options(stringsAsFactors = FALSE)
    
    
    # Search for three consecutive characters of your delimiters, then pull out
    # all of the characters after that
    # (in parentheses, represented in replace by \\1)
    nums <- as.vector(gsub(aa$C1, pattern = "^.*[.,•]{3}\\s*(.*)", replace = "\\1"))
    
    # Use strsplit to break the results apart at spaces and just get the numbers
    # Use unlist to conver that into a bare vector of numbers
    # Use matrix(, nrow = length(x)) to convert it back into a
    # matrix of appropriate length
    num.mat <- do.call(rbind, strsplit(nums, split = " "))
    
    
    # Mash it back together with your original strings
    result <- as.data.frame(cbind(aa, num.mat))
    
    # Give it informative names
    names(result) <- c("original.string", "num1", "num2", "num3")
    

提交回复
热议问题