ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataFrame.C
Go to the documentation of this file.
1 #include<TRInterface.h>
2 
3 using namespace ROOT::R;
4 void DataFrame()
5 {
6 ////////////////////////
7 //creating variables//
8 ////////////////////////
9 TVectorD v1(3);
10 std::vector<Double_t> v2(3);
11 std::array<Int_t,3> v3{ {1,2,3} };
12 std::list<std::string> names;
13 
14 //////////////////////
15 //assigning values//
16 //////////////////////
17 v1[0]=1;
18 v1[1]=2;
19 v1[2]=3;
20 
21 v2[0]=0.101;
22 v2[1]=0.202;
23 v2[2]=0.303;
24 
25 names.push_back("v1");
26 names.push_back("v2");
27 names.push_back("v3");
28 
30 
31 /////////////////////////////////////////////
32 //creating dataframe object with its labels//
33 /////////////////////////////////////////////
34 
35 TRDataFrame df1(Label["var1"]=v1,Label["var2"]=v2,Label["var3"]=v3,Label["strings"]=names);
36 
37 //////////////////////////////////////////////
38 //Passing dataframe to R's environment//
39 //////////////////////////////////////////////
40 
41 r["df1"]<<df1;
42 r<<"print(df1)";
43 
44 ////////////////////////////////
45 //Adding colunms to dataframe //
46 ////////////////////////////////
47 
48 TVectorD v4(3);
49 //filling the vector fro R's environment
50 r["c(-1,-2,-3)"]>>v4;
51 //adding new colunm to df1 with name var4
52 df1["var4"]=v4;
53 //updating df1 in R's environment
54 r["df1"]<<df1;
55 //printing df1
56 r<<"print(df1)";
57 
58 
59 //////////////////////////////////////////
60 //Getting dataframe from R's environment//
61 //////////////////////////////////////////
62 TRDataFrame df2;
63 
64 r<<"df2<-data.frame(v1=c(0.1,0.2,0.3),v2=c(3,2,1))";
65 r["df2"]>>df2;
66 
67 TVectorD v(3);
68 df2["v1"]>>v;
69 v.Print();
70 
71 df2["v2"]>>v;
72 v.Print();
73 
74 
75 
76 ///////////////////////////////////////////
77 //Working with colunms between dataframes//
78 ///////////////////////////////////////////
79 
80 df2["v3"]<<df1["strings"];
81 
82 //updating df2 in R's environment
83 r["df2"]<<df2;
84 r<<"print(df2)";
85 
86 //passing values from colunm v3 of df2 to var1 of df1
87 df2["v3"]>>df1["var1"];
88 //updating df1 in R's environment
89 r["df1"]<<df1;
90 r<<"print(df1)";
91 
92 }
const Double_t * v1
Definition: TArcBall.cxx:33
ROOT::R::TRInterface & r
Definition: Object.C:4
SVector< double, 2 > v
Definition: Dict.h:5
void DataFrame()
Definition: DataFrame.C:9
static TRInterface & Instance()
static method to get an TRInterface instance reference
Rcpp::internal::NamedPlaceHolder Label
Definition: RExports.cxx:14
TRandom3 R
a TMatrixD.
Definition: testIO.cxx:28
This is a class to create DataFrames from ROOT to R
Definition: TRDataFrame.h:183