使用in()中的向量和R中的替换函数(也涉及memisc)

使用in()中的向量和R中的替换函数(也涉及memisc),r,packages,R,Packages,所以我的标题很冗长,主要是因为我不知道我在说什么。两天来,我一直在努力使用该软件包,以便能够区分数据中不同类型的NAs。(旁注:这已经是我不满意的折衷方案,但实际上没有其他好的选择。如果你认为你有,我可以就此提出另一个问题。) 跳过所有让我想到这一点的东西,我们现在就在这里 #install.packages("memisc") library(memisc) df <- data.frame('a' = 1:4, 'b' = 2:5, 'c' = 3:6) ds <- data.s

所以我的标题很冗长,主要是因为我不知道我在说什么。两天来,我一直在努力使用该软件包,以便能够区分数据中不同类型的NAs。(旁注:这已经是我不满意的折衷方案,但实际上没有其他好的选择。如果你认为你有,我可以就此提出另一个问题。)

跳过所有让我想到这一点的东西,我们现在就在这里

#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")
无论我使用
get()
quote()/eval()
还是类似的设置,我都会得到相同类型的错误:

> ds <- within(ds, description(get(test_name)) <- "test")
Error in description(get(test_name)) <- "test" : 
  could not find function "get<-"
实际问题 我该如何获取我的data.set ds,并将描述向量descs分配给相应的项目?

试试:


for(i in seq_-along(descs))description(ds[[i]])我不完全确定为什么您需要在
中使用
(也许在包文档中有一个很好的解释),但我不明白为什么在这种情况下,您不能只对
循环使用
,而对(i in seq_-along(descs))description(ds[[i]])使用
,这是完美的,@SimonO'Hanlon。首先,我用
ds[,I]
尝试了这一点,但它不起作用,现在仍然不起作用。我这里缺少什么?我猜是
memisc
数据集
类不支持通过
[,
索引访问列,而必须使用
列表
索引,即
[]
。好吧,非常感谢。我想我以后会一直尝试这两种形式。:)添加了一个答案,以便可以关闭此对话框。
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.
> 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 function ####
"description<-" <- function(x,value){
  annotation(x)["description"] <- value
  x
}

#### I'd paste the stupid method code, but stackoverflow  doesn't ####
#### think it's properly formatted as code when I do, so pfft.    ####