Ask Your Question

# R coding , packages ?

Hi

I would like to start in R language. is there way to access the function x = rbinom (1000,1, .5) or dbinom(46:54, 100, 0.5) I suppose I must include a package ?

"%r"
a = r([10.4,5.6,3.1,6.4,21.7]); a
sum(a)
x = rbinom(1000,1,.5)
total = sum(x)


NameError Traceback (most recent call last) <ipython-input-7-529997c59866> in <module>() 2 a = r([RealNumber('10.4'),RealNumber('5.6'),RealNumber('3.1'),RealNumber('6.4'),RealNumber('21.7')]); a 3 sum(a) ----> 4 x = rbinom(Integer(1000),Integer(1),RealNumber('.5')) 5 total = sum(x) NameError: name 'rbinom' is not defined**

edit retag close merge delete

## 1 Answer

Sort by ยป oldest newest most voted

Not every R function is sent to the Sage namespace, in particular rbinom is not available directly, you have to specify that wou want to use this function from the r interface by typing r.rbinom instead:

sage: x = r.rbinom(1000,1,.5)
sage: x
[1] 0 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 0 1 1 1 0 0 0 1 0 0 1 1 1 1 0 1 1
[48] 0 1 0 1 0 1 0 1 0 1 0 0 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 0 0 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1
[95] 1 0 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 1 0 1 1 0 1 0 1 1 1 1 0 1 0 0 0 0 1 1 0 0 0 0 0 1 1 0 1 0 1
[142] 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 0 1 1 1 0 0 0 0 1 1 1 1 1 1 0 1 1 1 0 1 1 0 0 0
[189] 0 1 1 0 1 1 1 1 0 1 1 0 0 1 1 0 1 1 1 1 1 0 0 1 0 1 1 0 0 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 1 0 1
[236] 0 1 1 1 1 0 0 1 1 0 1 1 1 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 0 0 1 0 1 1 1 1
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0 1 0 0 1 0 0 1 0 1 1 0 0 1 0 1 1 0 1 1 0 1 0 1 0 1 0
[800] 1 1 0 1 0 0 0 1 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 1 0 1 1 0 0 0 1 1 1 0 1 1 0 1 0 0 1 1 0
[847] 1 0 1 0 1 1 1 1 1 1 1 0 1 1 0 0 1 1 0 1 1 1 0 0 0 1 0 0 1 1 0 1 1 1 1 1 0 1 1 1 0 0 1 1 1 0 1
[894] 1 1 1 1 1 1 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 0 0 0
[941] 1 1 1 1 0 1 0 1 1 1 0 1 0 1 1 1 0 0 1 1 1 0 1 0 1 0 0 1 1 1 0 1 1 1 0 1 1 0 1 0 1 1 1 1 1 1 0
[988] 1 1 0 0 0 0 1 0 0 0 1 1 1
sage: sum(x)
[1] 508


If you want to use it many times and do not want to use the r.rbinom, you can always do:

sage: rbinom=r.rbinom

more

## Comments

thank you tmonteil.

( 2018-01-01 13:30:22 +0200 )edit

but it seems r.sin(30:87) is not accepted ?

m=r.mean(a);m
10*r.mean(r.sin(30:87))

( 2018-01-01 13:45:40 +0200 )edit

The Sage/Python interpreter tries to understand 30:87 which does not makes sense in Python, so either you have to turn it into something Pythonic:

sage: r.sin([30..87])


or you have to pass it raw (as a string) to r:

sage: r.sin(r('30:87'))

( 2018-01-01 14:30:11 +0200 )edit

Ok thanks a lot tmonteil

just for info: R TRUE must be replaced by True, example:

"%r"
x = r('1:5');x
r.sample(x,3)
#r.sample(r('x,12,replace=TRUE')) # does not work
#r.sample(r('x,12,replace=True')) # does not work
r.sample(x,12,replace=True) # OK

#  Now let's roll the die 5000 times and see what fraction of the rolls give 6. We expect that about 1/6 of them will be.
# Simulate 1000 rolls
x = r.sample(r('1:6'),1000, replace=True); x

# x == 6 gives a vector of TRUE or FALSE. R is clever, when we sum the vector: each TRUE counts as 1 and each FALSE counts as 0. So the sum is the number of TRUE's. In this case that means the number of 6's, which happens to be 168.
r.sum(x == 6)


link text

( 2018-01-01 16:53:36 +0200 )edit

"%r" # Goal: estimate the probability of getting a sum of 7 when rolling two dice.

ntrials = 10000

x = r.matrix(r.sample(r('1:6'), 2*ntrials, replace=True), nrow=2, ncol=ntrials)

y = r.colSums(x)

r.mean(y == 7)


source: MIT Tutorial R coding

( 2018-01-04 14:51:28 +0200 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2018-01-01 12:35:39 +0200

Seen: 263 times

Last updated: Jan 01 '18