[R] factor documentation issue
There is a warning in the documentation for ?factor (R version 2.3.0) as follows: The interpretation of a factor depends on both the codes and the 'levels' attribute. Be careful only to compare factors with the same set of levels (in the same order). In particular, 'as.numeric' applied to a factor is meaningless, and may happen by implicit coercion. To revert a factor 'f' to its original numeric values, 'as.numeric(levels(f))[f]' is recommended and slightly more efficient than 'as.numeric(as.character(f))'. But as.numeric seems to work fine whereas as.numeric(levels(f))[f] doesn't always do anything useful. For example: f-factor(1:3,labels=c(A,B,C)) f [1] A B C Levels: A B C as.numeric(f) [1] 1 2 3 as.numeric(levels(f))[f] [1] NA NA NA Warning message: NAs introduced by coercion And also, f-factor(1:3,labels=c(1,5,6)) f [1] 1 5 6 Levels: 1 5 6 as.numeric(f) [1] 1 2 3 as.numeric(levels(f))[f] [1] 1 5 6 Is the documentation wrong, or is the code wrong, or have I missed something? Cheers, Geoff Russell __ 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] Metapost device driver
Hi All, I've started work on a MetaPost device driver (please don't hold your breath). I've copied the XFig driver and renamed everything and this works, I can open the new metapost() and it works exactly like the xfig driver. Now all I have to do is the actual work! Just one question. There is a magic number in ExtEntries as follows: static const R_ExternalMethodDef ExtEntries[] = { EXTDEF(PicTeX, 6), EXTDEF(PostScript, 16),EXTDEF(XFig, 11), EXTDEF(MetaPost, 12),/* Is 12 is OK */ EXTDEF(PDF, 13), I just picked 12, is this Ok, or does it have some special significance? Cheers, Geoff Russell. __ 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] What determines the order of rows in a lattice barchart?
Hi, What determines the order of the rows in a barchart? Cheers, Geoff. Here is my code, and the data follows. If I have z in alpha order, the barchart is in some order I can't determine. I originally tried rownames(twater)~twater$Cat, but the chart wasn't in rownames(twater) order either. library(lattice) twater-read.csv(totalwater.csv,strip.white=T) twater$Cat-ordered(twater$Cat,levels=(sort(levels(twater$Cat sel-twater$Cat==A nm-sort(rownames(twater)) z-factor(nm,levels=nm) chart-barchart(z~twater$Volume, xlab=Volume of Water '000 ML, main=Water Use In Australia for Food, scales=list(col='dark green', cex=1.2, fontface='bold', ), panel=function(x,y) { panel.barchart(x[!sel],y[!sel],col='blue') panel.barchart(x[sel],y[sel],col='red') }) pdf(Rplot-totalwater.pdf,height=4) plot(chart) dev.off() Volume, Cat, Source Agriculture, 16660, A, WA Cotton, 2908, A, WA Rice, 1951, A, WA Sugar, 1388, A, BA Grapes, 729, A, WA FruitVeg, 1358, A, WA Household, 2181, H, WA Beef, 3229, B, BA Diary, 3542, B, BA csv data: [[alternative HTML version deleted]] __ 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] barcharts with 2 different axes
Hi Deepayan, On 10/27/06, Deepayan Sarkar [EMAIL PROTECTED] wrote: On 10/26/06, Geoff Russell [EMAIL PROTECTED] wrote: Dear UseRs, I'm having trouble drawing a barchart with 2 quantities in different units as follows with one unit on the top axis and the other unit on the bottom. E.g., x Tonnes | . . . Row 1 Tonnes1 | xx Row 1 Litres1 | yyy Row 2 Tonnes1 | x Row 2 Litres1 | y Litres|. So if I read this right, the relative heights of the xxx bars and the yyy bars, which you have placed side by side, are in different units and thus their comparison is meaningless. It's doable, but do you really want this sort of graph? A better and much easier to implement design seems to be something like Yes, that is better than what I had in mind, and it will scale when I get the data for the other columns I have in mind. Sample data is below. +-+-+ Row 1 | xx | yyy | Row 2 | x | | +-+-+ Tonnes1 Litres1 Can anybody provide an example please? Please provide some example data (the form of which will matter). -Deepayan Many thanks. Geoff. csv data, Area '000 ha, Volume '000 ML Pastures for Grazing , 710 ,2826.9 Pastures for Seed, 32 ,139 Pastures for hay/silage , 162 ,683 Cereal crops for hay , 66 ,246 Cereal crops for grain/seed , 365 ,1002 Cereal crops not for grain/seed , 42 ,127 Rice , 44 ,615 Sugar Cane , 238 ,1293 Cotton , 234 ,1525 Other broadacre crops, 68 ,172 Fruit Trees, nuts, plantations/fruits, 138 ,660 Vegetables for human use , 112 ,439 Vegetables for seed , 4 ,8.4 Nurseries/flowers/turf , 13 ,77.9 Grapevines , 150 ,588 [[alternative HTML version deleted]] __ 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] barcharts with 2 different axes
Dear UseRs, I'm having trouble drawing a barchart with 2 quantities in different units as follows with one unit on the top axis and the other unit on the bottom. E.g., x Tonnes | . . . Row 1 Tonnes1 | xx Row 1 Litres1 | yyy Row 2 Tonnes1 | x Row 2 Litres1 | y Litres|. . . Can anybody provide an example please? Cheers, Geoff Russell [[alternative HTML version deleted]] __ 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] Key colour problem with lattice plot
Hi, I seem to have a key colour problem with a lattice barchart. The colours on my key rectangle don't match the colours on the barchart. Here is my data frame: LandFill Ruminants United States (USA) .21428 5528.16 France 200.527083 1299.87 Australia 185.878368 2448.17 Russian Federation 1752.8334 2024.29 Argentina 283.98732 2567.02 Brazil 1048.42248 8839.61 Colombia 265.125 1307.61 Mexico 981.023351 1814.89 Ethiopia 9.38020424 1237.49 Sudan 16.0184936 1796.67 India 553.425328 12360.3 Pakistan 47.1593928 2346.71 China 455.680191 8041.79 example LandFill Ruminants United States (USA) .214280 5528.16 France 200.527083 1299.87 Australia185.878368 2448.17 Russian Federation 1752.833400 2024.29 Argentina283.987320 2567.02 Brazil 1048.422480 8839.61 Colombia 265.125000 1307.61 Mexico 981.023351 1814.89 Ethiopia 9.380204 1237.49 Sudan 16.018494 1796.67 India553.425328 12360.30 Pakistan 47.159393 2346.71 China455.680191 8041.79 And here is my plotting statement. barchart(rownames(example)~example$LandFill+example$Ruminants,type=c(p,h), key=list(text=list(c(Landfill,Enteric Fermentation)), rectangles=Rows(trellis.par.get(superpose.symbol),c(1,2)) Cheers, Geoff Russell __ 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] Key colour problem with lattice plot
Thanks Deepayan, On 10/23/06, Deepayan Sarkar [EMAIL PROTECTED] wrote: On 10/22/06, Geoff Russell [EMAIL PROTECTED] wrote: Many thanks Deepayan, .. I'm not sure what you mean by that, but my intention in pointing you towards show.settings() was to make you realize that you should be using superpose.polygon rather than superpose.symbol in the value of 'key' in your code. Keep everything else unchanged, and you should get what you want. Excellent. When you are using a package you are supposed to consult the accompanying documentation, not a book (which, incidentally, was written for the S-PLUS version, where your code will not work in the first place) or the 'net'. The fact that this documentation is long is not your fault, but you don't really get to complain about it. Apologies for letting my frustration show. I understand that rich and flexible tools require learning, and I should have spent more time reading the docs. Thanks for your patience. Cheers, Geoff. -Deepayan __ 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] Stats question - cox proportional hazards adjustments
Hi useRs, Many studies of the link between red meat and colorectal cancer use Cox proportional hazards with (among other things) a gender covariate. If it is true that men eat more red meat, drink more alcohol and smoke more than women, and if it is also true that alcohol and tobacco are known risk factors then why does it make sense to adjust for gender? I would think that in this case some of the risk that should be properly attributed to the bad habits will actually end up being attributed to being male instead. Cheers, Geoff Russell __ 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] Stats question - cox proportional hazards adjustments
Peter et al, Thanks for the reply, I did reread the posting guide before posting and figured it was a short question and might just have a short answer. I have Therneau's book on order, which will probably clarify the matter in time. I understand stratifying to deal with confounding, but not adding it as a covariate in a regression. e.g, If one of the gender related effects you mention happens to be drinking, then we don't want to get rid of it, it may well be an additional covariate and we want its full effect embodied in the b value for that covariate. I'll keep reading! Cheers, Geoff On 20 Sep 2006 14:47:00 +0200, Peter Dalgaard [EMAIL PROTECTED] wrote: Geoff Russell [EMAIL PROTECTED] writes: Hi useRs, Many studies of the link between red meat and colorectal cancer use Cox proportional hazards with (among other things) a gender covariate. If it is true that men eat more red meat, drink more alcohol and smoke more than women, and if it is also true that alcohol and tobacco are known risk factors then why does it make sense to adjust for gender? I would think that in this case some of the risk that should be properly attributed to the bad habits will actually end up being attributed to being male instead. This is more than a bit off-topic for the list, but in (very) brief: Because you need to get rid of purely gender related effects that disturb the analysis and may create spurious association. Otherwise you would become able to prove effects like stiletto heels causing breast cancer, etc. -- O__ Peter Dalgaard Ă˜ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ 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] Reading a file in R
On 9/19/06, Mesomeris, Spyros [CIR] [EMAIL PROTECTED] wrote: Dear R helpers, I am trying to read a CSV file in R called EUROPE (originally an Excel file which I have saved as a CSV file) using the command EUROPEDATA - read.csv(EUROPE.csv) EUROPE.csv is basically a matrix of dimension 440*44, and has a line of headers, i.e. each column has a name. Check your file for unicode characters, they will get in the way. I'm new to R myself but have used both read.delim and read.csv. I've only had problems when the files contained unicode. I don't know excel, but in staroffice you need to explicitly set the character set during the save if the file had unicode on input. Cheers, Geoff Russell __ 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] Table manipulation question
I have a table: C1 RowName13 RowName22 and another table: C2 RowName15.6 RowName1a 4.3 RowName2NA I want to join join the tables with matching rows: C1 C2 RowName1 35.6 RowName22 NA I'm thinking of something like: T1$C2=T2$C2[index-expression-to-pullout the matching ones] Any ideas would be appreciated. Cheers, Geoff Russell [[alternative HTML version deleted]] __ 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] Running cox models
Hi, I'm reading van Belle et al Biostatistics and trying to run a cox test using a dataset from: http://faculty.washington.edu/~heagerty/Books/Biostatistics/chapter16.html (Primary Biliary Cirrhosis data link at top of the page), I'm using the following code: --- start of code library(survival) liver - scan(liver2.txt,list(age=0,albumin=0,alkphos=0,ascites=0,bili=0, cholest=0,edema=0,edmadj=0,hepmeg=0,obstime=0,platelet=0,protime=0, sex=0,sgot=0,spiders=0,stage=0,status=0,treatmnt=0, triglyc=0,urinecu=0)) fit-coxph(Surv(obstime,status)~bili+edmadj+albumin+protime+age,data=liver) summary(fit) - End of code but the answer is rather different from that in the book (p.688 - for anyone with the book). The book refers to EDTRT, but the dataset has EDMADJ and EDMEMA, also the book talks about 312 patients and the dataset has 418 lines. Has anybody else used this dataset? Cheers, Geoff Russell __ 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] Frequency tables without underlying data
Hi from a new useR, I know how to build a table() with 2 factors(), but I want to build a table() when I only know the frequencies: e.g. I know that useR useStatA rich 100 200 poor 200 5 but i don't have the underlying data to set up factors. Can I still make a frequency table? Cheers, Geoff Russell [[alternative HTML version deleted]] __ 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.