设为首页收藏本站获取积分奖励

R语言中文网

 找回密码
 立即注册
查看: 2072|回复: 1

[问题求助] 有一长串由0和1组成的向量,如何进行如下的操作

[复制链接]
发表于 2017-9-2 20:09:31 | 显示全部楼层 |阅读模式
100101001010111110101111111000000010101010101001010001010101111111
变成
1
00
1
0
1
00
1
0
1
0
11111
0
1
0
1111111
0000000
1
0
1
0
1
0
1
0
1
0
1
00
1
0
1
000
1
0
1
0
1
0
1111111
回复

使用道具 举报

发表于 2017-9-5 14:01:58 | 显示全部楼层
提供一个笨办法,如下代码:
source_txt="100101001010111110101111111000000010101010101001010001010101111111"
first_pos = end_pos = 0
i=1
return_vector=""
ch=""
while (i < nchar(source_txt))
{
   ch=substr(source_txt,i,i)
   first_pos = i
   end_pos = i
   while (ch == substr(source_txt,end_pos+1,end_pos+1))
   {
      end_pos = end_pos + 1
   }
   return_vector=c(return_vector,substr(source_txt,first_pos,end_pos))
   i = end_pos+1
}
return_vector=return_vector[-1]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|Archiver|R语言中文网    

GMT+8, 2024-5-20 19:00 , Processed in 0.028370 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表