Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

r - Generating two sequences with one function and one parameter

I want to generate these two sequences:

A = c(0.25,0.50,0.75,1,0.25,0.50,0.75,0.25,0.50,0.25)
B = c(0.33,0.66,1,0.33,0.66,0.33)

with one function. I already have this:

X = 5
X = 4
rep(seq(1/(X-1),1,1/(X-1)), X-1)

but, I still need to remove some values, I did it like this, but that is not really the right way:

rep(seq(1/(X-1),1,1/(X-1)), X-1)[-c(8,11,12,14,15,16)]
rep(seq(1/(X-1),1,1/(X-1)), X-1)[-c(6,8,9)]

Is there a way to write this in a single function?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

One way to do this is by sequence(x:1)/x, with x taking the required value.

> f = function(x) sequence(x:1)/x
> f(4)
# [1] 0.25 0.50 0.75 1.00 0.25 0.50 0.75 0.25 0.50 0.25
> f(3)
# [1] 0.3333333 0.6666667 1.0000000 0.3333333 0.6666667 0.3333333

(This is assuming that sequence counts as one function i.e. and we don't include : and / etc.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...