forked from rdotnet/rdotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataFrameRow.cs
152 lines (141 loc) · 4.91 KB
/
DataFrameRow.cs
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Dynamic;
namespace RDotNet
{
/// <summary>
/// A data frame row.
/// </summary>
public class DataFrameRow : DynamicObject
{
private DataFrame frame;
private int rowIndex;
/// <summary>
/// Creates a new object representing a data frame row
/// </summary>
/// <param name="frame">R Data frame</param>
/// <param name="rowIndex">zero-based row index</param>
public DataFrameRow(DataFrame frame, int rowIndex)
{
this.frame = frame;
this.rowIndex = rowIndex;
}
/// <summary>
/// Gets and sets the value at the specified column.
/// </summary>
/// <param name="index">The zero-based column index.</param>
/// <returns>The value.</returns>
public object this[int index]
{
get
{
DynamicVector column = DataFrame[index];
return column[RowIndex];
}
set
{
DynamicVector column = DataFrame[index];
column[RowIndex] = value;
}
}
/// <summary>
/// Gets the inner representation of the value; an integer if the column is a factor
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
internal object GetInnerValue(int index)
{
DynamicVector column = DataFrame[index];
if (column.IsFactor())
return column.AsInteger()[RowIndex];
else
return column[RowIndex];
}
/// <summary>
/// Sets the inner representation of the value; an integer if the column is a factor
/// </summary>
/// <param name="index"></param>
/// <param name="value"></param>
internal void SetInnerValue(int index, object value)
{
DynamicVector column = DataFrame[index];
if (column.IsFactor())
column.AsInteger()[RowIndex] = (int)value;
else
column[RowIndex] = value;
}
/// <summary>
/// Gets and sets the value at the specified column.
/// </summary>
/// <param name="name">The column name.</param>
/// <returns>The value.</returns>
public object this[string name]
{
get
{
DynamicVector column = DataFrame[name];
return column[RowIndex];
}
set
{
DynamicVector column = DataFrame[name];
column[RowIndex] = value;
}
}
/// <summary>
/// Gets the data frame containing this row.
/// </summary>
public DataFrame DataFrame
{
get { return this.frame; }
}
/// <summary>
/// Gets the index of this row.
/// </summary>
public int RowIndex
{
get { return this.rowIndex; }
}
/// <summary>
/// Gets the column names of the data frame.
/// </summary>
/// <returns></returns>
public override IEnumerable<string> GetDynamicMemberNames()
{
return DataFrame.ColumnNames;
}
/// <summary>
/// Try to get a member to a specified value
/// </summary>
/// <param name="binder">Dynamic get member operation at the call site; Binder whose name should be one of the data frame column name</param>
/// <param name="result">The value of the member</param>
/// <returns>false if setting failed</returns>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
string[] columnNames = DataFrame.ColumnNames;
if (columnNames == null || Array.IndexOf(columnNames, binder.Name) < 0)
{
result = null;
return false;
}
result = this[binder.Name];
return true;
}
/// <summary>
/// Try to set a member to a specified value
/// </summary>
/// <param name="binder">Dynamic set member operation at the call site; Binder whose name should be one of the data frame column name</param>
/// <param name="value">The value to set</param>
/// <returns>false if setting failed</returns>
public override bool TrySetMember(SetMemberBinder binder, object value)
{
string[] columnNames = DataFrame.ColumnNames;
if (columnNames == null || Array.IndexOf(columnNames, binder.Name) < 0)
{
return false;
}
this[binder.Name] = value;
return true;
}
}
}