楼主: verbatim
2205 4

[求助]Help please. thank you. [推广有奖]

  • 1关注
  • 3粉丝

博士生

5%

还不是VIP/贵宾

-

威望
0
论坛币
513 个
通用积分
2.0505
学术水平
0 点
热心指数
2 点
信用等级
0 点
经验
3404 点
帖子
139
精华
0
在线时间
143 小时
注册时间
2006-12-1
最后登录
2020-1-30

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

dear All:

I have a following problem:

A=c(0,24,3,4,5,0,2,6,7,0,25,5,7,8)

I want combine A(add new column) with the following dataset (1,1,1,1,1,2,2,2,2,3,3,3,3,3) which need to be generated based on A.

How to generate this column BASED ON A?

I appreciate!

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Thank you please Lease Thank Help 求助 Help please Thank

回帖推荐

oxhuangfei 发表于2楼  查看完整内容

countA<-c(which(A==0),length(A))output<-rep(0,length(A)) for(i in seq(length(countA)-1)){ curr<-rep(i,countA-countA);output[countA:countA]<-curr;}the output is the column you wanted.

chitchatla 发表于5楼  查看完整内容

a shorter one: holder<-rep(0, length(A))holder[which(A==0)]<-1output<-cumsum(holder)> output [1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3
沙发
oxhuangfei 发表于 2007-8-29 08:05:00 |只看作者 |坛友微信交流群

回复:(verbatim)[求助]Help please. thank you.

countA<-c(which(A==0),length(A))
output<-rep(0,length(A))

for(i in seq(length(countA)-1))
{
curr<-rep(i,countA[i+1]-countA);
output[countA:countA[i+1]]<-curr;
}

the output is the column you wanted.



已有 1 人评分论坛币 学术水平 热心指数 收起 理由
crystal8832 + 10 + 1 + 1 热心帮助其他会员

总评分: 论坛币 + 10  学术水平 + 1  热心指数 + 1   查看全部评分

使用道具

藤椅
lyrasover 发表于 2007-8-29 10:14:00 |只看作者 |坛友微信交流群

不错啊

使用道具

板凳
verbatim 发表于 2007-8-30 00:21:00 |只看作者 |坛友微信交流群

[讨论]

Warning messages:
1: Replacement length not a multiple of number of elements to replace in: output[countA:countA[i + 1]] <- curr
2: Replacement length not a multiple of number of elements to replace in: output[countA:countA[i + 1]] <- curr
3: Replacement length not a multiple of number of elements to replace in: output[countA:countA[i + 1]] <- curr
Did you get this message? Thank you for your help.

使用道具

报纸
chitchatla 发表于 2007-8-31 23:27:00 |只看作者 |坛友微信交流群

a shorter one:

holder<-rep(0, length(A))
holder[which(A==0)]<-1
output<-cumsum(holder)
> output
[1] 1 1 1 1 1 2 2 2 2 3 3 3 3 3

已有 1 人评分论坛币 学术水平 热心指数 收起 理由
crystal8832 + 10 + 1 + 1 热心帮助其他会员

总评分: 论坛币 + 10  学术水平 + 1  热心指数 + 1   查看全部评分

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-27 18:21