-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpy-exemplo-basico.py
82 lines (61 loc) · 1.63 KB
/
rpy-exemplo-basico.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
import rpy2.robjects as robjects
from rpy2.robjects import r
pi = robjects.r['pi']
pi[0]
pi.r_repr()
# pi é o objeto do R
# pi[0] é o valor
# >>> pi
# >>> <FloatVector - Python:0x101ae2710 / R:0x1039724e8>
# >>> [3.141593]
robjects.r.ls(globalenv)
robjects.globalenv["a"] = 123
print(robjects.r.ls(globalenv))
robjects.r.rm("a")
print(robjects.r.ls(globalenv))
from rpy2.robjects import IntVector
from rpy2.robjects import StrVector
from rpy2.robjects import FloatVector
a = IntVector([10])
b = IntVector([15])
print a[0] + b[0]
strings = robjects.StrVector(['abc', 'def'])
integers = robjects.IntVector([1, 2, 3])
floats = robjects.FloatVector([1.1, 2.2, 3.3])
valores = IntVector([6, 7, 4, 3, 2, 0, 0, 6])
valores[4] # importante notar que python começa de 0
valores[3] # e o R começa de 1
len(valores)
max(valores)
min(valores)
import ipdb; ipdb.set_trace()
print r.sum(valores)[0]
print r.prod(valores)[0]
print r.sort(valores)
print r.mean(valores)[0]
print r.median(valores)[0]
print r.sd(valores)[0]
print r.var(valores)[0]
valores_python = list(valores)
he = IntVector([10, 2, 23, 11, 14, 35, 46, 32, 13, 51, 27, 49])
ha = he
print r.var(he)[0]
print r.cov(ha, he)[0]
print r.cor(ha, he)[0]
# funções
sqr = robjects.r('function(x) x^2')
print(sqr)
print(sqr(2))
print(sqr(IntVector([4])))
print(sqr(IntVector([4,4])))
eleva3 = robjects.r('function(a){ return(a*a*a); }')
print(eleva3)
print(eleva3(2))
print(eleva3(IntVector([4])))
print(eleva3(IntVector([4,4])))
# utilitários
r.getwd()
r.setwd("c:/docs/mydir") # lançam exceções de python
r.dir() # Lista arquivos do cwd.
import ipdb; ipdb.set_trace()