Re: [R] Is this foreach behaviour correct?
I'm still not clear about whether this is a bug in foreach. Should c.Date be invoked by foreach with .combine='c'? On 11/06/2016 07:02 PM, William Dunlap wrote: Note that in the OP's example c.Date is never invoked. c.Date is called if .combine calls c rather than if .combine is c: > library(zoo) > trace(c.Date, quote(print(sys.call( Tracing function "c.Date" in package "base" [1] "c.Date" > foreach(i=1:10003, .combine=c) %do% { as.Date(i) } [1] 1 10001 10002 10003 > foreach(i=1:10003, .combine=function(...)c(...)) %do% { as.Date(i) } Tracing c.Date(...) on entry eval(expr, envir, enclos) Tracing c.Date(...) on entry eval(expr, envir, enclos) Tracing c.Date(...) on entry eval(expr, envir, enclos) [1] "1997-05-19" "1997-05-20" "1997-05-21" "1997-05-22" Bill Dunlap TIBCO Software wdunlap tibco.com<http://tibco.com> On Sun, Nov 6, 2016 at 2:20 PM, Duncan Murdoch <murdoch.dun...@gmail.com<mailto:murdoch.dun...@gmail.com>> wrote: On 06/11/2016 5:02 PM, Jim Lemon wrote: hi James, I think you have to have a starting date ("origin") for as.Date to convert numbers to dates. That's true with the function in the base package, but the zoo package also has an as.Date() function, which defaults the origin to "1970-01-01". If James is using zoo his code would be okay. If he's not, he would have got an error, so I think he must have been. Duncan Murdoch Jim On Sun, Nov 6, 2016 at 12:10 PM, James Hirschorn <james.hirsch...@hotmail.com<mailto:james.hirsch...@hotmail.com>> wrote: This seemed odd so I wanted to check: > x <- foreach(i=1:10100, .combine='c') %do% { as.Date(i) } yields a numeric vector for x: > class(x) [1] "numeric" Should it not be a vector of Date? __ R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Is this foreach behaviour correct?
Yes, I should have put > library(foreach) > library(zoo) at the top. On 11/06/2016 05:20 PM, Duncan Murdoch wrote: > On 06/11/2016 5:02 PM, Jim Lemon wrote: >> hi James, >> I think you have to have a starting date ("origin") for as.Date to >> convert numbers to dates. > > That's true with the function in the base package, but the zoo package > also has an as.Date() function, which defaults the origin to > "1970-01-01". If James is using zoo his code would be okay. If he's > not, he would have got an error, so I think he must have been. > > Duncan Murdoch > >> >> Jim >> >> On Sun, Nov 6, 2016 at 12:10 PM, James Hirschorn >> <james.hirsch...@hotmail.com> wrote: >>> This seemed odd so I wanted to check: >>> >>> > x <- foreach(i=1:10100, .combine='c') %do% { as.Date(i) } >>> >>> yields a numeric vector for x: >>> >>> > class(x) >>> [1] "numeric" >>> >>> Should it not be a vector of Date? >>> >>> __ >>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. >> > > . > __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Reduce does not work with data.table?
Reduce is failing when applied to a list of elements of class data.table. Perhaps this is a bug? Example: library(data.table) dt1 <- data.table(x = 1:3, y = 4:6) dt2 <- data.table(x = 4:6, y = 1:3) dt3 <- data.table(x = 0:-2, y = 0:-2) # This works fine dt1 + dt2 + dt2 #x y # 1: 5 5 # 2: 6 6 # 3: 7 7 # But: dt_list <- list(dt1, dt2, dt3) Reduce("+", dt_list) # Error in f(init, x[[i]]) : non-numeric argument to binary operator # In addition: Warning message: # In Reduce("+", dt_list) : # Incompatible methods ("Ops.data.frame", "Ops.data.table") for "+" If I use data.frame instead of data.table, Reduce works properly. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Is this a bug in quantmod::OpCl?
OpCl works on xts objects but not on quantmod.OHLC objects. Is this a bug? Example error: x.Date <- as.Date("2003-02-01") + c(1, 3, 7, 9, 14) - 1 set.seed(1) x <- zoo(matrix(runif(20, 0, 1), nrow=5, ncol=4), x.Date) q <- as.quantmod.OHLC(x,c("Open","High","Low","Close")) # error OpCl(q) #> Error in `colnames<-`(`*tmp*`, value = "OpCl.q") : #> attempt to set 'colnames' on an object with less than two dimensions # OK OpCl(as.xts(q)) [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Extracting from data.frame
Can someone please explain the following behavior? df1 and df2 are data.frames. Suppose I want a subset of the rows (observations) using extraction, say just the first row. What I want to know is why if df1 has just one column then df1[1,] returns a vector, whereas if df2 has 2 or more columns then df2[1,] returns a data frame? Why is the single column case different? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] extracting named vector from dataframe
Suppose df is a dataframe with one named row of numeric observations. I want to coerce df into a named vector. as.vector does not work as I expected: as.vector(df) returns the original dataframe, while as.vector(df,mode=numeric) returns an unnamed vector of NAs. This works: v - as.numeric(as.matrix(df)); names(v) - names(df); I just wanted check if there was a better/more natural way of doing this? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] extracting named vector from dataframe
Of course you are right that this would not be appropriate in general, but what I'm doing--which as Baptiste explained can be done much more nicely with unlist()---seems reasonable in my context: The dataframe has a computed statistic for each input, but I need a vector so that I can do operations such as sort(). -Original Message- From: David Winsemius [mailto:dwinsem...@comcast.net] Sent: Sunday, October 31, 2010 12:24 PM To: James Hirschorn Cc: R-help@r-project.org Subject: Re: [R] extracting named vector from dataframe On Oct 31, 2010, at 11:54 AM, James Hirschorn wrote: Suppose df is a dataframe with one named row of numeric observations. I want to coerce df into a named vector. I don't think you understand the structure of dataframes. They are named lists of component columns. The names you are attributing to the rows are not attached to the observations but rather are column names. So that row is not in any sense a named vector. If you created a dataframe with the first column a named vector its names would become the rownames. as.vector does not work as I expected: as.vector(df) returns the original dataframe, while as.vector(df,mode=numeric) returns an unnamed vector of NAs. This works: v - as.numeric(as.matrix(df)); names(v) - names(df); Right. You are now assigning the column names to the elements in a row, but in some ways that is an unnatural act, and not something that would be expected to work in the general case where a row might be a diverse set of types and even different classes. Your as.matrix operation coerced all of the values to be of one type. I just wanted check if there was a better/more natural way of doing this? -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Long model formulae
Here is a dodge I often use. This is a mock-up example. Very instructive (and helpful) ... ___ bar - data.frame(matrix(rnorm(1001), nrow = 1)) names(bar)[1] - y ## say head(bar[,1:5]) nbar - names(bar) form - as.formula(paste(nbar[1], ~, paste(nbar[-1], collapse = +))) fitModel - substitute(tm - rpart(FORM, data = DATA), list(FORM = form, DATA = quote(bar))) fitModel ## the screen quietly erupts... library(rpart) eval(fitModel) ## to do the job. ___ The advantage of proceeding this way is that the object you create, fm, has a meaningful (but large!) formula in it and the name of the dataframe from which the variables come. This makes it easy, e.g. to use manipulation tools on it. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Long model formulae
What is a good way to enter a very long model formula. For example: y ~ Input.2 + Input.3 + ... + Input.1000 (assuming the corresponding dataframe has many other columns). Is there a way to convert a character string to a formula? Are there command line expansions in R besides the simple '.'? Thanks. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] read columns of quoted numbers as factors
Yes, your solution of setting quote= would read the multi-word strings incorrectly. A more complicated version of your solution should work: First check which columns are identified as strings, and then apply your solution to the remaining columns. I'm a newbie at R, but it seems to me that there is a logical inconsistency in R: write.table puts quotes around numbers when they form a column of factors, but does not put quotes for a column of integers. Since read.table is the dual of write.table it seems that it should treat quoted and unquoted columns differently, analogously to write.table. However, there does not even seem to be an option to make read.table behave analogously. - Original Message From: peter dalgaard pda...@gmail.com To: james hirschorn j_hirsch...@yahoo.com Cc: r-help@r-project.org Sent: Tue, October 5, 2010 7:25:52 AM Subject: Re: [R] read columns of quoted numbers as factors On Oct 4, 2010, at 18:39 , james hirschorn wrote: Suppose I have a data file (possibly with a huge number of columns), where the columns with factors are coded as 1, 2, 3, etc ... The default behavior of read.table is to convert these columns to integer vectors. Is there a way to get read.table to recognize that columns of quoted numbers represent factors (while unquoted numbers are interpreted as integers), without explicitly setting them with colClasses ? I don't think there's a simple way, because the modus operandi of read.table is to read everything as character and then see whether it can be converted to numeric, and at that point any quotes will have been lost. One possibility, somewhat dependent on the exact file format, would be to temporarily set quote=, see which columns contains quote characters, and, on a second pass, read those columns as factors, using a computed colClasses argument. It will break down if you have space-separated columns with quoted multi-word strings, though. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd@cbs.dk Priv: pda...@gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] read columns of quoted numbers as factors
Suppose I have a data file (possibly with a huge number of columns), where the columns with factors are coded as 1, 2, 3, etc ... The default behavior of read.table is to convert these columns to integer vectors. Is there a way to get read.table to recognize that columns of quoted numbers represent factors (while unquoted numbers are interpreted as integers), without explicitly setting them with colClasses ? __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.