Logo ROOT   6.14/05
Reference Guide
DataFrame.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_r
3 /// \notebook -nodraw
4 ///
5 /// \macro_code
6 ///
7 /// \author
8 
9 void DataFrame()
10 {
11  using namespace ROOT::R;
12  // Creating variables
13  TVectorD v1(3);
14  std::vector<Double_t> v2 {0.101, 0.202, 0.303};
15  std::array<Int_t,3> v3{ {1,2,3} };
16  std::list<std::string> names {"v1", "v2", "v3"};
17 
18  // Assigning values
19  v1[0]=1;
20  v1[1]=2;
21  v1[2]=3;
22 
23  auto &r = TRInterface::Instance();
24 
25  // Creating dataframe object with its labels
26 
27  TRDataFrame df1(Label["var1"]=v1,Label["var2"]=v2,Label["var3"]=v3,Label["strings"]=names);
28 
29  // Passing dataframe to R's environment
30 
31  r["df1"]<<df1;
32  r<<"print(df1)";
33 
34  // Adding colunms to dataframe
35 
36  TVectorD v4(3);
37  //filling the vector fro R's environment
38  r["c(-1,-2,-3)"]>>v4;
39  //adding new colunm to df1 with name var4
40  df1["var4"]=v4;
41  //updating df1 in R's environment
42  r["df1"]<<df1;
43  //printing df1
44  r<<"print(df1)";
45 
46  // Getting dataframe from R's environment
47 
48  TRDataFrame df2;
49 
50  r<<"df2<-data.frame(v1=c(0.1,0.2,0.3),v2=c(3,2,1))";
51  r["df2"]>>df2;
52 
53  TVectorD v(3);
54  df2["v1"]>>v;
55  v.Print();
56 
57  df2["v2"]>>v;
58  v.Print();
59 
60  // Working with colunms between dataframes
61 
62  df2["v3"]<<df1["strings"];
63 
64  //updating df2 in R's environment
65  r["df2"]<<df2;
66  r<<"print(df2)";
67 
68  // Passing values from colunm v3 of df2 to var1 of df1
69  df2["v3"]>>df1["var1"];
70 
71  // Updating df1 in R's environment
72  r["df1"]<<df1;
73  r<<"print(df1)";
74 }
namespace associated R package for ROOT.
Definition: RExports.h:56
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
This is a class to create DataFrames from ROOT to R
Definition: TRDataFrame.h:177