i want to store additional information on top of a data.frame and return it from a function. as you can see – the additional data disappears.
example :
> d<-data.frame(N1=c(1,2,3),N2=(LETTERS[1:3]))
> d
N1 N2
1 1 A
2 2 B
3 3 C
> d.x = 3
> d
N1 N2
1 1 A
2 2 B
3 3 C
> d.x
[1] 3
> foo1 <- function() {
+ d<-data.frame(N1=c(1,2,3),N2=(LETTERS[1:3]))
+ d.x=3
+ return(d)
+ }
>
> d1<-foo1()
> d1
N1 N2
1 1 A
2 2 B
3 3 C
> d1.x
Error: object 'd1.x' not found
i looked into assign but since the data.frame is created inside the function and being returned i assume that it’s not relevant here.
Thanks.
Your comments suggest you want to create an attribute (the usual way to attach “metadata” to objects in R) named “d.3” and use foo1 to set that attribute for a dataframe:
See
?attrand?attributesfor more specifics. There is also acommentsfunction.