在 within() 中使用向量和 R 中的替换函数(也涉及 memisc。)
Using a vector in within() with a replacement function in R (also memisc is involved.)
所以我的标题很冗长主要是因为我实际上并不知道我在说什么。两天来,我一直在努力尝试使用 memisc 包,以便我可以区分数据中不同类型的 NA。 (旁注:这已经是我不满意的妥协,但实际上没有任何其他好的选择。如果你认为你有一个,我可以为此提出另一个单独的问题。)
跳过所有让我走到这一步的内容,这就是我们现在所处的位置。
#install.packages("memisc")
library(memisc)
df <- data.frame('a' = 1:4, 'b' = 2:5, 'c' = 3:6)
ds <- data.set(df)
descs <- c("This is a", "This is b", "This is c")
显然我的数据要大得多,否则我根本不会为此烦恼,但也许需要说明以防万一。
以下是我尝试给 'item' 描述时发生的情况:
有用的东西
ds <- within(ds, description(df.a) <- "test") # The way the package suggests
description(ds$df.a) == "test" # TRUE, as expected
description(ds$df.a) <- "test2" # Calling it with a name
description(ds$df.a) == "test2" # TRUE too
最终目标是让我的 "data.set" 中的所有 176 列都由向量中的 already-existing、long-winded 描述来描述。所以我需要让它接受一个向量或使用应用或以某种方式迭代它,我对我的选择感到满意。但是到目前为止,我为使替换函数 description()
以接受对 objects 的引用的方式工作所做的一切尝试都失败了。
不起作用的东西
description(ds[, 1]) <- "test" # Calling it by number doesn't wirj
description(ds$df.a) == "test" # FALSE
test_name <- "df.a"
ds <- within(ds, description(get(test_name)) <- "test") # No.
test_name <- quote("df.a")
ds <- within(ds, description(eval(test_name)) <- "test") # No.
无论我使用 get()
、quote()/eval()
还是一些类似的设置,我都会得到相同类型的错误:
> ds <- within(ds, description(get(test_name)) <- "test")
Error in description(get(test_name)) <- "test" :
could not find function "get<-"
所以我想我会很有创意,用两个值调用函数本身...
ds <- within(ds, 'description<-'(test_name, "test3"))
description(ds$df.a) == "test3" # FALSE
这也失败了,大概是因为 description()
是另一个函数(方法?)的包装器(我认为?),annotate()
,它具有相同的功能。
不是我的,obv,仅供参考
#### Description function ####
"description<-" <- function(x,value){
annotation(x)["description"] <- value
x
}
#### I'd paste the stupid method code, but Whosebug doesn't ####
#### think it's properly formatted as code when I do, so pfft. ####
真题
我怎么把我的 data.set、ds 和分配描述向量 descs 给合适的项目?
尝试:
for (i in seq_along(descs) ) description(ds[[i]]) <- descs[i]
所以我的标题很冗长主要是因为我实际上并不知道我在说什么。两天来,我一直在努力尝试使用 memisc 包,以便我可以区分数据中不同类型的 NA。 (旁注:这已经是我不满意的妥协,但实际上没有任何其他好的选择。如果你认为你有一个,我可以为此提出另一个单独的问题。)
跳过所有让我走到这一步的内容,这就是我们现在所处的位置。
#install.packages("memisc")
library(memisc)
df <- data.frame('a' = 1:4, 'b' = 2:5, 'c' = 3:6)
ds <- data.set(df)
descs <- c("This is a", "This is b", "This is c")
显然我的数据要大得多,否则我根本不会为此烦恼,但也许需要说明以防万一。
以下是我尝试给 'item' 描述时发生的情况:
有用的东西
ds <- within(ds, description(df.a) <- "test") # The way the package suggests
description(ds$df.a) == "test" # TRUE, as expected
description(ds$df.a) <- "test2" # Calling it with a name
description(ds$df.a) == "test2" # TRUE too
最终目标是让我的 "data.set" 中的所有 176 列都由向量中的 already-existing、long-winded 描述来描述。所以我需要让它接受一个向量或使用应用或以某种方式迭代它,我对我的选择感到满意。但是到目前为止,我为使替换函数 description()
以接受对 objects 的引用的方式工作所做的一切尝试都失败了。
不起作用的东西
description(ds[, 1]) <- "test" # Calling it by number doesn't wirj
description(ds$df.a) == "test" # FALSE
test_name <- "df.a"
ds <- within(ds, description(get(test_name)) <- "test") # No.
test_name <- quote("df.a")
ds <- within(ds, description(eval(test_name)) <- "test") # No.
无论我使用 get()
、quote()/eval()
还是一些类似的设置,我都会得到相同类型的错误:
> ds <- within(ds, description(get(test_name)) <- "test")
Error in description(get(test_name)) <- "test" :
could not find function "get<-"
所以我想我会很有创意,用两个值调用函数本身...
ds <- within(ds, 'description<-'(test_name, "test3"))
description(ds$df.a) == "test3" # FALSE
这也失败了,大概是因为 description()
是另一个函数(方法?)的包装器(我认为?),annotate()
,它具有相同的功能。
不是我的,obv,仅供参考
#### Description function ####
"description<-" <- function(x,value){
annotation(x)["description"] <- value
x
}
#### I'd paste the stupid method code, but Whosebug doesn't ####
#### think it's properly formatted as code when I do, so pfft. ####
真题
我怎么把我的 data.set、ds 和分配描述向量 descs 给合适的项目?
尝试:
for (i in seq_along(descs) ) description(ds[[i]]) <- descs[i]