使用某些数据标准时,使用多种方法查看data.frame的列会更简单.作为一个具体的例子,当使用SDTM数据进行临床试验时,每种数据类型(如实验室或生命体征)都有一列用于实验室的名为"LBTPT"的时间点和用于生命体征的"VSTPT".在加载数据时,理想情况下,我希望能够将该列称为"LBTPT"或"TPT".
具体来说,我想找到一种方法来完成以下工作:
d <- data.frame(LBTPT=1:3)
d <- alias_column(d, TPT="LBTPT")
d$TPT == d$LBTPT
Run Code Online (Sandbox Code Playgroud)
但是,我希望数据只存储一次 - 它只是一个别名而不是副本.
而且,对于加分,就在"做什么,我的意思是"方式与像功能进行交互时工作merge,names<-,bind_rows,等.
我的感觉是你可以通过使用R6和主动绑定来做到这一点.
考虑到这一点,我们可以创建一个示例.在这里,我们创建了两个iris数据集视图,我们使用两个不同的列名访问同一列.通过列名更改将更新共享的私有虹膜数据集.
我是R6的粉丝,因为它提供了一种维护(在这种情况下)数据帧引用语义的方法,同时允许多种方式来引用数据集.
NB.我希望这能指出你正确的方向.
require(R6)
data(iris)
dataframe_factory <- R6Class(
"dataframe_factory",
portable = FALSE,
lock_objects = FALSE,
private = list(
..iris_data = iris
),
active = list(
# add the binding here
Sepal.Length = function(x, ...) {
if ( missing(x) ) {
private$..iris_data$Sepal.Length
} else {
private$..iris_data$Sepal.Length[...] <<- x
}
},
another.Sepal.Length = function(x, ...) {
if ( missing(x) ) {
private$..iris_data$Sepal.Length
} else {
private$..iris_data$Sepal.Length[...] <<- x
}
}
)
)
# Create the DataFrame
my_Dataframe <- dataframe_factory$new()
# Retrieve the alias
my_Dataframe$Sepal.Length
my_Dataframe$another.Sepal.Length
my_Dataframe$Sepal.Length[1] <- 5
my_Dataframe$Sepal.Length[1]
my_Dataframe$another.Sepal.Length[2] <- 8
my_Dataframe$another.Sepal.Length[2]
my_Dataframe$Sepal.Length
my_Dataframe$another.Sepal.Length
head(my_Dataframe$Sepal.Length,2)
my_Dataframe$Sepal.Length[1:2]
identical(my_Dataframe$Sepal.Length, my_Dataframe$another.Sepal.Length)
identical(my_Dataframe$Sepal.Length[1], my_Dataframe$another.Sepal.Length[1])
identical(my_Dataframe$Sepal.Length[1:2], my_Dataframe$another.Sepal.Length[1:2])
Run Code Online (Sandbox Code Playgroud)
> require(R6)
> data(iris)
> dataframe_factory <- R6Class(
+ "dataframe_factory",
+ portable = FALSE,
+ lock_objects = FALSE,
+ private = list(
+ ..iris_data = iris
.... [TRUNCATED]
> # Create the DataFrame
> my_Dataframe <- dataframe_factory$new()
> # Retrieve the alias
> my_Dataframe$Sepal.Length
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1
[21] 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1
[41] 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2
[61] 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7
[81] 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7
[101] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0
[121] 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9
[141] 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
> my_Dataframe$another.Sepal.Length
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1
[21] 5.4 5.1 4.6 5.1 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 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7
[101] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0
[121] 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9
[141] 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
> my_Dataframe$another.Sepal.Length
[1] 5.0 8.0 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1
[21] 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1
[41] 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2
[61] 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7
[81] 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7
[101] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0
[121] 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9
[141] 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
> head(my_Dataframe$Sepal.Length,2)
[1] 5 8
> my_Dataframe$Sepal.Length[1:2]
[1] 5 8
> identical(my_Dataframe$Sepal.Length, my_Dataframe$another.Sepal.Length)
[1] TRUE
> identical(my_Dataframe$Sepal.Length[1], my_Dataframe$another.Sepal.Length[1])
[1] TRUE
> identical(my_Dataframe$Sepal.Length[1:2], my_Dataframe$another.Sepal.Length[1:2])
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1549 次 |
| 最近记录: |