r - How to find a data frame in a list from its attribute value? -
i have several data frames each represent data in given condition.
for instance, df.1 data measured @ temperature t1, df.2 @ temperature t2, , on.
i have set attribute temperature each data frame , stored data frames in list.
now want retrieve data frame given temperature.
so have 2 questions:
is attribute/list approach recommended one?
is there elegant way retrieve data frame appropriate temperature? see way loop, wonder if there better solution.
here example code:
x <- seq(0, 50, = 0.1) y1 <- sin(x) y2 <- cos(x) df.1 <- data.frame(time = x, value = y1) attr(df.1, "temperature") <- 20.0 df.2 <- data.frame(time = x, value = y2) attr(df.2, "temperature") <- 30.0 df.list <- list(df.1, df.2) result <- null (df in df.list) { if (attr(df, "temperature") == 20) { result <- df } }
it's advisable provide appropriate names list created, e.g.
names(df.list) <- c("t20", "t30")
this way, can call out appropriate table according names, e.g.
head(df.list[["t20"]]) time value 1 0.0 0.00000000 2 0.1 0.09983342 3 0.2 0.19866933 4 0.3 0.29552021 5 0.4 0.38941834 6 0.5 0.47942554 head(df.list[["t30"]]) time value 1 0.0 1.0000000 2 0.1 0.9950042 3 0.2 0.9800666 4 0.3 0.9553365 5 0.4 0.9210610 6 0.5 0.8775826
Comments
Post a Comment