r - extract a single column -
i have list of 701 given csv
files. each 1 has same number of columns (7) different number of rows (between 25000 , 28000).
here extract of first file:
date,week,week day,hour,price,volume,sale/purchase 18/03/2011,11,5,1,-3000.00,17416,sell 18/03/2011,11,5,1,-1001.10,17427,sell 18/03/2011,11,5,1,-1000.00,18055,sell 18/03/2011,11,5,1,-500.10,18057,sell 18/03/2011,11,5,1,-500.00,18064,sell 18/03/2011,11,5,1,-400.10,18066,sell 18/03/2011,11,5,1,-400.00,18066,sell 18/03/2011,11,5,1,-300.10,18068,sell 18/03/2011,11,5,1,-300.00,18118,sell
i made nonlinear regression of supply curve of 9th hr year 2012. datas 2012 in 290. 654. csv files.
allenamen <- dir(pattern="*.csv") alledat <- lapply(allenamen, read.csv, header = true, sep = ",", stringsasfactors = false) h <- list() for(i in 290:654) { g <- function(a, b, c, d, p) {a*atan(b*p+c)+d} f <- nlslm(volume ~ g(a,b,c,d,price), data=subset(alledat[[i-289]], (hour==9) & (sale.purchase == "sell") & (!price %in% as.character(-50:150))), start = list(a=4000, b=0.1, c=-5, d=32000)) h[[i-289]] <- coef(f) }
this works , coefficients a, b, c , d every day in 2012.
this head(h)
:
[[1]] b c d 2.513378e+03 4.668218e-02 -3.181322e+00 2.637142e+04 [[2]] b c d 2.803172e+03 6.696201e-02 -4.576432e+00 2.574454e+04 [[3]] b c d 3.298991e+03 5.817949e-02 -3.425728e+00 2.393888e+04 [[4]] b c d 2.150487e+03 3.810406e-02 -2.658772e+00 2.675609e+04 [[5]] b c d 2.326199e+03 3.044967e-02 -1.780965e+00 2.604374e+04 [[6]] b c d 2934.0193270 0.0302937 -1.9912913 26283.0300823
and dput(head(h))
:
list(structure(c(2513.37818972349, 0.0466821822063123, -3.18132213466142, 26371.4241646124), .names = c("a", "b", "c", "d")), structure(c(2803.17230054557, 0.0669620116294894, -4.57643230249848, 25744.5376725213), .names = c("a", "b", "c", "d")), structure(c(3298.99066895304, 0.0581794881246528, -3.42572804902504, 23938.8754575156), .names = c("a", "b", "c", "d")), structure(c(2150.48734655237, 0.0381040636898022, -2.65877160023262, 26756.0907073567), .names = c("a", "b", "c", "d")), structure(c(2326.19873555633, 0.0304496684589379, -1.7809654498454, 26043.735374657), .names = c("a", "b", "c", "d")), structure(c(2934.01932702805, 0.0302937043170001, -1.99129130343521, 26283.0300823458), .names = c("a", "b", "c", "d")))
now trying column h$a
null. how can column?
in add-on want plot single coefficients , date
. tried code:
koeffreihe <- function(x) { files <- list.files(pattern="*.csv") df <- data.frame() for(i in 1:length(files)){ xx <- read.csv(as.character(files[i])) xx <- subset(xx, sale.purchase == "sell" & hr == 3) df <- rbind(df, xx) g <- function(a, b, c, d, p) {a*atan(b*p+c)+d} f <- nlslm(volume ~ g(a,b,c,d,price), data=subset(alledat[[i]], (hour==9) & (sale.purchase == "sell") & (!price %in% as.character(-50:150))), start = list(a=4000, b=0.1, c=-5, d=32000)) h[[i]] <- coef(f) } df$date <- as.date(as.character(df$date), format="%d/%m/%y") plot(h$x ~ date, df, xlim = as.date(c("2012-01-01", "2012-12-31"))) } koeffreihe(a)
but error:
invalid type (null) variable 'h$x'
so problem h$a
null. if can prepare problem guess code work too.
thank help!
first transform list data.frame:
h.df <- setnames(do.call(rbind.data.frame, h), names(h[[1]])) # b c d #1 2513.378 0.04668218 -3.181322 26371.42 #2 2803.172 0.06696201 -4.576432 25744.54 #3 3298.991 0.05817949 -3.425728 23938.88 #4 2150.487 0.03810406 -2.658772 26756.09 #5 2326.199 0.03044967 -1.780965 26043.74 #6 2934.019 0.03029370 -1.991291 26283.03
then can extract variables easily:
h.df$a #[1] 2513.378 2803.172 3298.991 2150.487 2326.199 2934.019
alternatively can iterate on list extract variable:
sapply(h, "[", "a") # #2513.378 2803.172 3298.991 2150.487 2326.199 2934.019
r list
No comments:
Post a Comment