Carl
Carl

Reputation: 608

Access data frames from a list of dataframes/construction of a dynamic variable to do so

Here is my code:

library(scales)  
library(zoo)  
library(ggplot2)  

f <- function (z) {  
zz <- read.zoo(z, header = TRUE, split = 2, format = "%d/%m/%Y", aggregate = mean);  
z.fill <- na.locf(zz);  
z.fill <- (z.fill >= 0.5) * z.fill;  
z.fill <- na.fill(z.fill,0);  
zfill.mat = matrix(z.fill, NROW(z.fill));  
z.sum <- rowSums(zfill.mat);  
zsum <- zoo(z.sum,time(z.fill));  
return(zsum, z.fill);  
}  

DF <- read.csv(file.choose(), header = TRUE, as.is = TRUE);  
DF.S <- split(DF[-2], DF[[2]]);  
user <- DF[1,2];  
Ret <- lapply(DF.S,  f);  

DF is of the format (note that for all the users, the TS, values, and dates are different and irregular):

 Date User TS Data 
 21/05/2014 squiggle TS1 0.95  
 17/04/2014 squiggle TS1 1.02   
 27/03/2014 squiggle TS1 0.90   
 30/01/2014 squiggle TS1 0.80   
 12/12/2013 squiggle TS1 0.70  
 18/09/2013 squiggle TS1 0.67  
 01/11/2012 squiggle TS1 0.71  
 01/11/2012 squiggle TS1 0.70  
 21/05/2014 squiggle TS2 0.47  
 20/05/2014 squiggle TS2 0.51  
 16/05/2014 squiggle TS2 0.49  
 15/05/2014 squiggle TS2 0.55  
 10/05/2014 squiggle TS2 0.63  
 07/05/2014 squiggle TS2 0.77
 21/05/2014 squiggle TS1 0.95  
 17/04/2014 z0dlaw nix TS1 1.02   
 27/03/2014 z0dlaw nix TS1 0.90   
 30/01/2014 z0dlaw nix TS1 0.80   
 12/12/2013 z0dlaw nix TS1 0.70  
 18/09/2013 z0dlaw nix TS1 0.67  
 01/11/2012 z0dlaw nix TS1 0.71  
 01/11/2012 z0dlaw nix TS1 0.70  
 21/05/2014 z0dlaw nix TS2 0.47  
 20/05/2014 z0dlaw nix TS2 0.51  
 16/05/2014 z0dlaw nix TS2 0.49  
 15/05/2014 z0dlaw nix TS2 0.55  

...

The problem I have is that Ret contains a list of a data frame, e.g. Ret$user3. I can access this by typing Ret$user[[1]] and Ret$user[[2]] , but since user varies, I need to make this dynamic. I have tried to construct a dynamic expression e.g.:
x <- paste("Ret$'",user,"'",sep = "");
plot(x)

but could not get this to evaluate. How can I access the underlying data frames for each user? I wish to plot these, and merge them into a new dataframe of results which I'll then write to csv.

Upvotes: 0

Views: 58

Answers (1)

agstudy
agstudy

Reputation: 121568

It is not clear what you want , but you can access by sub-setting using [[ operator:

Ret[[username]]

Example:

Ret <- list(user1=data.frame(x=1),
            user2=data.frame(y=2))

Ret[['user2']]
  y
1 2

Upvotes: 2

Related Questions