Commenting on multiple objects at once

Suppose you have a list of variables a through j

for(x in 1:10) {
assign(letters[x],x)
} 

How can you comment on these newly created objects? I tried something like:

for(x in 1:10) { 
comment(get(letters[x])) <- paste(x)
} 

But that seems to fail. Via:

Error in comment(get(letters[x])) <- paste(x) : 
  could not find function "get<-"

What am I missing here?

+3
source share
4 answers

If you ever want them to sort through things, keep them on the list. It makes life easier. But if you really want to do this, you may need to loop through eval (parse (thingum:

> for(i in 1:10){
+  eval(parse(text=paste("comment(",letters[i],")<-'",as.character(i*2),"'",sep="")))
+ }
+4
source

You cannot assign a value to a variable returned , see

> x <- 'cars'
> get(x) <- 1
Error in get(x) <- 1 : could not find function "get<-"

But reading / loading the comment of the returned variable is possible with get, see

> comment(cars) <- "test"
> comment(get(x))
[1] "test"

, . , :

> l <- list(a=1,b=2,c=3)
> for (x in 1:3) {
+     comment(l[[letters[x]]]) <- paste(x)
+ }
> str(l)
List of 3
 $ a: atomic [1:1] 1
  ..- attr(*, "comment")= chr "1"
 $ b: atomic [1:1] 2
  ..- attr(*, "comment")= chr "2"
 $ c: atomic [1:1] 3
  ..- attr(*, "comment")= chr "3"

, , :

> attach(l)
The following object(s) are masked _by_ '.GlobalEnv':

    a, b, c
> a
[1] 1
> str(a)
 atomic [1:1] 1
 - attr(*, "comment")= chr "1"
+2

:

# sample data
letters <- setNames(as.list(1:10), LETTERS[1:10])

for(i in 1:10) {
   temp <- letters[[i]]
   comment(temp) <- paste(i)
   letters[[i]] <- temp
}
+2

get() . .

> for(x in 1:10) {
+     assign(letters[x],x)
+ }
>
> a
[1] 1
> comment(a)
NULL
>
> for(x in 1:10) { 
+     obj <- get(letters[x])
+     comment(obj) <- paste(x)
+     assign(letters[x], obj)
+ }
> ## cleanup
> rm(obj)
> a
[1] 1
> comment(a)
[1] "1"

, , , get(), get<-().

, , , , , .

+2

All Articles