Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R中将行字符串拆分为多列_R_Split_Text Mining_Arabic - Fatal编程技术网

在R中将行字符串拆分为多列

在R中将行字符串拆分为多列,r,split,text-mining,arabic,R,Split,Text Mining,Arabic,我使用了R,我将这个字符串作为一行,我需要将它拆分为列 “id”:1050442590754103297,“id_str”:“1050442590754103297”,“name”: ‘屏幕名称’:‘uclkGkQ5’,‘位置’:无, “url”:无,“描述”:“\u200f\u200fنزوا㶎㶎㶎㶎㶎㶎” “译者类型”:“无”、“受保护”:False, “已验证”:假,“追随者计数”:1567,“朋友计数”:4019, “列出的数量”:0,“收藏夹数量”:6669,“状态数量”:9279,

我使用了R,我将这个字符串作为一行,我需要将它拆分为列

“id”:1050442590754103297,“id_str”:“1050442590754103297”,“name”: ‘屏幕名称’:‘uclkGkQ5’,‘位置’:无, “url”:无,“描述”:“\u200f\u200fنزوا㶎㶎㶎㶎㶎㶎” “译者类型”:“无”、“受保护”:False, “已验证”:假,“追随者计数”:1567,“朋友计数”:4019, “列出的数量”:0,“收藏夹数量”:6669,“状态数量”:9279, “创建时间”:“2018年10月11日星期四17:46:44+0000”,“utc偏移”:无, “时区”:无,“启用地理位置”:False,“lang”:“ar”, “已启用贡献者”:False,“is_translator”:False, “配置文件背景颜色”:“F5F8FA”,“配置文件背景图像url”: ''“个人资料、背景、图像、url、https':'', “profile\u background\u tile”:False,“profile\u link\u color”:“1DA1F2”, “配置文件\侧栏\边框\颜色”:“C0契约”, “配置文件\侧栏\填充\颜色”:“DDEEF6”,“配置文件\文本\颜色”: '333333','profile\u use\u background\u image':True,'profile\u image\u url': '', '配置文件\u图像\u url\u https': '', “配置文件\u横幅\u url”: '', “默认配置文件”:True,“默认配置文件\图像”:False,“后续”: 无,“跟随请求发送”:无,“通知”:无

我尝试过这段代码是有效的,但是我需要指定我需要的列的数量,并且我需要在最后重命名这些列,所以这很困难并且需要时间

d<-str_split_fixed(try$user, ",", 4)
对于字符串的其余部分也是一样的 希望您理解我的意思,并感谢您

这是模仿JSON(但不合法)。一种方法(如果假设为真)是“转换为JSON”并从那里解析

首先:我在R会话中遇到一个问题,阿拉伯字母没有正确地存储在字符串中。这发生在调用
gsub
等之前,因此我相信它可能在您的机器上正常工作。因此,您将在输出中看到空字符串。(Vice在本地搜索到这一点,我想先让您了解一下代码。)

假设:

  • 没有嵌入双引号
  • 除了逻辑值之外,True/False/None文本字符串永远不会嵌入文本中(例如,
    'screen\u name':'Is None'
    不会发生)
  • 您愿意在数据中找到以前存在的
    :None
重要转换:

  • True
    False
    改为小写
  • None
    null
  • 将整个内容封装在一个字典中,周围有
    {
    }
  • 将所有单引号转换为双引号
您可以在此处使用
magrittr
管道以提高可读性,也可以将所有函数嵌套起来(只需稍微快一点):

out%
gsub(“:\\s*False\\s*(,?)”,“:False\\1”,)%>%
gsub(“:\\s*无\\s*(,?)”,“:null\\1”,)%>%
gsub(“'”,“'”,)%>%
粘贴0(“{,,“}”)%>%
jsonlite::fromJSON(.)
结果(用
str
压缩):

str(out)
#39人名单
#$id:num 1.05e+18
#$id_街:chr“1050442590754103297”
#$name:chr“”
#$screen_name:chr“uclkGkQ5”
#$location:NULL
#$url:NULL
#$description:chr“”
#$translator\u类型:chr“无”
#$protected:logi-FALSE
#$verified:logi-FALSE
#$followers_count:int 1567
#$friends\u count:int 4019
#$listed\u计数:整数0
#$Favorites\u count:国际货币6669
#$statuses\u计数:整数9279
#$created_:chr“2018年10月11日星期四17:46:44+0000”
#$utc\u偏移量:空
#$时区:空
#$geo_已启用:logi FALSE
#$lang:chr“ar”
#$contributors\u已启用:logi FALSE
#$is_转换器:logi FALSE
#$profile\背景\颜色:chr“F5F8FA”
#$profile\u background\u image\u url:chr“”
#$profile\u background\u image\u url\u https:chr“”
#$profile\u background\u tile:logi FALSE
#$profile\u link\u颜色:chr“1DA1F2”
#$profile\u侧边栏\u边框\u颜色:chr“c0dect”
#$profile\u侧边栏\u填充\u颜色:chr“DDEEF6”
#$profile\u text\u颜色:chr“333333”
#$profile\u use\u background\u image:logi TRUE
#$profile\u image\u url:chr“http://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg"
#$profile\u image\u url\u https:chr“https://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg"
#$profile\u banner\u url:chr“https://pbs.twimg.com/profile_banners/1050442590754103297/1539390015"
#$default\u profile:logi TRUE
#$default\u profile\u image:logi FALSE
#$following:NULL
#$follow\u请求\u已发送:空
#$notifications:NULL
注:

  • 提醒:这里的空格是我的本地“locale”设置的问题,不一定是这里的过程(对不起,我不经常在控制台上处理非ASCII)
  • 大整数
    id
    字段被转换为
    numeric
    ,如果不使用
    bit64
    包,则不能成为整数,我不知道这是否会对您造成问题
  • 我试图在替换时“小心”,并对regex模式非常具体,寻找前面的冒号、无记号和可选的后跟逗号(因为最后一个
    no
    
    'id': 1050442590754103297    'id_str': '1050442590754103297'   'name': 'ام رودينا <U+267F>'
    
    'screen_name': 'uclkGkQ5', 'location': None, 'url': None, 'description': '\u200f\u200fمن زوي الاحتياجات الخاصه<U+267F>', 'translator_type': 'none', 'protected': False, 'verified': False, 'followers_count': 1567, 'friends_count': 4019, 'listed_count': 0, 'favourites_count': 6669, 'statuses_count': 9279, 'created_at': 'Thu Oct 11 17:46:44 +0000 2018', 'utc_offset': None, 'time_zone': None, 'geo_enabled': False, 'lang': 'ar', 'contributors_enabled': False, 'is_translator': False, 'profile_background_color': 'F5F8FA', 'profile_background_image_url': '', 'profile_background_image_url_https': '', 'profile_background_tile': False, 'profile_link_color': '1DA1F2', 'profile_sidebar_border_color': 'C0DEED', 'profile_sidebar_fill_color': 'DDEEF6', 'profile_text_color': '333333', 'profile_use_background_image': True, 'profile_image_url': 'http://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg', 'profile_image_url_https': 'https://pbs.twimg.com/profile_images/1059769079790268416/sJpep_V8_normal.jpg', 'profile_banner_url': 'https://pbs.twimg.com/profile_banners/1050442590754103297/1539390015', 'default_profile': True, 'default_profile_image': False, 'following': None, 'follow_request_sent': None, 'notifications': None
    
     id                         id_str                    name        screen_name     
     1050442590754103297      1050442590754103297       ام رودينا \u267f           uclkGkQ5