分开10个没有空格的连续数字

时间:2017-05-19 05:55:10

标签: r

我的数据采用给定格式 -

Please find attached picture

如何在新列中获得10个连续数字?提前致谢

   app.directive("customGrid", function($timeout,$filter){
  return{
    scope:{ 
      "pages":"="
    },
    template:temp.join(''),
    link : function( scope, element, attrs ){
      scope.slice = null;

      scope.sortIt = function( value ){
        scope.pages.pin = $filter('orderBy')( value, '');
        console.log( scope.pages.pin ); 
//consoles sorted not updating in view
        scope.$apply(); 
      }

      $timeout(function(){
       scope.slice = scope.pages.pin;

       scope.sortIt( scope.slice );

      })

    }
  }
})

预期产出:

structure(list(Datetime = c("12/05/2017 08:22", "12/05/2017 08:32", 
"12/05/2017 08:32", "12/05/2017 08:44", "12/05/2017 08:44", "12/05/2017 08:47", 
"12/05/2017 08:48", "12/05/2017 08:49", "12/05/2017 08:51"), 
    Message = c("‪+91 98122 92212†< >", "‪+91 97799 88581†       9417673824   363006", 
    "‪+91 97799 88581†< >", "‪+91 99969 42327†     9990175777", 
    "‪+91 99969 42327†< >", "‪+91 98101 62461â€", "‪+91 98156 37302â€", 
    "‪+91 98156 37302†< >", "‪+91 88720 30191†      602013  9317588213"
    )), .Names = c("Datetime", "Message"), row.names = c("352", 
"353", "354", "355", "356", "357", "358", "365", "366"), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

使用stringi包,

library(stringi)
unlist(stri_extract_all_regex(df$Message, '([0-9]+){10}'))

#[1] NA           "9417673824" NA           "9990175777" NA           NA           NA           NA           "9317588213"

通过Base R,

sapply(strsplit(trimws(gsub('\\D+', ' ', df$Message)), ' '), 
                                                        function(i) i[nchar(i) == 10])

or

sapply(strsplit(trimws(gsub('\\D+', ' ', df$Message)), ' '), function(i) 
                                replace(i[nchar(i) == 10], !length(i[nchar(i) == 10]), NA))
#[1] NA           "9417673824" NA           "9990175777" NA           NA           NA           NA           "9317588213"