Skip to content

Commit a128c0c

Browse files
committed
fix of the rep function
1 parent 57660b8 commit a128c0c

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

R#/Runtime/Internal/internalInvokes/base.vb

+19-5
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,11 @@ Namespace Runtime.Internal.Invokes
893893
''' a POSIXct or POSIXlt or Date object; or an S4 object containing such
894894
''' an object.
895895
''' </param>
896+
''' <param name="each">
897+
''' non-negative integer. Each element of x is repeated each times. Other
898+
''' inputs will be coerced to an integer or double vector and the first
899+
''' element taken. Treated as 1 if NA or invalid.
900+
''' </param>
896901
''' <param name="times">an integer-valued vector giving the (non-negative)
897902
''' number of times to repeat each element if of length length(x), or to
898903
''' repeat the whole vector if of length 1. Negative or NA values are an
@@ -902,15 +907,24 @@ Namespace Runtime.Internal.Invokes
902907
<ExportAPI("rep")>
903908
Public Function rep(<RRawVectorArgument>
904909
x As Object,
905-
times As Integer,
910+
Optional times As Integer? = Nothing,
911+
Optional [each] As Integer? = Nothing,
906912
Optional env As Environment = Nothing) As Object
907913

908914
Dim out As New List(Of Object)
909-
Dim vx = REnv.asVector(Of Object)(x)
915+
Dim vx = REnv.asVector(Of Object)(x).AsObjectEnumerator.ToArray
910916

911-
For i As Integer = 1 To times
912-
out.AddRange(vx.AsObjectEnumerator)
913-
Next
917+
If Not times Is Nothing Then
918+
For i As Integer = 1 To times
919+
Call out.AddRange(vx)
920+
Next
921+
ElseIf Not [each] Is Nothing Then
922+
For Each item As Object In vx
923+
Call out.AddRange(Repeats(item, times:=[each]))
924+
Next
925+
Else
926+
Return Internal.debug.stop("the repeats `times` or `each` element repeats times parameter must be specific!", env)
927+
End If
914928

915929
Return REnv.TryCastGenericArray(out.ToArray, env)
916930
End Function

test/demo/math/moments.R

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require(ggplot);
2+
3+
let center = as.data.frame( hist( rnorm(1000,0,1), n=10));
4+
let right =as.data.frame( hist( rexp(1000, 1), n=10));
5+
let left = as.data.frame( hist( -rexp(1000, 1), n=10));
6+
7+
print(center);
8+
print(left);
9+
print(right);
10+
11+
stop();
12+
13+
bitmap(file = file.path(@dir,"moments.png")) {
14+
ggplot(dataset , aes(x ="x"))
15+
+ geom_point(aes(y = "center"))
16+
+ geom_point(aes(y = "left"))
17+
+ geom_point(aes(y = "right"))
18+
19+
}

test/demo/math/moments.png

Loading

0 commit comments

Comments
 (0)