[R] logical 'or' on list of vectors
Suppose I have a list of logicals, such as returned by lapply: Theoph$Dose[1] - NA Theoph$Time[2] - NA Theoph$conc[3] - NA lapply(Theoph,is.na) Is there a direct way to execute logical or across all vectors? The following gives the desired result, but seems unnecessarily complex. as.logical(apply(do.call(rbind,lapply(Theoph,is.na)),2,sum)) Regards, Tim __ R-help@stat.math.ethz.ch 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] logical 'or' on list of vectors
Thanks all for the many excellent suggestions! !complete.cases(Theoph) is probably the most succinct form for the current problem, while the examples with 'any' seem readily adaptable to similar situations. Kind regards, Tim. Dimitris Rizopoulos wrote: try the following: as.logical(rowSums(is.na(Theoph))) ## or !complete.cases(Theoph) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Tim Bergsma [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Sent: Friday, June 08, 2007 2:57 PM Subject: [R] logical 'or' on list of vectors Suppose I have a list of logicals, such as returned by lapply: Theoph$Dose[1] - NA Theoph$Time[2] - NA Theoph$conc[3] - NA lapply(Theoph,is.na) Is there a direct way to execute logical or across all vectors? The following gives the desired result, but seems unnecessarily complex. as.logical(apply(do.call(rbind,lapply(Theoph,is.na)),2,sum)) Regards, Tim __ R-help@stat.math.ethz.ch 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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] test for nested factors
Is there a conventional way to test for nested factors? I.e., if 'a' and 'b' are lists of same-length factors, does each level specified by 'a' correspond to exactly one level specified by 'b'? The function below seems to suffice, but I'd be happy to know of a more succinct solution, if it already exists. Thanks, Tim. --- %nested.in% - function(x,f,...){ #coerce to list if(!is.list(x))x-list(x) if(!is.list(f))f-list(f) #collapse to vector x - tapply(x[[1]],x) f - tapply(f[[1]],f) #analyse return(all(sapply(lapply(split(f,x),unique),length)==1)) } CO2$Plant %nested.in% CO2[,c(Type,Treatment)] #TRUE CO2$Plant %nested.in% (CO2$uptake mean(CO2$uptake)) #FALSE __ R-help@stat.math.ethz.ch 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] Extent of time zone vulerability for POSIX date and time classes
Hi. I frequently convert date and time data to and from character representations. I'm frustrated with chron, because 'seconds' are required to create a time object (my input data never has seconds). More importantly, I cannot make chron print the format 12/30/2006 (which my output data requires). I really like the format flexibility of strftime() and strptime(), but of course am paranoid about timezone issues. After reading the standard reference several times (http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf), I am tempted to conclude that if I never specify timezones, and never use Sys.time(), the vulnerabilities do not pertain. To the point: if I'm merely converting to and from character data that does not represent time zones, is there still a time zone vulnerability with strftime() and strptime()? Thanks in advance, Tim Bergsma, PhD Metrum Research Group, LLC #example strftime( strptime( 30-Dec-06 23:30, format=%d-%b-%y %H:%M ), format=%m/%d/%Y %H:%M ) [1] 12/30/2006 23:30 __ R-help@stat.math.ethz.ch 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] Extent of time zone vulerability for POSIX date and time classes
Gabor, thanks for the feedback. I like the idea of coercing the system time zone, and sticking with strptime/strftime. The balance would probably tip in favor of chron, however, if I could get the format correct. Apparently the default is two-digit year: library(chron) chron(1) [1] 01/02/70 chron(1,format=m/d/y) [1] 01/02/70 But I need four digit year, so I try: chron(1,format=m/d/) [1] Jan/02/1970 Now, month has switched to character! I don't know of a format convention that gets it back to numeric (although conventions are documented for forcing to character). Comment? Regards, Tim. Gabor Grothendieck wrote: If you use Sys.putenv(TZ = GMT) at the beginning of your session then local time zone and GMT time zone will be the same so you should not have a problem. This was not possible, at least on Windows, at the time the R News article was written. On 4/5/07, Tim Bergsma [EMAIL PROTECTED] wrote: Hi. I frequently convert date and time data to and from character representations. I'm frustrated with chron, because 'seconds' are required to create a time object (my input data never has seconds). You could use paste: times(paste(12:30, 0, sep = :)) and make it into a function if you use it a lot. More importantly, I cannot make chron print the format 12/30/2006 (which my output data requires). That is the default output format so you don't have to specify anything. Its only with POSIX that its not the default. For example: library(chron) x - chron(12/30/2006) x [1] 12/30/06 I really like the format flexibility of strftime() and strptime(), but of course am paranoid about timezone issues. After reading the standard reference several times (http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf), I am tempted to conclude that if I never specify timezones, and never use Sys.time(), the vulnerabilities do not pertain. That won't protect you but this would let you use POSIX safely: http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg83508.html To the point: if I'm merely converting to and from character data that does not represent time zones, is there still a time zone vulnerability with strftime() and strptime()? Thanks in advance, Tim Bergsma, PhD Metrum Research Group, LLC #example strftime( strptime( 30-Dec-06 23:30, format=%d-%b-%y %H:%M ), format=%m/%d/%Y %H:%M ) [1] 12/30/2006 23:30 __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] Extent of time zone vulerability for POSIX date and time classes
Gabor, The out.fmt approach seems optimal. Thank you! Tim. Gabor Grothendieck wrote: There are several ways: 1. the default number of year digits is set by the chron.year.abb option. The R News article mentions this in the Avoiding Errors section but does suggest you not set it. options(chron.year.abb = FALSE) chron(1) 2. Alternately, define: out.fmt - function(x) with( month.day.year(x), sprintf(%02.f/%02.f/%04.f, month, day, year)) # Now any of these will work: x - chron(1:5) out.fmt(x) # or x - chron(1:5) chron(x, out.format = out.fmt) # or x - chron(1:5, out.format = out.fmt) x On 4/5/07, Tim Bergsma [EMAIL PROTECTED] wrote: Gabor, thanks for the feedback. I like the idea of coercing the system time zone, and sticking with strptime/strftime. The balance would probably tip in favor of chron, however, if I could get the format correct. Apparently the default is two-digit year: library(chron) chron(1) [1] 01/02/70 chron(1,format=m/d/y) [1] 01/02/70 But I need four digit year, so I try: chron(1,format=m/d/) [1] Jan/02/1970 Now, month has switched to character! I don't know of a format convention that gets it back to numeric (although conventions are documented for forcing to character). Comment? Regards, Tim. Gabor Grothendieck wrote: If you use Sys.putenv(TZ = GMT) at the beginning of your session then local time zone and GMT time zone will be the same so you should not have a problem. This was not possible, at least on Windows, at the time the R News article was written. On 4/5/07, Tim Bergsma [EMAIL PROTECTED] wrote: Hi. I frequently convert date and time data to and from character representations. I'm frustrated with chron, because 'seconds' are required to create a time object (my input data never has seconds). You could use paste: times(paste(12:30, 0, sep = :)) and make it into a function if you use it a lot. More importantly, I cannot make chron print the format 12/30/2006 (which my output data requires). That is the default output format so you don't have to specify anything. Its only with POSIX that its not the default. For example: library(chron) x - chron(12/30/2006) x [1] 12/30/06 I really like the format flexibility of strftime() and strptime(), but of course am paranoid about timezone issues. After reading the standard reference several times (http://cran.r-project.org/doc/Rnews/Rnews_2004-1.pdf), I am tempted to conclude that if I never specify timezones, and never use Sys.time(), the vulnerabilities do not pertain. That won't protect you but this would let you use POSIX safely: http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg83508.html To the point: if I'm merely converting to and from character data that does not represent time zones, is there still a time zone vulnerability with strftime() and strptime()? Thanks in advance, Tim Bergsma, PhD Metrum Research Group, LLC #example strftime( strptime( 30-Dec-06 23:30, format=%d-%b-%y %H:%M ), format=%m/%d/%Y %H:%M ) [1] 12/30/2006 23:30 __ R-help@stat.math.ethz.ch 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-help@stat.math.ethz.ch 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] fitting the gamma cumulative distribution function
Hi. I have a vector of quantiles and a vector of probabilites that, when plotted, look very like the gamma cumulative distribution function. I can guess some shape and scale parameters that give a similar result, but I'd rather let the parameters be estimated. Is there a direct way to do this in R? Thanks, Tim. week - c(0,5,6,7,9,11,14,19,39) fraction - c(0,0.23279,0.41093,0.58198,0.77935,0.88057,0.94231,0.98583,1) weeks - 1:160/4 plot(weeks, pgamma(weeks,shape=6,scale=1.15),type=l) points(week,fraction,col=red) __ R-help@stat.math.ethz.ch 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] S4 newbie - extending S3 classes
I tried recently to extend data.frame using S4, with depressing results. Someone asked about this back in January, and was referred to the Register or Convert advice. Putting the pieces together, I'm guessing that to extend data.frame usefully in S4, one would actually have to write it from scratch, creating all the usual attributes, methods, etc. One certainly wouldn't want to do this if unnecessary or if already done by someone more competent. I've ordered John Chamber's book Programming with Data. In the meantime, can someone confirm/deny my suspicions that data.frame would need an S4 rewrite and that no standard rewrite exists? Thanks, Tim. __ R-help@stat.math.ethz.ch 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] extending data.frame with S4 class
Hi. When I extend numeric with S4 and create an instance, I get something that looks and acts like a numeric. When I extend data.frame and create an instance, I get a NULL 'core'. Why is this, and can it be fixed? setOldClass(), prototype=, and initialize() don't seem to help. Thanks, Tim setClass(numClass,representation(numeric,num=numeric)) nn - new(numClass,2,num=1) nn nn+1 setClass(dfClass,representation(data.frame,num=numeric)) df - new(dfClass,as.data.frame(BOD),num=1) df __ R-help@stat.math.ethz.ch 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.