全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
2418 4
2007-08-29

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!

二维码

扫码加我 拉你入群

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

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

全部回复
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.



二维码

扫码加我 拉你入群

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

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

2007-8-29 10:14:00

不错啊

二维码

扫码加我 拉你入群

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

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

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.
二维码

扫码加我 拉你入群

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

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

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

二维码

扫码加我 拉你入群

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

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

相关推荐
栏目导航
热门文章
推荐文章

分享

扫码加好友,拉您进群