I'm pretty new in R, trying to create a dataframe with several factors, one of them ("probability") needs to consist of a sequence of ascending values (30, 40, 50, 60, 70) 25 times, followed by a sequence of descending values (70, 60, 50, 50, 30) for 25 times.
here's what i have tried so far:
probability = c(seq(30, 70, 10, along_with=1:25), rev(seq(30, 70, 10, along_with=1:25))),
probability = rep(c(c(30, 40, 50, 60, 70), len=25), rep(c(70, 60, 50, 40, 30), len=25)),
probability = rep(c(c(30, 40, 50, 60, 70), c(70, 60, 50, 40, 30), each=25)),
I would appreciate any help from you guys...
Thanks!
CodePudding user response:
There where a problem in parenthesis, adding a wrong element 25 in your last line.
You can do: rep(c(seq(30, 70, 10), seq(70, 30, -10)), each = 25)
CodePudding user response:
I think you need this:
probability_ascending<-rep(c(30, 40, 50, 60, 70),25)
probability_descending<-rep(c(70, 60, 50, 40, 30),25)
If you want it in one vector:
probability<-c(rep(c(30, 40, 50, 60, 70),25),rep(c(70, 60, 50, 40, 30),25))
CodePudding user response:
Here is a step by step answer to get at my comment to the question.
1. These are the sequences to be repeated.
# first sequence
seq(30, 70, 10)
#> [1] 30 40 50 60 70
# second sequence
seq(70, 30, -10)
#> [1] 70 60 50 40 30
Created on 2022-07-12 by the reprex package (v2.0.1)
2. Combine them into one vector.
# combine the sequences above
c(seq(30, 70, 10), seq(70, 30, -10))
#> [1] 30 40 50 60 70 70 60 50 40 30
Created on 2022-07-12 by the reprex package (v2.0.1)
3. Repeat the result above 25 times.
# combine the sequences above
rep(c(seq(30, 70, 10), seq(70, 30, -10)), 25)
#> [1] 30 40 50 60 70 70 60 50 40 30
#> [11] 30 40 50 60 70 70 60 50 40 30
#> [21] 30 40 50 60 70 70 60 50 40 30
#> [31] 30 40 50 60 70 70 60 50 40 30
#> [41] 30 40 50 60 70 70 60 50 40 30
#> [51] 30 40 50 60 70 70 60 50 40 30
#> [61] 30 40 50 60 70 70 60 50 40 30
#> [71] 30 40 50 60 70 70 60 50 40 30
#> [81] 30 40 50 60 70 70 60 50 40 30
#> [91] 30 40 50 60 70 70 60 50 40 30
#> [101] 30 40 50 60 70 70 60 50 40 30
#> [111] 30 40 50 60 70 70 60 50 40 30
#> [121] 30 40 50 60 70 70 60 50 40 30
#> [131] 30 40 50 60 70 70 60 50 40 30
#> [141] 30 40 50 60 70 70 60 50 40 30
#> [151] 30 40 50 60 70 70 60 50 40 30
#> [161] 30 40 50 60 70 70 60 50 40 30
#> [171] 30 40 50 60 70 70 60 50 40 30
#> [181] 30 40 50 60 70 70 60 50 40 30
#> [191] 30 40 50 60 70 70 60 50 40 30
#> [201] 30 40 50 60 70 70 60 50 40 30
#> [211] 30 40 50 60 70 70 60 50 40 30
#> [221] 30 40 50 60 70 70 60 50 40 30
#> [231] 30 40 50 60 70 70 60 50 40 30
#> [241] 30 40 50 60 70 70 60 50 40 30
Created on 2022-07-12 by the reprex package (v2.0.1)