Reputation: 300
In Java and Python, I found working with maps and dictionaries a very convenient way to handle different dataframes at once. Being a R-newbie, I tried to use this approach to store "parts" of one big dataframe, which I am indexing by using the values ("intervals") from the tlist. To my surprise, the following code did not work and I do not find a solution why:
library(hash)
df <- read.csv("(/testfile.csv")
tlist <- list(5, 30, 60, 90, 180, 356)
data_map <- hash()
for(t in 2:length(tlist)){
data_map[[paste0("T", t)]] <- df[df$T >= tlist[[t-1]] & df$T <= tlist[[t]], ]
}
data_map[["T1"]] #calling dataframe t1 out of the map
When calling the elements of my data_map as in the last line, I get null and I don't understand why. Can anyone give me some insight why filling a hash in this particular matter does not work? Any help is highly appreciated, thanks in advance.
Upvotes: 0
Views: 337
Reputation: 388962
Are you sure you want a hash? Most of the times lists are enough. You can use something like this.
tlist <- c(5, 30, 60, 90, 180, 356)
data_map <- vector('list', length(tlist)-1)
for(t in seq_along(tlist)[-1]) {
data_map[[t-1]] <- df[df$T >= tlist[t-1] & df$T <= tlist[t], ]
}
then access individual values with data_map[[1]]
, data_map[[2]]
etc.
You can assign names with
names(data_map) <- paste0('T', seq_along(data_map))
Upvotes: 1