Re: [R] Explore patterns with GAM
Thank you very much Andrew! I will follow your suggestion. Spyros -- View this message in context: http://r.789695.n4.nabble.com/Explore-patterns-with-GAM-tp4655838p4655928.html Sent from the R help mailing list archive at Nabble.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.
Re: [R] How to calculate monthly average from daily files in R?
Hello, I guess that you are working on Windows. So, I don't know whether the following is pertinent or not. On Linux, the first file is 'ET100.bin', not 'ET1.bin'. Thus, the first 'monthly' mean is calculated between April, 10 and May, 10. You can try the following script. When I read your data, the code for the missing values is -999, not -32765, maybe due to the machine. You can manage your own missing value code by modifying missval. #- missval - -999 files - paste0(C:\\New folder (4)\\New folder\\ET,1:365,.bin) mm - factor(rep(1:12, c(31,28,31,30,31,30,31,31,30,31,30,31))) files.group - split(files, mm) monthly - NULL for(ff in files.group){ x - do.call(rbind,(lapply(ff, readBin, double(), size=4, n=360*720, signed=T))) x[x==missval] - NA x - apply(x,2,mean) monthly - rbind(monthly, x) rm(x) } dim(monthly) #- HTH, Pascal Le 18/01/2013 02:37, Jonsson a écrit : I have 365 binary files: https://echange-fichiers.inra.fr/get?k=oy3CN1yV1Um7ouRWm2U ,I want to calculate the monthly average. So from the 365 files, I will get 12 files.I would like also to tell R not to take into account the no-data value (-32765).for example, for the first month, there are 31 records: 3 of these records has the value -32765,I want R to take the average of the rest records(28 records) and so on with all months. This code will take the average of every 30 files(any idea on how to make it according to number of days in a month?and not to take into account the no-data values) files- list.files(C:\\New folder (4)\\New folder, *.bin,full.names=TRUE) # assume that we want to take the average of every 30 files files.group- split(files , rep(seq_along(files), each = 30, length =length(files))) results- list() for (.files in files.group){ # read in the 30 files as a vector of numbers that you take the average of x- do.call(rbind,(lapply(.files, readBin , double() , size = 4 ,n =360 * 720 , signed =T))) ## take the means across the 30 files results[[length(results) + 1L]]- colMeans(x)} close(x) for (i in seq_along(results)){ fileName - sprintf(C:/New folder/glo_%d.flt, i) writeBin(as.double(results[[i]]), fileName, size = 4)} -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-monthly-average-from-daily-files-in-R-tp4655869.html Sent from the R help mailing list archive at Nabble.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-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] ggplot zoomin
Dear All, I am plotting a graph in ggplot, I would like to magnify the values between 0-1 without losing data in the higher range. How can I do that? neither scale_y_continous nor coord_cartesian works. Thank you Özgül Université Libre de Bruxelles __ 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] repeat resampling with different subsample sizes
Hi, I'm trying to write a code (see below) to randomly resample measurements of one variable (say here the variable counts in the data frame dat) with different resampled subsample sizes. The code works fine for a single resampled subsample size (in the code below = 10). I then tried to generalize this by writing a function with a loop, where in each loop the function should do the calculations with increasing subsample size (say for j in 1:100). Problem is, I can't tell R to write the results of each of these loops. As a result, the function always keeps the results of the last loop (with j=100). Many thanks in advance for any suggestion. # Creating data frame with counts and depths test - matrix(rep(1, 1000)) depths - matrix(1:1000) dat - cbind(test, depths) rm(depths, test) colnames(dat) - c(counts, depths) dat - as.data.frame(dat) ### Easy code to resample 999 times 10 counts from dat require(MASS) # Makes sums and var based on 999 resampled counts and stores results in a matrix # with sums and vars in 999 rows and 2 columns - THIS WORKS resamples10 - lapply(1:999, function(i) sample(dat$counts, 10, replace=T)) r10.stat - cbind(sapply(resamples10, sum), sapply(resamples10, var), sapply(resamples10, mean)) colnames(r10.stat) - c(r10.sum, r10.var, r10.mean) ### NEED to generalize this for different sample sizes # Creates the resample function with arguments data, stat. # by default, num = size of resampled subsample varies for j in 1:100 # CAN'T tell him to store all sums of different sample sizes in one data.frame require(MASS) b.stat - function(data, stat) { for (j in 1:100){ rj.repl - lapply(1:999, function(i) sample(data, j, replace=T)) rj.stat - sapply(rj.repl, stat) rj.stderr - sqrt(var(rj.stat)) list(std.err=rj.stderr, resamples=rj.repl, stats=rj.stat) } b.sum - cbind([, rj.stat]) } b.sum - b.stat(dat$counts, sum) -- View this message in context: http://r.789695.n4.nabble.com/repeat-resampling-with-different-subsample-sizes-tp4655927.html Sent from the R help mailing list archive at Nabble.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.
Re: [R] plotting from dataframes
So by hand the command would be par(mfrow=c(1,2)) plot(frames$'1'hour1) plot(frames$'2'hour1) But in my case there are far more than 2 days, so I want to use a loop. Suppose I have 10 plots par(mfrow=c(2,5)) for(i in 1:10){ plot( /what should be put here??/) } -- View this message in context: http://r.789695.n4.nabble.com/plotting-from-dataframes-tp4655851p4655931.html Sent from the R help mailing list archive at Nabble.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.
Re: [R] How to calculate monthly average from daily files in R?
Thanks,I am using windows -- View this message in context: http://r.789695.n4.nabble.com/How-to-calculate-monthly-average-from-daily-files-in-R-tp4655869p4655933.html Sent from the R help mailing list archive at Nabble.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.
Re: [R] reading multiple key=value pairs per line
Hi One option are regular expressions but you can also read data with = as separator. test-read.table(input_kvpairs.csv, sep=c(=), header=F, stringsAsFactors=F) #use this function to split and extract numeric parts extract-function(x) as.numeric(sapply(strsplit(x,,),[,1)) # and apply the function to appropriate columns test1 - data.frame(sapply(test[,2:3], extract), test[,4]) Now you can put names to resulting data frame see ?names Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Frank Singleton Sent: Friday, January 18, 2013 5:21 AM To: r-help@r-project.org Subject: [R] reading multiple key=value pairs per line Hi, Thanks for a great environmentfor statistical computing :-) I have some input data in a file (input_kvpairs.csv) of the form key1=23, key2=67, key3=hello there key1=7, key2=22, key3=how are you key1=2, key2=77, key3=nice day, thanks Now in my head I wish it was of the form (input.csv) #key1, key2, key3 23,67, hello there 7, 22, how are you 2, 77, nice day, thanks so I could do data - read.csv(input.csv, header=TRUE) where the header column names are derived from the key names dynamically, and I could access the data using normal data$key1 or data$key2 mechanism. I guess I could just pre process the file first using python etc to create a CSV file with column header derived from key names, and values derived from key values, but I am interested to see how experienced R folks would handle this inside R. Thanks, Frank __ 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-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] plotting from dataframes
Hi -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of condor Sent: Friday, January 18, 2013 10:17 AM To: r-help@r-project.org Subject: Re: [R] plotting from dataframes So by hand the command would be par(mfrow=c(1,2)) plot(frames$'1'hour1) plot(frames$'2'hour1) But in my case there are far more than 2 days, so I want to use a loop. Suppose I have 10 plots par(mfrow=c(2,5)) for(i in 1:10){ plot( /what should be put here??/) use plot[,i] if you want plot columns 1:10 or put this col.to.plot=c(5,7,9,12,15, 17, 21, 25, 26, 30) in front of cycle and use plot[,col.to.plot[i]] if you want to plot preselected columns Regards Petr } -- View this message in context: http://r.789695.n4.nabble.com/plotting- from-dataframes-tp4655851p4655931.html Sent from the R help mailing list archive at Nabble.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-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] Which df to extract from ANCOVA model
Hi, I am running an ANCOVA model like: aov(Y ~ Var1 + Var2 + Var3 + Var4 + CoVar1 + CoVar2) where Y is the response (metric, 1.0-1000.0), VarX are all metric predictors, and CoVarX are two Covariates each a factor of 4-5 levels. So far as I can remember results of an ANCOVA for a Covariate of interest (e.g. CoVar1) are given as e.g. (ANCOVA Fx,y = 2.72, P-value = 0.01). But which degrees of freedom (df) are usually reported together with an F-value (x and y?)? Which df do I need to extract from the model resp the model summary? best regards, Johannes __ 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] ggplot zoomin
Works for me. If you want a more helpful response, provide a reproducible example of what didn't work. It may also be necessary to explain what 'works' means to you. Also note that ggplot2 has a dedicated forum under Google Groups that may be a better option for this question. --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Ozgul Inceoglu ozgul.inceo...@ulb.ac.be wrote: Dear All, I am plotting a graph in ggplot, I would like to magnify the values between 0-1 without losing data in the higher range. How can I do that? neither scale_y_continous nor coord_cartesian works. Thank you Özgül Université Libre de Bruxelles __ 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-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] how to use ...
On 13-01-17 10:00 PM, John Sorkin wrote: Rolf Perhaps the philosophy of the help system needs to change . . . And perhaps it doesn't. Who knows? Maybe we just need a manual on how to use the existing help system. But I suspect people who won't read the existing manuals won't read that one, either. Duncan Murdoch John Sent from my iPhone On Jan 17, 2013, at 7:11 PM, Rolf Turner rolf.tur...@xtra.co.nz rolf.tur...@xtra.co.nz wrote: The help facility is applicable to functions and data sets. It is not designed or intended to give help with respect to R syntax (with the exception of the basic syntax of the operators --- unary and binary --- and the associated rules of precedence). cheers, Rolf Turner On 01/18/2013 09:17 AM, Steve Taylor wrote: The ellipsis object is not listed in the base help pages! help(`+`) # this works - help on arithmetic operators help(+) # also works help(`...`) # fails with Error: '...' used in an incorrect context help(...) # fails also with No documentation for '...' in specified packages and libraries: you could try '??...' -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Ivan Calandra Sent: Friday, 18 January 2013 4:43a To: R. Michael Weylandt Cc: R list Subject: =?utf-8?B?UmU6IFtSXSBob3cgdG8gdXNlICIuLi4i==? Do you know where I can find some documentation about 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. Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:6}} __ 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-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] Sweave, Texshop, and sync with included Rnw file
On 13-01-17 6:33 PM, michele caseposta wrote: Hi, I just updated R and patchDVI (from CRAN). Now I can reverse search from the pdf to the included.Rnw. However, I cannot forward search from the included to the pdf. Is this how it is expected to work? I think it works if you use first line % !TEX root = Sweavetest.Rnw in the included file, rather than % !Rnw root = Sweavetest.Rnw If not, I think you'll have to ask the TeXShop people. Duncan Murdoch Forward and inverse search work between main Rnw and pdf. I am pasting below the code in the two files. % Sweavetest.Rnw \documentclass{article} \begin{document} \SweaveOpts{concordance=TRUE} \title{an Sweave inclusion test} \author{Michele} \maketitle This is an example for the inclusion of RNW files and forward-inverse search. following is a chunk of sweave code: = n - 5 for(i in 1:n){ print(i) } @ \section{text from an included file} \SweaveInput{included.Rnw} \end{document} % Included.Rnw % !Rnw root = Sweavetest.Rnw this is some text included in an Rnw file = for(i in 1:3){ print('included') } @ On Jan 11, 2013, at 5:38 AM, Duncan Murdoch wrote: On 13-01-10 4:54 PM, michele caseposta wrote: Hi everybody, thanks for the replies. I might have not explained the problem completely. Duncan Mackay: Yes, I am already having a master file and separate Rnw files. Duncan Murdock: I am using patchDVI in the TexShop Sweave engine. Sync works flawlessly between the master file and the pdf produced by pdflatex. My problem is that I don't seem to be able to obtain sync between the *included* Rnws and the pdf, either way. The sweave engine is as follows: #!/bin/bash R CMD Sweave $1 latexmk -pdf -silent -pdflatex=‘pdflatex –shell-escape –synctex=1′${1%.*} Rscript -e library(‘patchDVI’);patchSynctex(‘${1%.*}.synctex.gz’) Funny thing is that the sync works in texworks, using the following Rscript line patchDVI::SweavePDF('$fullname',stylepath=FALSE) I tried to mix and match configurations between texshop and texworks but I had no luck I just tried a simple example in TeXShop and it worked for me. My Sweave engine is #!/bin/tcsh # set path= ($path /usr/local/bin) Rscript -e patchDVI::SweavePDF( '$1' ) So it seems to be work listing versions: On the Mac, I'm using R 2.15.0 patched, rev 59478, with patchDVI version 1.8.1584 (I just uploaded 1.9 to CRAN, by the way), TeXShop version 2.43. It also works on Windows, where I have current releases of R and patchDVI installed. If you've got current versions of everything installed and it's still not working, could you try putting together a small reproducible example? Duncan Murdoch On Jan 10, 2013, at 11:23 AM, Duncan Murdoch wrote: On 13-01-09 9:09 PM, Duncan Murdoch wrote: On 13-01-09 3:25 PM, michele caseposta wrote: Hello everyone. I am in the process of writing a book in Latex with Texshop, on Mac. This book contains a lot of R code, hence the need to use Sweave. I was able to compile Rnw files, and to sync back and forth from the pdf to the source Rnw. My problem now is that the book is divided in Chapters, and every chapter is in its own Rnw file. I can compile them from the main one (book.Rnw) using the directive \SweaveInput{chapter1.Rnw} The problem stands in the fact that like this I am missing synchronization between the pdf and the source Rnw. If part of text is in book.Rnw I can synchronize, but if the text is in one of the included files, it just doesn't work. I am using the sweave engine found in the following webpage: http://cameron.bracken.bz/synctex-with-sweavepgfsweave-in-texshoptexworks Has anybody succeeded in synchronizing with included Rnw files? This is a problem addressed by my patchDVI package, available on R-forge. You have a main file (which can be .tex or .Rnw), and put code at the start of each .Rnw file to indicate where to find it. Then you just run Sweave on one of the chapters, and it automatically produces the full document. The sample document here: http://www.umanitoba.ca/statistics/seminars/2011/3/4/duncan-murdoch-using-sweave-R/ includes an appendix describing how to set this up with TeXShop. I just committed an update to the vignette in patchDVI giving a quick version of the instructions for basic use. Version 1.8.1585 has the new vignette. I should get around to pushing it to CRAN one of these days... Duncan Murdoch __ 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] function approx interpolation of time series data sets
On 16/01/2013, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Like this? data1 - read.table(text = 01:23:40 5 01:23:45 10 01:23:50 12 01:23:55 7 ) data2 - read.table(text = 01:23:42 01:23:47 01:23:51 01:23:54 ) approx(as.POSIXct(data1$V1, format = %H:%M:%S), y = data1$V2, xout = as.POSIXct(data2$V1, format = %H:%M:%S)) Thanks. I have later realised that if data frames are unequal (i.e. data2edit 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 the result for 'y' is $y [1] NA NA NA NA NA NA Similar error occurs with the 'zoo' package. Is it better to do interpolation/further manipulation in another software tool? __ 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] coxph with smooth survival
survreg does work. Hard to tell what went wrong without any code from you. As for smoothing a Cox survival function, see example below. However, just because you can, doesn't mean you should. Chris library(survival) nn - 10 zz - rep(0:1, nn) xx - rexp(2*nn) cc - rexp(2*nn) tt - pmin(xx, cc) dd - xx = cc mod - coxph(Surv(tt,dd)~zz) sf - survfit(mod) steps.y - sf[[c(surv)]][c(1,1+which(diff(sf[[c(surv)]])0))] steps.x - sf[[c(time)]][c(1,1+which(diff(sf[[c(surv)]])0))] plot(sf, conf.int=FALSE) lines(steps.x, steps.y, col=2) smoothfun - approxfun(steps.x, steps.y) plot(smoothfun, from=0, to=3, add=TRUE, col=3, n=1000, lty=2) -Original Message- From: Bond, Stephen [mailto:stephen.b...@cibc.com] Sent: Thursday, January 17, 2013 9:49 AM To: r-help@r-project.org Subject: [R] coxph with smooth survival Hello users, I would like to obtain a survival curve from a Cox model that is smooth and does not have zero differences due to no events for those particular days. I have: sum((diff(surv))==0) [1] 18 So you can see 18 days where the survival curve did not drop due to no events. Is there a way to ask survfit to fit a nice spline for the survival?? Note: I tried survreg and it did not work, but maybe I did not do it properly?? Thank you very much. Stephen B ** Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues __ 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] Naming an object after another object...can it be done?
-Original Message- x-dat.col Now, is there a function (or combination of functions) that will let me assign the character string dat.col to a new object (called y) without actually typing the characters dat$col, i.e. just by referring to x? Yes. dat - data.frame(col=sample(1:8)) namex - function(x) deparse(substitute(x)) y - namex(dat$col) y But wouldn't it be nicer to have the original name follow the data about so you only needed one object? The following mild extension of the above will do that,using attributes: namedvar - function(x) { attr(x, original.name) - deparse(substitute(x)) x #returns the values as a vector with an attribute original.name } #Then y - namedvar(dat$col) plot(y, xlab=attr(y, original.name) ) *** This email and any attachments are confidential. Any use...{{dropped:8}} __ 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] function approx interpolation of time series data sets
On Fri, Jan 18, 2013 at 7:31 AM, e-letter inp...@gmail.com wrote: On 16/01/2013, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Like this? data1 - read.table(text = 01:23:40 5 01:23:45 10 01:23:50 12 01:23:55 7 ) data2 - read.table(text = 01:23:42 01:23:47 01:23:51 01:23:54 ) approx(as.POSIXct(data1$V1, format = %H:%M:%S), y = data1$V2, xout = as.POSIXct(data2$V1, format = %H:%M:%S)) Thanks. I have later realised that if data frames are unequal (i.e. data2edit 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 the result for 'y' is $y [1] NA NA NA NA NA NA Similar error occurs with the 'zoo' package. its not clear precisely what you tried with zoo but have a look at this: data1 - + 01:23:40 5 + 01:23:45 10 + 01:23:50 12 + 01:23:55 7 data2 - + 01:23:42 + 01:23:47 + 01:23:51 + 01:23:54 library(zoo) library(chron) z1 - read.zoo(text = data1, FUN = times) z2 - read.zoo(text = data2, FUN = times) na.approx(z1, xout = time(z2)) 01:23:42 01:23:47 01:23:51 01:23:54 7.0 10.8 11.0 8.0 __ 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] lattice: loess smooths based on y-axis values
Hi there, I'm using the lattice package to create an xy plot of abundance vs. depth for 5 stages of barnacle larvae from 5 species. Each panel of the plot represents a different stage, while different loess smoothers within each panel should represent different species. However, I would like depth to be on the y-axis and abundance to be on the x-axis, because this is more intuitive as an oceanographer. The smoothing curves would then represent the abundance profiles with depth of the larvae. I have tried just switching depth and abundance in the plot formula/coding, but this means that the loess smoother is based on averaging depth values for each abundance, which means that the curves give multiple possible abundances at each depth level. Rather, I would like the loess smooth to still be based on 'averaging' abundance at each depth. Essentially, I would like a mirror image of the plot from this code: ord-order(Zoo_nocyp$Depth_m, decreasing=TRUE) xyplot(log10Ab[ord]~factor(-1*Depth_m[ord])|StageF[ord], data=Zoo_nocyp[Zoo_nocyp$log10Ab!=0 ,], type=c(p,smooth), par.settings=list(strip.background=list(col=white), fontzize=list(text=16,points=10), lty = 0), space=0, border=NA, col=c(blue,red,green,purple,orange), stack=TRUE, groups=Zoo_nocyp$SpeciesF, key= list(title=Species, cex.title=1,text=list(c(BB,BC,CH,SB,VS)),space=right, rectangles=list(size=2, border=white,col=c(blue,red,green,purple,orange))), xlab=depth (m), ylab=log10 abundance, layout=c(2,3)) which is different to what you get when you switch log10Ab and Depth_m. Is there any way to specify the smoothers to be based on sequential y-axis values, rather than x? Or to get the mirror image of a plot? I can provide a dataset offline, for those interested. Thanks very much, Raeanne The Scottish Association for Marine Science (SAMS) is registered in Scotland as a Company Limited by Guarantee (SC009292) and is a registered charity (9206). SAMS has an actively trading wholly owned subsidiary company: SAMS Research Services Ltd a Limited Company (SC224404). All Companies in the group are registered in Scotland and share a registered office at Scottish Marine Institute, Oban Argyll PA37 1QA. The content of this message may contain personal views which are not the views of SAMS unless specifically stated. Please note that all email traffic is monitored for purposes of security and spam filtering. As such individual emails may be examined in more detail. [[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] Nesting fixed factors in lme4 package
Hi, can anyone tell me how to nest two fixed factors using glmer in lme4? I have a split-plot design with two fixed factors - A (whole plot factor) and B (subplot factor), both with two levels. I want to do GLMM as I also want to include different plots as a random factor. But I am interested on the effect of A a B and their interaction on the response variable. I tried this:glmer(response~A*B+(A/B)+(1|C),data=Exp2,family=poisson but it gives the same output as if I removed (A/B) all together or used (A:B) instead thus the output is the same as: glmer(response~A*B+(1|C),data=Exp2,family=poisson anyone can help with how I define this nesting, so that data are analysed correctly given my split-plot design? thanks, Martina [[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] problem that arises after using the new version of BRugs
Respected Sir, With reference to my mail to you and the reply mail by you dated 9th and 16th January, 2013, I am sending the reproducible code in the attached document named MODIFIED ANS . I am also attaching the txt file named hazModel, which is required to save in my documents folder to run the program. The file also contains the error message along with the code. I would be very much obliged if you kindly give me a solution for this question. Thanking you. Moumita Chatterjee Research Scholar University Of Calcutta. x-c(13,5,8,0,11) delta-c(1,1,0,1,0) ZOSull- function (x, range.x, intKnots, drv = 0) { if (drv 2) stop(splines not smooth enough for more than 2 derivatives) library(splines) if (missing(range.x)) range.x - c(1.05 * min(x) - 0.05 * max(x), 1.05 * max(x) - 0.05 * min(x)) if (missing(intKnots)) { numIntKnots - min(length(unique(x)), 35) intKnots - quantile(unique(x), seq(0, 1, length = (numIntKnots + 2))[-c(1, (numIntKnots + 2))]) } numIntKnots - length(intKnots) allKnots - c(rep(range.x[1], 4), intKnots, rep(range.x[2], 4)) K - length(intKnots) L - 3 * (K + 8) xtilde - (rep(allKnots, each = 3)[-c(1, (L - 1), L)] + rep(allKnots, each = 3)[-c(1, 2, L)])/2 wts - rep(diff(allKnots), each = 3) * rep(c(1, 4, 1)/6, K + 7) Bdd - spline.des(allKnots, xtilde, derivs = rep(2, length(xtilde)), outer.ok = TRUE)$design Omega - t(Bdd * wts) %*% Bdd eigOmega - eigen(Omega) indsZ - 1:(numIntKnots + 2) UZ - eigOmega$vectors[, indsZ] LZ - t(t(UZ)/sqrt(eigOmega$values[indsZ])) indsX - (numIntKnots + 3):(numIntKnots + 4) UX - eigOmega$vectors[, indsX] L - cbind(UX, LZ) stabCheck - t(crossprod(L, t(crossprod(L, Omega if (sum(stabCheck^2) 1.0001 * (numIntKnots + 2)) print(WARNING: NUMERICAL INSTABILITY ARISING\\\n FROM SPECTRAL DECOMPOSITION) B - spline.des(allKnots, x, derivs = rep(drv, length(x)), outer.ok = TRUE)$design Z - B %*% LZ attr(Z, range.x) - range.x attr(Z, intKnots) - intKnots return(Z) } BRugsMCMC- function (data, inits, parametersToSave, nBurnin, nIter, nThin, modelFile) { if ((nBurnin 100) | (nIter 100)) stop(currently only working for chains longer than 100) if ((100 * round(nBurnin/100) != nBurnin) | (100 * round(nIter/100) != nIter)) warning(chain lengths not multiples of 100; truncation may occur.) modelCheck(modelFile) modelData(bugsData(data)) modelCompile(numChains = 1) modelInits(bugsInits(inits)) burninIncSize - round(nBurnin/(nThin * 100)) for (i in 1:100) { modelUpdate(burninIncSize, thin = nThin) } samplesSet(parametersToSave) iterIncSize - round(nIter/(nThin * 100)) for (i in 1:100) { modelUpdate(iterIncSize, thin = nThin) } sims - samplesStats(*) return(list(Stats = sims)) } require(BRugs) numKnots - 25 range.x - c(0,202) ox - order(x) x - x[ox] delta - delta[ox] n - length(x) xTil - as.numeric(names(table(x))) cnts - as.numeric(table(x)) nTil - length(xTil) deltaTil - rep(NA, nTil) for (iTil in 1:nTil) deltaTil[iTil] - sum(delta[(1:n)[x == xTil[iTil]]]) sd.xTil - sd(xTil) xTil - xTil/sd.xTil range.x - range.x/sd.xTil d1xTil - xTil[2:nTil] - xTil[1:(nTil - 1)] d2xTil - xTil[3:nTil] - xTil[1:(nTil - 2)] rcsrCnts - rev(cumsum(rev(cnts))) trapLef - 2 * cnts[1] * xTil[1] + (sum(cnts) - cnts[1]) * (xTil[2] + xTil[1]) trapMid - cnts[2:(nTil - 1)] * d1xTil[-length(d1xTil)] + rcsrCnts[1:(nTil - 2)] * d2xTil trapRig - cnts[nTil] * d1xTil[length(d1xTil)] oTil - log(c(trapLef, trapMid, trapRig)/2) X - cbind(rep(1, nTil), xTil) numIntKnots - 25 intKnots - quantile(xTil, seq(0, 1, length = numIntKnots + 2)[-c(1, numIntKnots + 2)]) Z - ZOSull(xTil, intKnots = intKnots, range.x = range.x) numKnots - ncol(Z) allData - list(n = nTil, numKnots = numKnots, x = xTil, delta = deltaTil, offs = oTil, Z = Z) parInits - list(list(beta0 = 0.1, beta1 = 0.1, u = rep(0.1, numKnots), tauU = 1)) # # the file hazModel.txt must be in the # My Documents folder for the next step to run, I am attaching this with my mail. # BRugsMCMC(data = allData, inits = parInits, parametersToSave = c(beta0, beta1, u, tauU), nBurnin =150, nIter =150, nThin = 15, modelFile = hazModel.txt) betaMCMC - rbind(samplesSample(beta0), samplesSample(beta1)) # An error is coming in this
[R] OT: IWSM 2013
dear all, apologizes for this off topic. I would like to inform you that registration and paper submission for the 28th International Workshop on Statistical Modelling (IWSM) to be held in Palermo (Italy) 8-12 July 2013 is now open at http://iwsm2013.unipa.it Register at http://iwsm2013.unipa.it/?cmd=registration and then submit your abstract. Deadlines for Abstract submission is February 4, 2013 and for Early Registration is April 20, 2013. **Invited Speakers** 1)Ciprian Crainiceanu Johns Hopkins University, USA 2)Torsten Hothorn Ludwig-Maximilians-Universität Munchen, DEU 3)Stefano M. Iacus Università di Milano, ITA 4)Geoff McLachlan University of Queensland, AUS 5)Hein Putter Leiden University Medical Cente, NLD **Short Course** (sunday 7 july 2013) J. Fox, ' An Introduction to Structural Equation Modelling with the sem Package for R'. best wishes, Vito Muggeo, on behalf of the IWSM2013 Scientific Committee -- == Vito M.R. Muggeo Dip.to Sc Statist e Matem `Vianelli' Università di Palermo viale delle Scienze, edificio 13 90128 Palermo - ITALY tel: 091 23895240 fax: 091 485726 http://dssm.unipa.it/vmuggeo 28th IWSM International Workshop on Statistical Modelling July 8-12, 2013, Palermo http://iwsm2013.unipa.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.
Re: [R] function approx interpolation of time series data sets
On 18/01/2013, Gabor Grothendieck ggrothendi...@gmail.com wrote: On Fri, Jan 18, 2013 at 7:31 AM, e-letter inp...@gmail.com wrote: On 16/01/2013, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Like this? data1 - read.table(text = 01:23:40 5 01:23:45 10 01:23:50 12 01:23:55 7 ) data2 - read.table(text = 01:23:42 01:23:47 01:23:51 01:23:54 ) approx(as.POSIXct(data1$V1, format = %H:%M:%S), y = data1$V2, xout = as.POSIXct(data2$V1, format = %H:%M:%S)) Thanks. I have later realised that if data frames are unequal (i.e. data2edit 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 the result for 'y' is $y [1] NA NA NA NA NA NA Similar error occurs with the 'zoo' package. its not clear precisely what you tried with zoo but have a look at this: data1 - + 01:23:40 5 + 01:23:45 10 + 01:23:50 12 + 01:23:55 7 data2 - + 01:23:42 + 01:23:47 + 01:23:51 + 01:23:54 library(zoo) library(chron) z1 - read.zoo(text = data1, FUN = times) z2 - read.zoo(text = data2, FUN = times) na.approx(z1, xout = time(z2)) 01:23:42 01:23:47 01:23:51 01:23:54 7.0 10.8 11.0 8.0 With respect to zoo package, looks like I tried to apply zoo package to the vectors directly, not converting them to zoo objects using 'read.zoo ...'. __ 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] function approx interpolation of time series data sets
On 18/01/2013, Gabor Grothendieck ggrothendi...@gmail.com wrote: On Fri, Jan 18, 2013 at 7:31 AM, e-letter inp...@gmail.com wrote: On 16/01/2013, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Like this? data1 - read.table(text = 01:23:40 5 01:23:45 10 01:23:50 12 01:23:55 7 ) data2 - read.table(text = 01:23:42 01:23:47 01:23:51 01:23:54 ) approx(as.POSIXct(data1$V1, format = %H:%M:%S), y = data1$V2, xout = as.POSIXct(data2$V1, format = %H:%M:%S)) Thanks. I have later realised that if data frames are unequal (i.e. data2edit 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 the result for 'y' is $y [1] NA NA NA NA NA NA Similar error occurs with the 'zoo' package. its not clear precisely what you tried with zoo but have a look at this: data1 - + 01:23:40 5 + 01:23:45 10 + 01:23:50 12 + 01:23:55 7 data2 - + 01:23:42 + 01:23:47 + 01:23:51 + 01:23:54 library(zoo) library(chron) z1 - read.zoo(text = data1, FUN = times) z2 - read.zoo(text = data2, FUN = times) na.approx(z1, xout = time(z2)) 01:23:42 01:23:47 01:23:51 01:23:54 7.0 10.8 11.0 8.0 Latest error: data1 V1 V2 1 01:23:40 5 2 01:23:45 10 3 01:23:50 12 4 01:23:55 7 data2 V1 1 01:23:42 2 01:23:47 3 01:23:51 4 01:23:54 5 01:23:58 6 01:23:59 data1zoo-read.zoo(text=data1,FUN=times) Error in textConnection(text) : invalid 'text' argument data2zoo-read.zoo(text=data2,FUN=times) Error in textConnection(text) : invalid 'text' argument Then I tried to create objects differently: data1zoo-read.zoo('test1.txt',FUN=times) data1zoo 01:23:40 01:23:45 01:23:50 01:23:55 5 10 127 data2zoo-read.zoo('test2.txt',FUN=times) data2zoo 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 data3-(na.approx(merge(data1zoo,data2zoo),time(data1zoo))) Error in na.approx.default(object, x = x, xout = xout, na.rm = FALSE, : x and index must have the same length __ 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] Nesting fixed factors in lme4 package
Martina Ozan martina_ozan at hotmail.com writes: Hi, can anyone tell me how to nest two fixed factors using glmer in lme4? I have a split-plot design with two fixed factors - A (whole plot factor) and B (subplot factor), both with two levels. I want to do GLMM as I also want to include different plots as a random factor. But I am interested on the effect of A a B and their interaction on the response variable. I tried this:glmer(response~A*B+(A/B)+(1|C),data=Exp2,family=poisson but it gives the same output as if I removed (A/B) all together or used (A:B) instead thus the output is the same as: glmer(response~A*B+(1|C),data=Exp2,family=poisson anyone can help with how I define this nesting, so that data are analysed correctly given my split-plot design? thanks, Martina In general mixed model questions should go to r-sig-mixed-mod...@r-project.org , but this is actually *not* specifically a mixed model problem. If A and B are fixed factors, you're typically interested in A*B, which translates to 1+A+B+A:B, i.e. intercept; main effects of A and of B; and the interaction. The nesting syntax A/B translates to 1 + A + A:B, i.e. no main effect of B. Nesting would typically make more sense in a random-effects context where the meaning of B=1 in unit A=1 is different from B=1 in unit A=2, i.e. where you don't want or it doesn't make sense to estimate a main effect of B across levels of A. Ben Bolker __ 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] lattice: loess smooths based on y-axis values
Reanne: UNTESTED: I would reverse the xyplot call as xyplot(x~y,...) to get all axes set up properly and then just write an explicit panel function using loess() and predict.loess the correct way, e.g. as loess(y~x) to get the (x,y) curve pairs to be plotted via panel.lines(y,x). You will have to make sure the pairs are ordered in decreasing x. This presumes familiarity with loess() and lattice panel functions, of course. Deepayan's book is a comprehensive resource for the latter. Murrell's also contains a less comprehensive but probably adequate overview. -- If you need anything at all, that is. -- Bert On Fri, Jan 18, 2013 at 3:36 AM, Raeanne Miller raeanne.mil...@sams.ac.uk wrote: Hi there, I'm using the lattice package to create an xy plot of abundance vs. depth for 5 stages of barnacle larvae from 5 species. Each panel of the plot represents a different stage, while different loess smoothers within each panel should represent different species. However, I would like depth to be on the y-axis and abundance to be on the x-axis, because this is more intuitive as an oceanographer. The smoothing curves would then represent the abundance profiles with depth of the larvae. I have tried just switching depth and abundance in the plot formula/coding, but this means that the loess smoother is based on averaging depth values for each abundance, which means that the curves give multiple possible abundances at each depth level. Rather, I would like the loess smooth to still be based on 'averaging' abundance at each depth. Essentially, I would like a mirror image of the plot from this code: ord-order(Zoo_nocyp$Depth_m, decreasing=TRUE) xyplot(log10Ab[ord]~factor(-1*Depth_m[ord])|StageF[ord], data=Zoo_nocyp[Zoo_nocyp$log10Ab!=0 ,], type=c(p,smooth), par.settings=list(strip.background=list(col=white), fontzize=list(text=16,points=10), lty = 0), space=0, border=NA, col=c(blue,red,green,purple,orange), stack=TRUE, groups=Zoo_nocyp$SpeciesF, key= list(title=Species, cex.title=1,text=list(c(BB,BC,CH,SB,VS)),space=right, rectangles=list(size=2, border=white,col=c(blue,red,green,purple,orange))), xlab=depth (m), ylab=log10 abundance, layout=c(2,3)) which is different to what you get when you switch log10Ab and Depth_m. Is there any way to specify the smoothers to be based on sequential y-axis values, rather than x? Or to get the mirror image of a plot? I can provide a dataset offline, for those interested. Thanks very much, Raeanne The Scottish Association for Marine Science (SAMS) is registered in Scotland as a Company Limited by Guarantee (SC009292) and is a registered charity (9206). SAMS has an actively trading wholly owned subsidiary company: SAMS Research Services Ltd a Limited Company (SC224404). All Companies in the group are registered in Scotland and share a registered office at Scottish Marine Institute, Oban Argyll PA37 1QA. The content of this message may contain personal views which are not the views of SAMS unless specifically stated. Please note that all email traffic is monitored for purposes of security and spam filtering. As such individual emails may be examined in more detail. [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm __ 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] plotting from dataframes
Hello, Maybe instead of a loop, you could try lapply(frames, function(y) plot(y$hour1)) Hope this helps, Rui Barradas Em 18-01-2013 09:16, condor escreveu: So by hand the command would be par(mfrow=c(1,2)) plot(frames$'1'hour1) plot(frames$'2'hour1) But in my case there are far more than 2 days, so I want to use a loop. Suppose I have 10 plots par(mfrow=c(2,5)) for(i in 1:10){ plot( /what should be put here??/) } -- View this message in context: http://r.789695.n4.nabble.com/plotting-from-dataframes-tp4655851p4655931.html Sent from the R help mailing list archive at Nabble.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-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] scaling of nonbinROC penalties
Dear R Helpers I am having difficulty understanding how to use the penalty matrix for the nomROC function in package 'nonbinROC'. The documentation says that the values of the penalty matrix code the penalty function L[i,j] in which 0 = L[i,j] = 1 for j i. It gives an example that if we have an ordered response with 4 categories, then we might wish to penalise larger misclassifications more - so there is (for example) 0 penalty for correct classifications, 0.25 penalty for misclassifying by one category, 0.5 penalty for misclassifying by two categories and 1.0 penalty for misclassifying by 3 categories. I wanted to use this sort of penalty - but with equal distances between the 4 categories (0, 1/3, 2/3, 1). But, I found that if I simply re-scale the penalty matrix, while maintaining equal distances between categories, then the estimate of overall accuracy increases. In effect I can achieve any value for accuracy - including unity - by re-scaling the penalty matrix. So, I'd like to ask what, if any are the contraints on the scaling process? Here is a working code that illustrates my difficulty:- set.seed(1); gldstd=round(runif(200)*4); gldstd[gldstd==0]=4; table(gldstd) pred1=gldstd*rnorm(200, mean=1, sd=2/gldstd); boxplot(pred1~gldstd) library(nonbinROC); gldstd=ordered(gldstd) ordered_penalty = matrix(c(0,0,0,0,1/3,0,0,0,2/3,1/3,0,0,1,2/3,1/3,0), nrow = 4) constant_penalty = matrix(c(0,0,0,0,1,0,0,0,1,1,0,0,1,1,1,0), nrow = 4) # first using the constant_penalty (default) matrix ordROC(gldstd,pred1, penalty=constant_penalty) # now re-scaling the constant_penalty matrix by 1/2 ordROC(gldstd,pred1, penalty=constant_penalty/2) # now using an ordered penalty matrix ordROC(gldstd,pred1,penalty=ordered_penalty) # now re-scaling the ordered_penalty matrix by 1/2 ordROC(gldstd,pred1,penalty=ordered_penalty/2) Here is the resulting output:- # first using the constant_penalty (default) matrix ordROC(gldstd,pred1, penalty=constant_penalty) $`Pairwise Accuracy` Pair Estimate Standard.Error 1 1 vs 2 0.6420973 0.05667060 2 1 vs 3 0.7083172 0.05293275 3 1 vs 4 0.8500507 0.04149018 4 2 vs 3 0.6081169 0.05438183 5 2 vs 4 0.7950680 0.04736247 6 3 vs 4 0.7025974 0.05371362 $`Penalty Matrix` 1 2 3 4 1 0 1 1 1 2 0 0 1 1 3 0 0 0 1 4 0 0 0 0 $`Overall Accuracy` Estimate Standard.Error 1 0.7074935 0.02764736 # now re-scaling the constant_penalty matrix by 1/2 ordROC(gldstd,pred1, penalty=constant_penalty/2) $`Pairwise Accuracy` Pair Estimate Standard.Error 1 1 vs 2 0.6420973 0.05667060 2 1 vs 3 0.7083172 0.05293275 3 1 vs 4 0.8500507 0.04149018 4 2 vs 3 0.6081169 0.05438183 5 2 vs 4 0.7950680 0.04736247 6 3 vs 4 0.7025974 0.05371362 $`Penalty Matrix` 1 2 3 4 1 0 0.5 0.5 0.5 2 0 0.0 0.5 0.5 3 0 0.0 0.0 0.5 4 0 0.0 0.0 0.0 $`Overall Accuracy` Estimate Standard.Error 1 0.8537467 0.01382368 === larger estimate of overall accuracy (cf 0.707 for original constant_penalty matrix) # now using an ordered penalty matrix ordROC(gldstd,pred1,penalty=ordered_penalty) $`Pairwise Accuracy` Pair Estimate Standard.Error 1 1 vs 2 0.6420973 0.05667060 2 1 vs 3 0.7083172 0.05293275 3 1 vs 4 0.8500507 0.04149018 4 2 vs 3 0.6081169 0.05438183 5 2 vs 4 0.7950680 0.04736247 6 3 vs 4 0.7025974 0.05371362 $`Penalty Matrix` 1 2 3 4 1 0 0.333 0.667 1.000 2 0 0.000 0.333 0.667 3 0 0.000 0.000 0.333 4 0 0.000 0.000 0.000 $`Overall Accuracy` Estimate Standard.Error 1 0.8616933 0.01622384 # now re-scaling the ordered_penalty matrix by 1/2 ordROC(gldstd,pred1,penalty=ordered_penalty/2) $`Pairwise Accuracy` Pair Estimate Standard.Error 1 1 vs 2 0.6420973 0.05667060 2 1 vs 3 0.7083172 0.05293275 3 1 vs 4 0.8500507 0.04149018 4 2 vs 3 0.6081169 0.05438183 5 2 vs 4 0.7950680 0.04736247 6 3 vs 4 0.7025974 0.05371362 $`Penalty Matrix` 1 2 3 4 1 0 0.167 0.333 0.500 2 0 0.000 0.167 0.333 3 0 0.000 0.000 0.167 4 0 0.000 0.000 0.000 $`Overall Accuracy` Estimate Standard.Error 1 0.9308467 0.00811192 === larger estimate of overall accuracy (cf 0.862 for original ordered_penalty matrix) I can see that penalising differences between categories less might produce better overall accuracy. However, if I use a constant penalty matrix with all off-diagonal values very close to zero, then the overall accuracy approaches 1. It seems counter-intuitive to me that the estimate of overall accuracy for an ordinal gold standard should depend on the absolute values of the penalty matrix. So, I would like to ask, (a) should the penalty matrix always contain at least one penalty with a value of 1 and/or (b) should there be any other constraint on the sum of penalties in the matrix (e.g. should the matrix sum to some
[R] eliminate double entries in data.frame
hello together, i want to eliminate double entries in a data.frame. I have a data.frame, like this one: 1 2 A Albert1800 B Albert1800 C Jack2000 D Mike 2200 As you can see, Albert is two times available. I want a solution like this one: 1 2 A Albert1800 B Jack2000 C Mike 2200 How can i eliminate this double entries? Thanks. Mat? -- View this message in context: http://r.789695.n4.nabble.com/eliminate-double-entries-in-data-frame-tp4655956.html Sent from the R help mailing list archive at Nabble.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] Classification by Standard Deviation of Lognormal with Weight
Hi, We got a actuarial question which cannot be solved in Excel, so we are wondering if R can help us on it. As the sample table below, variable X has 50 different values and the weighted Y has a lognormal distribution. We want to make X into four or five classes, based on the standard deviation ó in lognormal of the weighted Y. X Y Weight of Y 01 500 2 01 600 1 02 350 3 03 400 3 03 550 2 04 200 4 ... ... ... If it is doable in R, can you please give us some suggestions? Thank you very much. Best regards, Fiona [[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] reading multiple key=value pairs per line
HI, May be this helps: Lines1-readLines(textConnection('key1=23, key2=67, key3=hello there key1=7, key2=22, key3=how are you key1=2, key2=77, key3=nice day, thanks')) res-read.table(text=gsub(key{0,1}\\d,,gsub([\,],,Lines1)),sep==,header=FALSE,stringsAsFactors=F)[-1] names(res)- paste(substr(Lines1,1,3),1:3,sep=) res # key1 key2 key3 #1 23 67 hello there #2 7 22 how are you #3 2 77 nice day thanks A.K. - Original Message - From: Frank Singleton b17fly...@gmail.com To: r-help@r-project.org Cc: Sent: Thursday, January 17, 2013 11:21 PM Subject: [R] reading multiple key=value pairs per line Hi, Thanks for a great environmentfor statistical computing :-) I have some input data in a file (input_kvpairs.csv) of the form key1=23, key2=67, key3=hello there key1=7, key2=22, key3=how are you key1=2, key2=77, key3=nice day, thanks Now in my head I wish it was of the form (input.csv) #key1, key2, key3 23, 67, hello there 7, 22, how are you 2, 77, nice day, thanks so I could do data - read.csv(input.csv, header=TRUE) where the header column names are derived from the key names dynamically, and I could access the data using normal data$key1 or data$key2 mechanism. I guess I could just pre process the file first using python etc to create a CSV file with column header derived from key names, and values derived from key values, but I am interested to see how experienced R folks would handle this inside R. Thanks, Frank __ 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-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] reading multiple key=value pairs per line
Hi, Sorry, there was a mistake. I didn't notice comma in key3 .Lines1-readLines(textConnection('key1=23, key2=67, key3=hello there key1=7, key2=22, key3=how are you key1=2, key2=77, key3=nice day, thanks')) res1-read.table(text=gsub(key{0,1}\\d,,gsub([\],,Lines1)),sep==,header=FALSE,stringsAsFactors=F)[-1] res1[,1:2]-sapply(res1[,1:2],function(x) as.numeric(gsub(\\, $,,x))) names(res1)- paste(substr(Lines1,1,3),1:3,sep=) res1 # key1 key2 key3 #1 23 67 hello there #2 7 22 how are you #3 2 77 nice day, thanks str(res1) #'data.frame': 3 obs. of 3 variables: # $ key1: num 23 7 2 # $ key2: num 67 22 77 # $ key3: chr hello there how are you nice day, thanks A.K. - Original Message - From: Frank Singleton b17fly...@gmail.com To: r-help@r-project.org Cc: Sent: Thursday, January 17, 2013 11:21 PM Subject: [R] reading multiple key=value pairs per line Hi, Thanks for a great environmentfor statistical computing :-) I have some input data in a file (input_kvpairs.csv) of the form key1=23, key2=67, key3=hello there key1=7, key2=22, key3=how are you key1=2, key2=77, key3=nice day, thanks Now in my head I wish it was of the form (input.csv) #key1, key2, key3 23, 67, hello there 7, 22, how are you 2, 77, nice day, thanks so I could do data - read.csv(input.csv, header=TRUE) where the header column names are derived from the key names dynamically, and I could access the data using normal data$key1 or data$key2 mechanism. I guess I could just pre process the file first using python etc to create a CSV file with column header derived from key names, and values derived from key values, but I am interested to see how experienced R folks would handle this inside R. Thanks, Frank __ 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-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] function approx interpolation of time series data sets
Hello, Both Gabor's and my way work and produce the same results: #-- Gabor library(zoo) library(chron) data1 - 01:23:40 5 01:23:45 10 01:23:50 12 01:23:55 7 data2 - 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 data1zoo - read.zoo(text=data1, FUN=times) data2zoo - read.zoo(text=data2, FUN=times) na.approx(data1zoo, xout = time(data2zoo)) #-- Rui data1 - read.table(text = 01:23:40 5 01:23:45 10 01:23:50 12 01:23:55 7 ) data2 - read.table(text = 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 ) approx(as.POSIXct(data1$V1, format = %H:%M:%S), y = data1$V2, xout = as.POSIXct(data2$V1, format = %H:%M:%S)) Note that the last two values of data2 are outside the range of data1, and therefore the interpolation functions return nothing (zoo::na.approx) or NA (stats::approx) Hope this helps, Rui Barradas Em 18-01-2013 13:51, e-letter escreveu: On 18/01/2013, Gabor Grothendieck ggrothendi...@gmail.com wrote: On Fri, Jan 18, 2013 at 7:31 AM, e-letter inp...@gmail.com wrote: On 16/01/2013, Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Like this? data1 - read.table(text = 01:23:40 5 01:23:45 10 01:23:50 12 01:23:55 7 ) data2 - read.table(text = 01:23:42 01:23:47 01:23:51 01:23:54 ) approx(as.POSIXct(data1$V1, format = %H:%M:%S), y = data1$V2, xout = as.POSIXct(data2$V1, format = %H:%M:%S)) Thanks. I have later realised that if data frames are unequal (i.e. data2edit 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 the result for 'y' is $y [1] NA NA NA NA NA NA Similar error occurs with the 'zoo' package. its not clear precisely what you tried with zoo but have a look at this: data1 - + 01:23:40 5 + 01:23:45 10 + 01:23:50 12 + 01:23:55 7 data2 - + 01:23:42 + 01:23:47 + 01:23:51 + 01:23:54 library(zoo) library(chron) z1 - read.zoo(text = data1, FUN = times) z2 - read.zoo(text = data2, FUN = times) na.approx(z1, xout = time(z2)) 01:23:42 01:23:47 01:23:51 01:23:54 7.0 10.8 11.0 8.0 Latest error: data1 V1 V2 1 01:23:40 5 2 01:23:45 10 3 01:23:50 12 4 01:23:55 7 data2 V1 1 01:23:42 2 01:23:47 3 01:23:51 4 01:23:54 5 01:23:58 6 01:23:59 data1zoo-read.zoo(text=data1,FUN=times) Error in textConnection(text) : invalid 'text' argument data2zoo-read.zoo(text=data2,FUN=times) Error in textConnection(text) : invalid 'text' argument Then I tried to create objects differently: data1zoo-read.zoo('test1.txt',FUN=times) data1zoo 01:23:40 01:23:45 01:23:50 01:23:55 5 10 127 data2zoo-read.zoo('test2.txt',FUN=times) data2zoo 01:23:42 01:23:47 01:23:51 01:23:54 01:23:58 01:23:59 data3-(na.approx(merge(data1zoo,data2zoo),time(data1zoo))) Error in na.approx.default(object, x = x, xout = xout, na.rm = FALSE, : x and index must have the same length __ 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] eliminate double entries in data.frame
thx, works perfectly :-) Mat -- View this message in context: http://r.789695.n4.nabble.com/eliminate-double-entries-in-data-frame-tp4655956p4655963.html Sent from the R help mailing list archive at Nabble.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.
Re: [R] plotting from dataframes
You need to (re-) read the Introduction to R document that comes with R, particularly about indexing lists. Briefly, there are three ways: integer, string, and approximate string indexing. You seem to be stuck now using approximate string indexing with the $ operator. Integer indexing is more appropriate for your loop. for(i in 1:10){ plot( frames[[ i ]]$hour1 ) } --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. condor radonniko...@hotmail.nl wrote: So by hand the command would be par(mfrow=c(1,2)) plot(frames$'1'hour1) plot(frames$'2'hour1) But in my case there are far more than 2 days, so I want to use a loop. Suppose I have 10 plots par(mfrow=c(2,5)) for(i in 1:10){ plot( /what should be put here??/) } -- View this message in context: http://r.789695.n4.nabble.com/plotting-from-dataframes-tp4655851p4655931.html Sent from the R help mailing list archive at Nabble.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-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] sem package, suppress warnings
Hi, I'm using the sem package under conditions where I know beforehand that several models will be problematic. Because of that, I want to suppress warnings. I've used the following code sem(mod, S = as.matrix(dataset), N = 1000, maxiter = 1, warn = FALSE) But I still get warnings. I've also tried adding check.analytic = FALSE, debug = FALSE, and that doesn't seem to do it either. Any ideas why it's not working? It may be important to note that the command above is wrapped within a function and I'm using try() to avoid errors. Thanks in advance! -- Dustin Fife PhD Student Quantitative Psychology University of Oklahoma [[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] lattice: loess smooths based on y-axis values
Yes ... that's much better. RTFM, Bert. -- Bert On Fri, Jan 18, 2013 at 7:57 AM, Raeanne Miller raeanne.mil...@sams.ac.uk wrote: Hi Bert, Thanks for your answer - I've done a good bit of research in to panel functions this afternoon, and they're starting to seem a *little* less intimidating. I didn't know there was a panel.lines, so that's very helpful! Turns out that panel.loess has an argument 'horizontal', which when set to 'TRUE' bases the smooth on the y axis, as opposed to the x. So by using a panel function: panel.groups = function(x, y, ..., lty) { panel.xyplot(x, y, ...) panel.loess(x,y, horizontal=TRUE, lty=lty,...) } in my call to xyplot, it seems I can get the smooth the way I want it, with Depth on the y axis and abundance on the x axis. Hope that is useful for anyone else interested - and Bert, thanks again for the swift reply! Raeanne -Original Message- From: Bert Gunter [mailto:gunter.ber...@gene.com] Sent: 18 January 2013 15:08 To: Raeanne Miller Cc: r-help@r-project.org Subject: Re: [R] lattice: loess smooths based on y-axis values Reanne: UNTESTED: I would reverse the xyplot call as xyplot(x~y,...) to get all axes set up properly and then just write an explicit panel function using loess() and predict.loess the correct way, e.g. as loess(y~x) to get the (x,y) curve pairs to be plotted via panel.lines(y,x). You will have to make sure the pairs are ordered in decreasing x. This presumes familiarity with loess() and lattice panel functions, of course. Deepayan's book is a comprehensive resource for the latter. Murrell's also contains a less comprehensive but probably adequate overview. -- If you need anything at all, that is. -- Bert On Fri, Jan 18, 2013 at 3:36 AM, Raeanne Miller raeanne.mil...@sams.ac.uk wrote: Hi there, I'm using the lattice package to create an xy plot of abundance vs. depth for 5 stages of barnacle larvae from 5 species. Each panel of the plot represents a different stage, while different loess smoothers within each panel should represent different species. However, I would like depth to be on the y-axis and abundance to be on the x-axis, because this is more intuitive as an oceanographer. The smoothing curves would then represent the abundance profiles with depth of the larvae. I have tried just switching depth and abundance in the plot formula/coding, but this means that the loess smoother is based on averaging depth values for each abundance, which means that the curves give multiple possible abundances at each depth level. Rather, I would like the loess smooth to still be based on 'averaging' abundance at each depth. Essentially, I would like a mirror image of the plot from this code: ord-order(Zoo_nocyp$Depth_m, decreasing=TRUE) xyplot(log10Ab[ord]~factor(-1*Depth_m[ord])|StageF[ord], data=Zoo_nocyp[Zoo_nocyp$log10Ab!=0 ,], type=c(p,smooth), par.settings=list(strip.background=list(col=white), fontzize=list(text=16,points=10), lty = 0), space=0, border=NA, col=c(blue,red,green,purple,orange), stack=TRUE, groups=Zoo_nocyp$SpeciesF, key= list(title=Species, cex.title=1,text=list(c(BB,BC,CH,SB,VS)),space=right, rectangles=list(size=2, border=white,col=c(blue,red,green,purple,orange))), xlab=depth (m), ylab=log10 abundance, layout=c(2,3)) which is different to what you get when you switch log10Ab and Depth_m. Is there any way to specify the smoothers to be based on sequential y-axis values, rather than x? Or to get the mirror image of a plot? I can provide a dataset offline, for those interested. Thanks very much, Raeanne The Scottish Association for Marine Science (SAMS) is registered in Scotland as a Company Limited by Guarantee (SC009292) and is a registered charity (9206). SAMS has an actively trading wholly owned subsidiary company: SAMS Research Services Ltd a Limited Company (SC224404). All Companies in the group are registered in Scotland and share a registered office at Scottish Marine Institute, Oban Argyll PA37 1QA. The content of this message may contain personal views which are not the views of SAMS unless specifically stated. Please note that all email traffic is monitored for purposes of security and spam filtering. As such individual emails may be examined in more detail. [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website:
[R] How to re-project ease( Equal Area Scalable Earth) grid with a ~25 km cylindrical projection to WGS84 0.25 degree?
I have nc files for global soil moisture,here is one file https://echange-fichiers.inra.fr/get?k=f9DDllPKdUKs5ZNQwfq from the metadata ,the projection is cylindrical and the resolution is 25 km(it is based on authalic sphere based on International 1924 ellipsoid).As I want to compare with other data, I have to make them identical. - my other data are in WGS84 with 0.25*0.25 degree resolution and extent -180, 180, -90, 90. So I want to re-project the data I have here from EASE grid, cylindrical,25 km ,1383 pixel 586 lines to WGS84,0.25*0.25 degree,1440 pixel 720 lines: f=open.ncdf(C:\\Users\\aalyaari\\Desktop\\SM_RE01_MIR_CLF31D_20100812T00_20100812T235959_246_001_7.DBL) A = get.var.ncdf(nc=f,varid=Soil_Moisture,verbose=TRUE) I tried this: d -raster(C:\\Users\\aalyaari\\Desktop\\SM_RE01_MIR_CLF31D_20100812T00_20100812T235959_246_001_7.nc, varname = Soil_Moisture) d: class : RasterLayer dimensions : 586, 1383, 810438 (nrow, ncol, ncell) resolution : 0.2603037, 0.2916659 (x, y) extent : -180, 180, -85.4581, 85.4581 (xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 names : Retrieved.soil.moisture.value zvar: Soil_Moisture a - spTransform(d, CRS (+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs)) but I got this error: Error in function (classes, fdef, mtable) : unable to find an inherited method for function ‘spTransform’ for signature ‘RasterLayer, CRS’ -- View this message in context: http://r.789695.n4.nabble.com/How-to-re-project-ease-Equal-Area-Scalable-Earth-grid-with-a-25-km-cylindrical-projection-to-WGS84-0-tp4655977.html Sent from the R help mailing list archive at Nabble.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] longitudinal study
Hello R user, I have a data set from a longitudinal study ( sample below) where subjects are followed over time. Second column (status) contains info about if subject is dead or still in the study and third column is time measured in the week. Here is what I need: if status is not dead or unknown take the last week, if status is dead or unknown I need to have corresponding week. Desired resulst: 1 no 7 2 yes 5 3 Unknown 4 Sample data id status week 1 no 1 1 no 2 1 no 3 1 no 4 1 no 5 1 no 6 1 no 7 2 no 1 2 no 2 2 no 3 2 no 4 2 yes 5 2 yes 6 2 na 7 2 na 8 2 na 9 3 no 1 3 no 2 3 no 3 3 Unknown 4 3 unknown 5 3 na 6 3 na 7 3 na 8 Any suggestion is much appreciated! Thank you. Bibek __ 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] longitudinal study
On Fri, 18 Jan 2013, bibek sharma wrote: I have a data set from a longitudinal study ( sample below) where subjects are followed over time. Second column (status) contains info about if subject is dead or still in the study and third column is time measured in the week. Here is what I need: if status is not dead or unknown take the last week, if status is dead or unknown I need to have corresponding week. Desired resulst: 1 no 7 2 yes 5 3 Unknown 4 Looks like a survival analysis situation. I know there are R packages for this. Rich __ 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] How to convert a string to the column it represents in a dataframe, with a reproducible example
Many thanks to everyone who chimed in on this one. I really appreciate the time you took to help me. Especially Dave W. who made me question what exactly I was after. Dan N.'s solution does exactly what I want, and it helped me learn about the eval() and parse() functions too. Thanks again, and all the best. Mark Na On Thu, Jan 17, 2013 at 5:15 PM, Nordlund, Dan (DSHS/RDA) nord...@dshs.wa.gov wrote: -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of mtb...@gmail.com Sent: Thursday, January 17, 2013 2:27 PM To: David Winsemius; r-help@r-project.org Subject: Re: [R] How to convert a string to the column it represents in a dataframe, with a reproducible example Hi David, I would like to have two objects, one containing the values in a column and the other containing the column's name. Of course, that's easy to do manually, but I don't want to have to type out the name of the column more than once (thus, below, I have typed it once in quotes, and I am trying to find a programatic way to create the other object, without typing the column name again). Thank you for your help. Mark Na Something like this eval(parse(text=y)) could be what you want. But even if it is, I am not sure it is what you should want. Without more context, it is hard to say. Hope this is at least somewhat helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204 __ 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. [[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] columns called X rename Y
I have a data. frame to which you want to change the names to some of their columns. For example: seba - data.frame ('constant' = 3, 'numbers' = 1: 10, 'letters' = LETTERS [1:10], otros = 2:11) List their names: names (Seba) [1] constant numbers letters I want to rename c the column called constant and b the column called numbers. I do not know in which order appear names. How could do it? Thanks in advance, Sebastian. [[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 tab delimited file from a certain line
Christof, I've added support for this to the R.filesets package. In your case, then all you need to do is: library(R.filesets) dlf - readDataFrame(filename, skip=^year) No need to specify any other arguments - they're all automagically inferred - and the default is stringsAsFactors=FALSE. This is in R.filesets v2.0.0 which I still haven't published on CRAN. In the meanwhile, you can install it via: source(http://aroma-project.org/hbLite.R;) hbLite(R.filesets) /Henrik On Thu, Jan 17, 2013 at 1:34 AM, Christof Kluß ckl...@email.uni-kiel.de wrote: Hello thank you for the fast and helpful answer! Now the following works fine for me x - readLines(filename) i - grep(^year, x) dlf - read.table(textConnection(x[i:length(x)]), header = T, stringsAsFactors=F,sep=\t) Greetings Christof Am 16-01-2013 16:55, schrieb Rui Barradas: Hello, Read the file using readLines, then grep ^year. You can then use a textConnection to read.table: x - readLines(con = textConnection( informations (unknown count of lines) ... and at some point the table -- year month mday value 2013 1 16 0 )) # This is it i - grep(^year, x) read.table(textConnection(x[i:length(x)]), header = TRUE) Hope this helps, Rui Barradas Em 16-01-2013 14:17, Christof Kluß escreveu: Hi I would like to read table data from a text-files with extra informations in the header (of unknown line count). Example: informations (unknown count of lines) ... and at some point the table -- year month mday value 2013 1 16 0 ... If it was an excel file I could use something like read.xls(..., pattern=year) But it is a simple tab seperated text-file. Is there an easy way to read only the table? (Without dirty things ;)) Thx Christof __ 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-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-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] lattice: loess smooths based on y-axis values
Hi Bert, Thanks for your answer - I've done a good bit of research in to panel functions this afternoon, and they're starting to seem a *little* less intimidating. I didn't know there was a panel.lines, so that's very helpful! Turns out that panel.loess has an argument 'horizontal', which when set to 'TRUE' bases the smooth on the y axis, as opposed to the x. So by using a panel function: panel.groups = function(x, y, ..., lty) { panel.xyplot(x, y, ...) panel.loess(x,y, horizontal=TRUE, lty=lty,...) } in my call to xyplot, it seems I can get the smooth the way I want it, with Depth on the y axis and abundance on the x axis. Hope that is useful for anyone else interested - and Bert, thanks again for the swift reply! Raeanne -Original Message- From: Bert Gunter [mailto:gunter.ber...@gene.com] Sent: 18 January 2013 15:08 To: Raeanne Miller Cc: r-help@r-project.org Subject: Re: [R] lattice: loess smooths based on y-axis values Reanne: UNTESTED: I would reverse the xyplot call as xyplot(x~y,...) to get all axes set up properly and then just write an explicit panel function using loess() and predict.loess the correct way, e.g. as loess(y~x) to get the (x,y) curve pairs to be plotted via panel.lines(y,x). You will have to make sure the pairs are ordered in decreasing x. This presumes familiarity with loess() and lattice panel functions, of course. Deepayan's book is a comprehensive resource for the latter. Murrell's also contains a less comprehensive but probably adequate overview. -- If you need anything at all, that is. -- Bert On Fri, Jan 18, 2013 at 3:36 AM, Raeanne Miller raeanne.mil...@sams.ac.uk wrote: Hi there, I'm using the lattice package to create an xy plot of abundance vs. depth for 5 stages of barnacle larvae from 5 species. Each panel of the plot represents a different stage, while different loess smoothers within each panel should represent different species. However, I would like depth to be on the y-axis and abundance to be on the x-axis, because this is more intuitive as an oceanographer. The smoothing curves would then represent the abundance profiles with depth of the larvae. I have tried just switching depth and abundance in the plot formula/coding, but this means that the loess smoother is based on averaging depth values for each abundance, which means that the curves give multiple possible abundances at each depth level. Rather, I would like the loess smooth to still be based on 'averaging' abundance at each depth. Essentially, I would like a mirror image of the plot from this code: ord-order(Zoo_nocyp$Depth_m, decreasing=TRUE) xyplot(log10Ab[ord]~factor(-1*Depth_m[ord])|StageF[ord], data=Zoo_nocyp[Zoo_nocyp$log10Ab!=0 ,], type=c(p,smooth), par.settings=list(strip.background=list(col=white), fontzize=list(text=16,points=10), lty = 0), space=0, border=NA, col=c(blue,red,green,purple,orange), stack=TRUE, groups=Zoo_nocyp$SpeciesF, key= list(title=Species, cex.title=1,text=list(c(BB,BC,CH,SB,VS)),space=right, rectangles=list(size=2, border=white,col=c(blue,red,green,purple,orange))), xlab=depth (m), ylab=log10 abundance, layout=c(2,3)) which is different to what you get when you switch log10Ab and Depth_m. Is there any way to specify the smoothers to be based on sequential y-axis values, rather than x? Or to get the mirror image of a plot? I can provide a dataset offline, for those interested. Thanks very much, Raeanne The Scottish Association for Marine Science (SAMS) is registered in Scotland as a Company Limited by Guarantee (SC009292) and is a registered charity (9206). SAMS has an actively trading wholly owned subsidiary company: SAMS Research Services Ltd a Limited Company (SC224404). All Companies in the group are registered in Scotland and share a registered office at Scottish Marine Institute, Oban Argyll PA37 1QA. The content of this message may contain personal views which are not the views of SAMS unless specifically stated. Please note that all email traffic is monitored for purposes of security and spam filtering. As such individual emails may be examined in more detail. [[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. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm The Scottish Association for Marine Science (SAMS) is registered in Scotland as a Company Limited by Guarantee (SC009292) and is a registered
Re: [R] plotting from dataframes
Hi, May be this helps: frames-list(data.frame(c1=1:3,day1=17,hour1=c(10,11,6)),data.frame(c1=6:7,day1=19,hour1=8),data.frame(c1=8:10,day1=21,hour1=c(11,15,18)),data.frame(c1=12:13,day1=23,hour1=7)) par(mfrow=c(2,2)) lapply(seq_along(frames),function(i) plot(frames[[i]][,3])) A.K. - Original Message - From: condor radonniko...@hotmail.nl To: r-help@r-project.org Cc: Sent: Friday, January 18, 2013 4:16 AM Subject: Re: [R] plotting from dataframes So by hand the command would be par(mfrow=c(1,2)) plot(frames$'1'hour1) plot(frames$'2'hour1) But in my case there are far more than 2 days, so I want to use a loop. Suppose I have 10 plots par(mfrow=c(2,5)) for(i in 1:10){ plot( /what should be put here??/) } -- View this message in context: http://r.789695.n4.nabble.com/plotting-from-dataframes-tp4655851p4655931.html Sent from the R help mailing list archive at Nabble.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-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] Thermoisoplethendiagramm
Hello, I would like to generate isolines of temperature for the 24 hours by the 12 month. Something like hier: http://www.diercke.de/bilder/omeda/800/12351E.jpg. On the x-axis are the month and on the y-axis the 24 hours, than as contour lines the temperature. My data are: 'data.frame': 288 obs. of 3 variables: $ Group.1: num 0 1 2 3 4 5 6 7 8 9 ... $ Group.2: num 1 1 1 1 1 1 1 1 1 1 ... $ x : num 6.99 6.66 6.36 6.1 5.94 ... Group 1: hour Group 2: month x: temperature ºC Have anybody an idea? I have tried without success the command filled.contour(). Many thanks!! Best regards, Dominic [[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] longitudinal study
HI, May be this helps: dat1-read.table(text= id status week 1 no 1 1 no 2 1 no 3 1 no 4 1 no 5 1 no 6 1 no 7 2 no 1 2 no 2 2 no 3 2 no 4 2 yes 5 2 yes 6 2 na 7 2 na 8 2 na 9 3 no 1 3 no 2 3 no 3 3 Unknown 4 3 unknown 5 3 na 6 3 na 7 3 na 8 ,sep=,header=TRUE,stringsAsFactors=FALSE,na.strings=na) dat2-dat1[complete.cases(dat1),] res-do.call(rbind,lapply(split(dat2,dat2$id),function(x) rbind(tail(x[all(x[,2]==no)],1),head(x[x[,2]==yes|x[,2]==Unknown,],1 res # id status week #1 1 no 7 #2 2 yes 5 #3 3 Unknown 4 A.K. - Original Message - From: Rich Shepard rshep...@appl-ecosys.com To: R help r-help@r-project.org Cc: Sent: Friday, January 18, 2013 12:18 PM Subject: Re: [R] longitudinal study On Fri, 18 Jan 2013, bibek sharma wrote: I have a data set from a longitudinal study ( sample below) where subjects are followed over time. Second column (status) contains info about if subject is dead or still in the study and third column is time measured in the week. Here is what I need: if status is not dead or unknown take the last week, if status is dead or unknown I need to have corresponding week. Desired resulst: 1 no 7 2 yes 5 3 Unknown 4 Looks like a survival analysis situation. I know there are R packages for this. Rich __ 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-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] Hclust tree to Figtree w/ branch lengths
Hi, I'm doing hierarchical clustering, and want to export my dendrogram to a tree-viewing/editing software. I can do this by converting the data to Newick format (hc2Newick in ctc package), but I can't get branch lengths to show in the resulting phylogram. I figured it might help to convert my hclust object into a phylo object (as.phylo in ape package), but the following lines give me this Error in UseMethod(as.phylo) : no applicable method for 'as.phylo' applied to an object of class character: data = read.table(C:/path/data.txt) d - dist(data, method = euclidean) library(ape) fit - as.phylo(hclust(d, method=ward) library(ctc) write.table(as.phylo(hc2Newick(fit), file=C:/path/output.txt,row.names=FALSE,col.names=FALSE)) Would anyone have an idea about how to make this work? Thanks so much in advance! Julian -- View this message in context: http://r.789695.n4.nabble.com/Hclust-tree-to-Figtree-w-branch-lengths-tp4655990.html Sent from the R help mailing list archive at Nabble.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.
Re: [R] Thermoisoplethendiagramm
On Jan 18, 2013, at 9:49 AM, Dominic Roye wrote: Hello, I would like to generate isolines of temperature for the 24 hours by the 12 month. Something like hier: http://www.diercke.de/bilder/omeda/800/12351E.jpg. On the x-axis are the month and on the y-axis the 24 hours, than as contour lines the temperature. My data are: 'data.frame': 288 obs. of 3 variables: $ Group.1: num 0 1 2 3 4 5 6 7 8 9 ... $ Group.2: num 1 1 1 1 1 1 1 1 1 1 ... $ x : num 6.99 6.66 6.36 6.1 5.94 ... Perhaps: require(lattice) levelplot(x ~ Group.2 + Group.1, data =dfrm) Group 1: hour Group 2: month x: temperature ºC Have anybody an idea? I have tried without success the command filled.contour(). I believe filled contour would have required the z argument to be a matrix, i.e. you owuld haveneeded to recast the data in wide format. -- David Winsemius Alameda, CA, USA __ 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] A smart way to use $ in data frame
Hello all, I have a data frame dataa: newdate newstate newid newbalance newaccounts 1 31DEC2001AR 1 1170 61 2 31DEC2001VA 2 4565 54 3 31DEC2001WA 3 2726 35 4 31DEC2001AR 3 2700 35 The following gives me the balance of state AR: dataa$newbalance[data$newstate == 'AR'] 1170 2700 Now, I have another different data frame datab, it is very similar to data, except that the name of the columns are different, and the order of the columns are different: oldstate olddate oldbalance oldid oldaccounts 1 AR 31DEC20121234 7 40 2 WA 31DEC2012 3 30 3 VA 31DEC20122345 5 23 3 AR 31DEC20125673 5 23 datab$oldbalance[datab$oldstate== 'AR' ] 1234 5673 Could I have a way to quote data$balance[data$state == 'AR'] in general, where balance=oldbalance, state=oldstate when data=dataa, and balance = newbalance, state = newstate when data=datab ? Thanks very much! Cheers, Rebecca -- This message, and any attachments, is for the intended r...{{dropped:5}} __ 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] columns called X rename Y
Hello, Try the following. names(seba)[grep(numbers, names(seba))] - b names(seba)[grep(constant, names(seba))] - c names(seba) Hope this helps, Rui Barradas Em 18-01-2013 18:14, Sebastian Kruk escreveu: I have a data. frame to which you want to change the names to some of their columns. For example: seba - data.frame ('constant' = 3, 'numbers' = 1: 10, 'letters' = LETTERS [1:10], otros = 2:11) List their names: names (Seba) [1] constant numbers letters I want to rename c the column called constant and b the column called numbers. I do not know in which order appear names. How could do it? Thanks in advance, Sebastian. [[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-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] A smart way to use $ in data frame
On 18/01/2013 2:40 PM, Yuan, Rebecca wrote: Hello all, I have a data frame dataa: newdate newstate newid newbalance newaccounts 1 31DEC2001AR 1 1170 61 2 31DEC2001VA 2 4565 54 3 31DEC2001WA 3 2726 35 4 31DEC2001AR 3 2700 35 The following gives me the balance of state AR: dataa$newbalance[data$newstate == 'AR'] 1170 2700 Now, I have another different data frame datab, it is very similar to data, except that the name of the columns are different, and the order of the columns are different: oldstate olddate oldbalance oldid oldaccounts 1 AR 31DEC20121234 7 40 2 WA 31DEC2012 3 30 3 VA 31DEC20122345 5 23 3 AR 31DEC20125673 5 23 datab$oldbalance[datab$oldstate== 'AR' ] 1234 5673 Could I have a way to quote data$balance[data$state == 'AR'] in general, where balance=oldbalance, state=oldstate when data=dataa, and balance = newbalance, state = newstate when data=datab ? Yes, you could use the name of the dataframe to get the column names, e.g. state - c(dataa=oldstate, datab=newstate) balance - c(dataa=oldbalance, datab=newbalance) dfname - dataa df - get(dfname) df[ df[,state[dfname]] == 'AR', balance[dfname]] but that is really, really unreadable code. You would be much better off to name the columns consistently in the first place. Duncan Murdoch __ 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] tables package: How to remove column headings and alignment issues
Dear expeRts, Here is a minimal example with the latest version of 'tables' (questions below): require(tables) saveopts - table_options(toprule=\\toprule, midrule=\\midrule, bottomrule=\\bottomrule, titlerule=\\cmidrule(lr), rowlabeljustification=r)#, justification=r) ## data.frame x - expand.grid(beta=as.factor(c(95, 99, 99.9)), m=as.factor(c(64, 256)), p=as.factor(c(8, 64, 512)), f=as.factor(c(A, B)), gamma=as.factor(c(0.25, 0.5))) x - cbind(x, value=1:nrow(x)) ## tabular (tab - tabular(Heading($p$) * Justify(c) * p * Heading($m$) * Justify(c) * m * Heading($\\gamma$) * Justify(c) * gamma ~ Heading() * f * Heading() * beta * Heading() * value * Heading() * identity, data=x)) latex(tab) Questions: 1) How can the column labels f and beta be removed? I tried to use Heading() to remove the labels, but some of them (actually all except identity) still appear. 2) The above example leads to a tabular with columns aligned like this: rrrcc. However, if put in a .tex document, the table entries (1:9) are filled with \phantom{0}, so that the numbers do not appear centered (rather right-aligned). In contrast, the row labels (given in the first three columns) appear centered although they should be right-aligned. 3) Additionally using justification=r in table_options() provides right-justification of the entries, however, they are now all wrapped in \multicolumn{1}{c}{...}. Why? Cheers, Marius Here is a .tex wrapper for convenience: \documentclass{article} \usepackage[T1]{fontenc} \usepackage{lmodern} \usepackage[american]{babel} \usepackage{tabularx} \usepackage{booktabs} \begin{document} % put code here \end{document} __ 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] columns called X rename Y
Simply colnames(seba)[1] - c colnames(seba)[2] - b Regards, Jose From: r-help-boun...@r-project.org [r-help-boun...@r-project.org] On Behalf Of Sebastian Kruk [residuo.so...@gmail.com] Sent: 18 January 2013 18:14 To: R-help Subject: [R] columns called X rename Y I have a data. frame to which you want to change the names to some of their columns. For example: seba - data.frame ('constant' = 3, 'numbers' = 1: 10, 'letters' = LETTERS [1:10], otros = 2:11) List their names: names (Seba) [1] constant numbers letters I want to rename c the column called constant and b the column called numbers. I do not know in which order appear names. How could do it? Thanks in advance, Sebastian. [[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. Wrap Up and Run 10k is back! Also, new for 2013 – 2km intergenerational walks at selected venues. So recruit a buddy, dust off the trainers and beat the winter blues by signing up now: http://www.ageuk.org.uk/10k Milton Keynes | Oxford | Sheffield | Crystal Palace | Exeter | Harewood House, Leeds | Tatton Park, Cheshire | Southampton | Coventry Age UK Improving later life http://www.ageuk.org.uk --- Age UK is a registered charity and company limited by guarantee, (registered charity number 1128267, registered company number 6825798). Registered office: Tavis House, 1-6 Tavistock Square, London WC1H 9NA. For the purposes of promoting Age UK Insurance, Age UK is an Appointed Representative of Age UK Enterprises Limited, Age UK is an Introducer Appointed Representative of JLT Benefit Solutions Limited and Simplyhealth Access for the purposes of introducing potential annuity and health cash plans customers respectively. Age UK Enterprises Limited, JLT Benefit Solutions Limited and Simplyhealth Access are all authorised and regulated by the Financial Services Authority. -- This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you receive a message in error, please advise the sender and delete immediately. Except where this email is sent in the usual course of our business, any opinions expressed in this email are those of the author and do not necessarily reflect the opinions of Age UK or its subsidiaries and associated companies. Age UK monitors all e-mail transmissions passing through its network and may block or modify mails which are deemed to be unsuitable. Age Concern England (charity number 261794) and Help the Aged (charity number 272786) and their trading and other associated companies merged on 1st April 2009. Together they have formed the Age UK Group, dedicated to improving the lives of people in later life. The three national Age Concerns in Scotland, Northern Ireland and Wales have also merged with Help the Aged in these nations to form three registered charities: Age Scotland, Age NI, Age Cymru. __ 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 tab delimited file from a certain line
On Jan 18, 2013, at 10:26 AM, Henrik Bengtsson wrote: Christof, I've added support for this to the R.filesets package. In your case, then all you need to do is: library(R.filesets) dlf - readDataFrame(filename, skip=^year) No need to specify any other arguments - they're all automagically inferred - and the default is stringsAsFactors=FALSE. This is in R.filesets v2.0.0 which I still haven't published on CRAN. In the meanwhile, you can install it via: source(http://aroma-project.org/hbLite.R;) hbLite(R.filesets) Mac GUI users should exit to a Terminal session running R --vanilla before attempting this. (Probably a safer practice in general, but sometimes we get get lazy because the GUI Package Installer is so handy.) -- David. /Henrik On Thu, Jan 17, 2013 at 1:34 AM, Christof Kluß ckl...@email.uni-kiel.de wrote: Hello thank you for the fast and helpful answer! Now the following works fine for me x - readLines(filename) i - grep(^year, x) dlf - read.table(textConnection(x[i:length(x)]), header = T, stringsAsFactors=F,sep=\t) Greetings Christof Am 16-01-2013 16:55, schrieb Rui Barradas: Hello, Read the file using readLines, then grep ^year. You can then use a textConnection to read.table: x - readLines(con = textConnection( informations (unknown count of lines) ... and at some point the table -- year month mday value 2013 1 16 0 )) # This is it i - grep(^year, x) read.table(textConnection(x[i:length(x)]), header = TRUE) Hope this helps, Rui Barradas Em 16-01-2013 14:17, Christof Kluß escreveu: Hi I would like to read table data from a text-files with extra informations in the header (of unknown line count). Example: informations (unknown count of lines) ... and at some point the table -- year month mday value 2013 1 16 0 ... If it was an excel file I could use something like read.xls(..., pattern=year) But it is a simple tab seperated text-file. Is there an easy way to read only the table? (Without dirty things ;)) Thx Christof __ 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-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-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. David Winsemius Alameda, CA, USA __ 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] select rows with identical columns from a data frame
I have a data frame with several columns. I want to select the rows with no NAs (as with complete.cases) and all columns identical. E.g., for --8---cut here---start-8--- f - data.frame(a=c(1,NA,NA,4),b=c(1,NA,3,40),c=c(1,NA,5,40)) f a b c 1 1 1 1 2 NA NA NA 3 NA 3 5 4 4 40 40 --8---cut here---end---8--- I want the vector TRUE,FALSE,FALSE,FALSE selecting just the first row because there all 3 columns are the same and none is NA. thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://memri.org http://mideasttruth.com http://honestreporting.com http://pmw.org.il http://iris.org.il All extremists should be taken out and shot. __ 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.csv returns no lines available for input
Hello, I'm trying to read a file rows at a time, so as to not read the entire file into memory. When reading the connections and readLines help, and R help archive, it seems this should be possible with read.csv and a file connection, making use of the nrows argument. From certain posts, it seemed that read.csv should return character(0) when the end of file is reached, and there are no more rows to read. Instead, I get an error there are no lines available for input. What is the proper way to check the end-of-file condition with read.csv, such that I could break a while loop reading the data in? #example, make a test file con - file(test.csv,wt) cat(a,b,c\n, 1,2,3\n, 4,5,6\n, 7,6,5\n, 4,3,2\n, 3,2,1\n,file=con) unlink(con) #show the file is valid con - file(test.csv,rt) read.csv(con,header=T) unlink(con) #show that readLines ends with character(0), like expected con - file(test.csv,rt) readLines(con,n=10) readLines(con,n=10) unlink(con) #show that read.csv end with error con - file(test.csv,rt) read.csv(con,header=T,nrows=10) read.csv(con,header=F,nrows=10) unlink(con) Sincerely, Stephen sessionInfo() R version 2.15.0 (2012-03-30) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base loaded via a namespace (and not attached): [1] tools_2.15.0 [[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] select rows with identical columns from a data frame
I can do Reduce(==,f[complete.cases(f),]) but that creates an intermediate data frame which I would love to avoid (to save memory). * Sam Steingold f...@tah.bet [2013-01-18 15:53:21 -0500]: I have a data frame with several columns. I want to select the rows with no NAs (as with complete.cases) and all columns identical. E.g., for f - data.frame(a=c(1,NA,NA,4),b=c(1,NA,3,40),c=c(1,NA,5,40)) f a b c 1 1 1 1 2 NA NA NA 3 NA 3 5 4 4 40 40 I want the vector TRUE,FALSE,FALSE,FALSE selecting just the first row because there all 3 columns are the same and none is NA. thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://truepeace.org http://iris.org.il http://www.PetitionOnline.com/tap12009/ http://ffii.org http://jihadwatch.org War doesn't determine who's right, just who's left. __ 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] Working with regular expression
Hello, This one seems to work. gsub([[:alpha:]_]*(.*), \\1, Text) Hope this helps, Rui Barradas Em 18-01-2013 20:11, Christofer Bogaso escreveu: Hello again, I was trying to extract the date element from a character vector using Regular expression. Below is a sample what I was trying to do: Text - c(asdf May 09 2009, dsaf_01-01-2001) gsub([a-zA-Z_], , Text) [1] 09 2009 01-01-2001 I basically have a long text and the task is to extract the date element. My above strategy failed because I can not extract the month for the first element. Can somebody help me out? Thanks and regards, __ 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-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] select rows with identical columns from a data frame
Hello, Try the following. complete.cases(f) apply(f, 1, function(x) all(x == x[1])) Hope this helps, Rui Barradas Em 18-01-2013 20:53, Sam Steingold escreveu: I have a data frame with several columns. I want to select the rows with no NAs (as with complete.cases) and all columns identical. E.g., for --8---cut here---start-8--- f - data.frame(a=c(1,NA,NA,4),b=c(1,NA,3,40),c=c(1,NA,5,40)) f a b c 1 1 1 1 2 NA NA NA 3 NA 3 5 4 4 40 40 --8---cut here---end---8--- I want the vector TRUE,FALSE,FALSE,FALSE selecting just the first row because there all 3 columns are the same and none is NA. thanks! __ 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] Working with regular expression
Thanks Rui for your help. Could you also explain the underlying logic please? Thanks and regards, On Sat, Jan 19, 2013 at 2:43 AM, Rui Barradas ruipbarra...@sapo.pt wrote: gsub([[:alpha:]_]*(.*), \\1, Text) __ 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] reading multiple key=value pairs per line
You could use the strapply function from the gsubfn package to extract the data from strings. This will return a list that you could use with do.call(rbind( The stringr package may have something similar or an alternative (but I am less familiar with that package). On Thu, Jan 17, 2013 at 9:21 PM, Frank Singleton b17fly...@gmail.comwrote: Hi, Thanks for a great environmentfor statistical computing :-) I have some input data in a file (input_kvpairs.csv) of the form key1=23, key2=67, key3=hello there key1=7, key2=22, key3=how are you key1=2, key2=77, key3=nice day, thanks Now in my head I wish it was of the form (input.csv) #key1, key2, key3 23,67, hello there 7, 22, how are you 2, 77, nice day, thanks so I could do data - read.csv(input.csv, header=TRUE) where the header column names are derived from the key names dynamically, and I could access the data using normal data$key1 or data$key2 mechanism. I guess I could just pre process the file first using python etc to create a CSV file with column header derived from key names, and values derived from key values, but I am interested to see how experienced R folks would handle this inside R. Thanks, Frank __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Gregory (Greg) L. Snow Ph.D. 538...@gmail.com [[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] select rows with identical columns from a data frame
On Jan 18, 2013, at 1:02 PM, Rui Barradas wrote: Hello, Try the following. complete.cases(f) apply(f, 1, function(x) all(x == x[1])) Hope this helps, Rui Barradas Em 18-01-2013 20:53, Sam Steingold escreveu: I have a data frame with several columns. I want to select the rows with no NAs (as with complete.cases) and all columns identical. E.g., for --8---cut here---start-8--- f - data.frame(a=c(1,NA,NA,4),b=c(1,NA,3,40),c=c(1,NA,5,40)) f a b c 1 1 1 1 2 NA NA NA 3 NA 3 5 4 4 40 40 --8---cut here---end---8--- f[ which( rowSums(f==f[[1]]) == length(f) ), ] a b c 1 1 1 1 I want the vector TRUE,FALSE,FALSE,FALSE selecting just the first row because there all 3 columns are the same and none is NA. thanks! David Winsemius Alameda, CA, USA __ 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] select rows with identical columns from a data frame
Here are two related approaches to your problem. The first uses a logical vector, keep, to say which rows to keep. The second uses an integer vector, it can be considerably faster when the columns are not well correlated with one another (so the number of desired rows is small proportion of the input rows). f1 - function (x) { # sieve with logical 'keep' vector stopifnot(is.data.frame(x), ncol(x) 1) keep - x[[1]] == x[[2]] for (i in seq_len(ncol(x))[-(1:2)]) { keep - keep x[[i - 1]] == x[[i]] } !is.na(keep) keep } f2 - function (x) { # sieve with integer 'keep' vector stopifnot(is.data.frame(x), ncol(x) 1) keep - which(x[[1]] == x[[2]]) for (i in seq_len(ncol(x))[-(1:2)]) { keep - keep[which(x[[i - 1]][keep] == x[[i]][keep])] } seq_len(nrow(x)) %in% keep } E.g., for a 10 million by 10 data.frame I get: x - data.frame(lapply(structure(1:10,names=letters[1:10]), function(i)sample(c(NA,1,1,1,2,2,2,3), replace=TRUE, size=1e7))) system.time(v1 - f1(x)) user system elapsed 4.040.164.19 system.time(v2 - f2(x)) user system elapsed 0.800.000.79 identical(v1, v2) [1] TRUE head(x[v1,]) a b c d e f g h i j 4811 2 2 2 2 2 2 2 2 2 2 41706 1 1 1 1 1 1 1 1 1 1 56633 1 1 1 1 1 1 1 1 1 1 70859 1 1 1 1 1 1 1 1 1 1 83848 1 1 1 1 1 1 1 1 1 1 84767 1 1 1 1 1 1 1 1 1 1 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Sam Steingold Sent: Friday, January 18, 2013 12:53 PM To: r-help@r-project.org Subject: [R] select rows with identical columns from a data frame I have a data frame with several columns. I want to select the rows with no NAs (as with complete.cases) and all columns identical. E.g., for --8---cut here---start-8--- f - data.frame(a=c(1,NA,NA,4),b=c(1,NA,3,40),c=c(1,NA,5,40)) f a b c 1 1 1 1 2 NA NA NA 3 NA 3 5 4 4 40 40 --8---cut here---end---8--- I want the vector TRUE,FALSE,FALSE,FALSE selecting just the first row because there all 3 columns are the same and none is NA. thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://memri.org http://mideasttruth.com http://honestreporting.com http://pmw.org.il http://iris.org.il All extremists should be taken out and shot. __ 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-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] A smart way to use $ in data frame
The important thing to understand is that $ is a shortcut for [[ and you are moving into the realm where a shortcut is the longest distance between 2 points (see fortune(312)). So your code can be something like: state - 'oldstate' balance - 'oldbalance' dataa[[balance]][ dataa[[state]]=='AR' ] You may also benefit from learning to use tools like with and subset (though subset has its own complications when used inside of other functions) or grep and match to find the columns of interest. On Fri, Jan 18, 2013 at 12:40 PM, Yuan, Rebecca rebecca.y...@bankofamerica.com wrote: Hello all, I have a data frame dataa: newdate newstate newid newbalance newaccounts 1 31DEC2001AR 1 1170 61 2 31DEC2001VA 2 4565 54 3 31DEC2001WA 3 2726 35 4 31DEC2001AR 3 2700 35 The following gives me the balance of state AR: dataa$newbalance[data$newstate == 'AR'] 1170 2700 Now, I have another different data frame datab, it is very similar to data, except that the name of the columns are different, and the order of the columns are different: oldstate olddate oldbalance oldid oldaccounts 1 AR 31DEC20121234 7 40 2 WA 31DEC2012 3 30 3 VA 31DEC20122345 5 23 3 AR 31DEC20125673 5 23 datab$oldbalance[datab$oldstate== 'AR' ] 1234 5673 Could I have a way to quote data$balance[data$state == 'AR'] in general, where balance=oldbalance, state=oldstate when data=dataa, and balance = newbalance, state = newstate when data=datab ? Thanks very much! Cheers, Rebecca -- This message, and any attachments, is for the intended...{{dropped:19}} __ 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] select rows with identical columns from a data frame
apply(f,1,function(x) all(duplicated(x)|duplicated(x,fromLast=TRUE)!is.na(x))) #[1] TRUE FALSE FALSE FALSE A.K. - Original Message - From: Sam Steingold s...@gnu.org To: r-help@r-project.org Cc: Sent: Friday, January 18, 2013 3:53 PM Subject: [R] select rows with identical columns from a data frame I have a data frame with several columns. I want to select the rows with no NAs (as with complete.cases) and all columns identical. E.g., for --8---cut here---start-8--- f - data.frame(a=c(1,NA,NA,4),b=c(1,NA,3,40),c=c(1,NA,5,40)) f a b c 1 1 1 1 2 NA NA NA 3 NA 3 5 4 4 40 40 --8---cut here---end---8--- I want the vector TRUE,FALSE,FALSE,FALSE selecting just the first row because there all 3 columns are the same and none is NA. thanks! -- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://memri.org http://mideasttruth.com http://honestreporting.com http://pmw.org.il http://iris.org.il All extremists should be taken out and shot. __ 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-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] Error in mer_finalize(ans) : Downdated X'X is not positive definite, 8.
Dear All, I have conducted an experiment in order to examine predation pressure in the surroundings of potential wildlife road-crossing structures. I have documented predation occurrence (binary…) in these structures and calculated several possible explanatory variables describing the spatial heterogeneity in several scales. At the landscape scale I have calculated the percentage of different land-uses (7) in buffers around the structures with changing radii (100m, 250m, 500m, 1000m and 2000m). For each radii I have generated a set of all possible models from the given 7 variables related to this radii. I have tried to account for random effects. A spatial random effect – the structure itself as a repeated measurement, by adding the term (1|Road_Structure). A temporal random effect – the observation session (is it correct to say that this examines possible learning in time ?), by adding the term (1|Session.ord). I have generated 4 sets for each radii: 1) with both random effects, 2+3) only one (each) random effect, and 4) none. I have tried to model the relationships using glm, glmer and lmer. The problem is that in the full model (all 7 variables) of some sets with random effects I get the following error: *Error in mer_finalize(ans) : Downdated X'X is not positive definite, 8.* The only record of this error I have found in the internet is for data with missing observation. This doesn't comply with my data set. I think it might be a singularity problem but I don't know how to check it, and as a result, I obviously don't know how to fix it. I have attached example R scripts (for the radius 2000) and the data table. Any help is highly appreciated ! Thanks and all the best, -- Yoav Avneon Department of Life Sciences The Spatial Ecology Lab Ben Gurion University of the Negev P.O.B. 653 Beer-Sheva 84105 ISRAEL +972-52-3391918 (mobile) +972-8-6461350 (lab) --- Yoav Yeled Teva ANP_multi_landscape_No_RE_Buffer_2000m.R http://r.789695.n4.nabble.com/file/n4656015/ANP_multi_landscape_No_RE_Buffer_2000m.R ANP_multi_landscape_Plus_RE_Str_Session_Buffer_2000m.R http://r.789695.n4.nabble.com/file/n4656015/ANP_multi_landscape_Plus_RE_Str_Session_Buffer_2000m.R ANP_multi_landscape_Plus_RE_Str_Buffer_2000m.R http://r.789695.n4.nabble.com/file/n4656015/ANP_multi_landscape_Plus_RE_Str_Buffer_2000m.R ANP_multi_landscape_Plus_RE_Session_Buffer_2000m.R http://r.789695.n4.nabble.com/file/n4656015/ANP_multi_landscape_Plus_RE_Session_Buffer_2000m.R Raw_data_4_logit_reg_Only_str_point_all_Landscape_vars.txt http://r.789695.n4.nabble.com/file/n4656015/Raw_data_4_logit_reg_Only_str_point_all_Landscape_vars.txt -- View this message in context: http://r.789695.n4.nabble.com/Error-in-mer-finalize-ans-Downdated-X-X-is-not-positive-definite-8-tp4656015.html Sent from the R help mailing list archive at Nabble.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.
Re: [R] Working with regular expression
Greetings! I hope you don't mind, Rui Barradas, but I'd like to explain the regex. Parsing it was a fun exercise! Here's the regex broken into two parts.. [[:alpha:]_]* = match zero or more alphabet or underscore characters (.*)= match zero or more characters and add them to \1 pattern buffer Going character by character through the date string asdf May 09 2009 asdf matches the first part May 09 2009 matches the second part and is stored in the \1 pattern buffer The gsub command - gsub([[:alpha:]_]*(.*), \\1, Text) - replaces the entire string (because this regex matches the entire string.. they all begin with a sequence of alphabet and/or underscore characters and the .* pattern at the end matches the rest of the line) with the contents of the \1 pattern buffer and stores it in the variable Text. If the length of the string prepended to the date is consistent another possible solution would be - gsub(.{5}(.*), \\1, Text) - which would strip off the first five characters (.{5} matches five any characters). --David Alston Without rules there is no game for it is by the rules the game is defined. --SOv On Fri, Jan 18, 2013 at 3:05 PM, Christofer Bogaso bogaso.christo...@gmail.com wrote: Thanks Rui for your help. Could you also explain the underlying logic please? Thanks and regards, On Sat, Jan 19, 2013 at 2:43 AM, Rui Barradas ruipbarra...@sapo.pt wrote: gsub([[:alpha:]_]*(.*), \\1, Text) __ 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-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] Working with regular expression
Hi, Not sure what format you wanted the dates: gsub(^\\w+ ,,gsub([_], ,Text)) #[1] May 09 2009 01-01-2001 #Another way is: gsub(^\\w+ |\\w+_,,Text) #[1] May 09 2009 01-01-2001 res- gsub(^\\w+ |\\w+_,,Text) res1-c(as.Date(res[grep( ,res)],format=%b %d %Y), as.Date(res[-grep( ,res)],format=%m-%d-%Y)) res1 #[1] 2009-05-09 2001-01-01 A.K. - Original Message - From: Christofer Bogaso bogaso.christo...@gmail.com To: r-help r-help@r-project.org Cc: Sent: Friday, January 18, 2013 3:11 PM Subject: [R] Working with regular expression Hello again, I was trying to extract the date element from a character vector using Regular expression. Below is a sample what I was trying to do: Text - c(asdf May 09 2009, dsaf_01-01-2001) gsub([a-zA-Z_], , Text) [1] 09 2009 01-01-2001 I basically have a long text and the task is to extract the date element. My above strategy failed because I can not extract the month for the first element. Can somebody help me out? Thanks and regards, __ 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-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] columns called X rename Y
If you have a lot of names to change, or you need to do it regularly, you can use something like: renames - read.table(text= oldname newname constant c numbers b , header=TRUE, as.is=TRUE ) names(seba)[match(renames$oldname, names(seba))] - renames$newnames --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try the following. names(seba)[grep(numbers, names(seba))] - b names(seba)[grep(constant, names(seba))] - c names(seba) Hope this helps, Rui Barradas Em 18-01-2013 18:14, Sebastian Kruk escreveu: I have a data. frame to which you want to change the names to some of their columns. For example: seba - data.frame ('constant' = 3, 'numbers' = 1: 10, 'letters' = LETTERS [1:10], otros = 2:11) List their names: names (Seba) [1] constant numbers letters I want to rename c the column called constant and b the column called numbers. I do not know in which order appear names. How could do it? Thanks in advance, Sebastian. [[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-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-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] Object created within a function disappears after the function is run
Dear R-helpers, I have run the code below which I expected to make an object called dd1, but that object does not exist. So, in summary, my problem is that my function is meant to make an object (dd1), and it does indeed make that object (I know that the last line of the function prints it out) but then, after the function has run, the object has disappeared. It's late on a Friday so I may be overlooking something obvious, but I'd appreciate your help if you can see what I'm doing wrong. Many thanks, Mark Na rm(list=ls()) #LOAD package, data, and keep clean copies of data library(reshape2); library(MuMIn);library(AICcmodavg) birdmatrix-read.csv(birdmatrix.csv,stringsAsFactors=FALSE); birdmatrixclean-birdmatrix habitatmatrix-read.csv(habitatmatrix.csv,stringsAsFactors=FALSE); habitatmatrixclean-habitatmatrix traits-read.csv(traits.csv,stringsAsFactors=FALSE); traitsclean-traits birdhabitat-merge(birdmatrix,habitatmatrix,by=SARBASINYEAR) #merge bird and habitat data data-birdhabitat; data$SAR-substr(data$SARBASINYEAR,1,3) #convenience; create SAR variable #make a FUNCTION to combine variables into models dredgeit-function(lm1){ dd1-dredge(lm1,eval=FALSE) #create all possible combinations dd1-gsub(formula = ,,dd1,fixed=TRUE) #delete characters dd1-gsub( + 1,,dd1,fixed=TRUE) #delete characters dd1#inspect model formulae } lm1-lm(data$BiRich.o~log(data$HaArea,10)+log(data$HaPeri,10)) #saturated submodel dredgeit(lm1) [[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] Object created within a function disappears after the function is run
Hi Sarah, that works perfectly, thank you! Mark Na On Fri, Jan 18, 2013 at 5:23 PM, Sarah Goslee sarah.gos...@gmail.comwrote: Hi, You need to assign the result of the function to an object: dd1 - dredgeit(lm1) On Fri, Jan 18, 2013 at 6:17 PM, mtb...@gmail.com wrote: Dear R-helpers, I have run the code below which I expected to make an object called dd1, but that object does not exist. So, in summary, my problem is that my function is meant to make an object (dd1), and it does indeed make that object (I know that the last line of the function prints it out) but then, after the function has run, the object has disappeared. It's late on a Friday so I may be overlooking something obvious, but I'd appreciate your help if you can see what I'm doing wrong. Many thanks, Mark Na rm(list=ls()) #LOAD package, data, and keep clean copies of data library(reshape2); library(MuMIn);library(AICcmodavg) birdmatrix-read.csv(birdmatrix.csv,stringsAsFactors=FALSE); birdmatrixclean-birdmatrix habitatmatrix-read.csv(habitatmatrix.csv,stringsAsFactors=FALSE); habitatmatrixclean-habitatmatrix traits-read.csv(traits.csv,stringsAsFactors=FALSE); traitsclean-traits birdhabitat-merge(birdmatrix,habitatmatrix,by=SARBASINYEAR) #merge bird and habitat data data-birdhabitat; data$SAR-substr(data$SARBASINYEAR,1,3) #convenience; create SAR variable #make a FUNCTION to combine variables into models dredgeit-function(lm1){ dd1-dredge(lm1,eval=FALSE) #create all possible combinations dd1-gsub(formula = ,,dd1,fixed=TRUE) #delete characters dd1-gsub( + 1,,dd1,fixed=TRUE) #delete characters dd1#inspect model formulae } lm1-lm(data$BiRich.o~log(data$HaArea,10)+log(data$HaPeri,10)) #saturated submodel dredgeit(lm1) -- Sarah Goslee http://www.functionaldiversity.org [[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] Working with regular expression
Hello, Right, thanks for the explanation, it saved me time. Rui Barradas Em 18-01-2013 22:50, David Alston escreveu: Greetings! I hope you don't mind, Rui Barradas, but I'd like to explain the regex. Parsing it was a fun exercise! Here's the regex broken into two parts.. [[:alpha:]_]* = match zero or more alphabet or underscore characters (.*)= match zero or more characters and add them to \1 pattern buffer Going character by character through the date string asdf May 09 2009 asdf matches the first part May 09 2009 matches the second part and is stored in the \1 pattern buffer The gsub command - gsub([[:alpha:]_]*(.*), \\1, Text) - replaces the entire string (because this regex matches the entire string.. they all begin with a sequence of alphabet and/or underscore characters and the .* pattern at the end matches the rest of the line) with the contents of the \1 pattern buffer and stores it in the variable Text. If the length of the string prepended to the date is consistent another possible solution would be - gsub(.{5}(.*), \\1, Text) - which would strip off the first five characters (.{5} matches five any characters). --David Alston Without rules there is no game for it is by the rules the game is defined. --SOv On Fri, Jan 18, 2013 at 3:05 PM, Christofer Bogaso bogaso.christo...@gmail.com wrote: Thanks Rui for your help. Could you also explain the underlying logic please? Thanks and regards, On Sat, Jan 19, 2013 at 2:43 AM, Rui Barradas ruipbarra...@sapo.pt wrote: gsub([[:alpha:]_]*(.*), \\1, Text) __ 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-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] columns called X rename Y
On Jan 18, 2013, at 3:13 PM, Jeff Newmiller wrote: If you have a lot of names to change, or you need to do it regularly, you can use something like: renames - read.table(text= oldname newname constant c numbers b , header=TRUE, as.is=TRUE ) names(seba)[match(renames$oldname, names(seba))] - renames$newnames Thnks Jeff; I like it, plan to add it to my import scripts, ... although the 'newname' column is misspelled. If you have a set of target variable and a diverse set of input variables that need to be mapped to a constant set of names and a re thinking of incorporating in a function, it might be better with: names(newdat)[ match(renames[['oldname']], names(newdat))] - renames[['newsource3']] save(renames, file=name_map_data.rda) --- Jeff NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Rui Barradas ruipbarra...@sapo.pt wrote: Hello, Try the following. names(seba)[grep(numbers, names(seba))] - b names(seba)[grep(constant, names(seba))] - c names(seba) I would think that it might be dangerous if there were multiple column names that contained either numbers or data. Hope this helps, Rui Barradas Em 18-01-2013 18:14, Sebastian Kruk escreveu: I have a data. frame to which you want to change the names to some of their columns. For example: seba - data.frame ('constant' = 3, 'numbers' = 1: 10, 'letters' = LETTERS [1:10], otros = 2:11) List their names: names (Seba) [1] constant numbers letters I want to rename c the column called constant and b the column called numbers. I do not know in which order appear names. How could do it? Thanks in advance, Sebastian. [[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-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-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. David Winsemius Alameda, CA, USA __ 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] Tinn-R and R problem
Dear r-users, Actually, this is not a big problem, however it is a bit annoying. Everytime I want to use Tinn-R and R. I have to do this step first before I can excute a set of R codes: R-- configure -- temporarily (current session) if not it will give this message: .trPaths - paste(paste(Sys.getenv('APPDATA'), '\\Tinn-R\\tmp\\', sep=''), c('', 'search.txt', 'objects.txt', 'file.r', 'selection.r', 'block.r', 'lines.r'), sep='' source('C:/Users/Owner/AppData/Roaming/Tinn-R/custom/Rconfigure_default.r') Hope somebody can explain to me and help me to solve this problem? R version 2.12.1 (2010-12-16) Thank you so much for your help. [[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] an apply question
Hello, It should be easu but I cannot figure out how to use apply function. I am trying to replace negative values in an array with these values + 24. Would appreciate help. Thanks, Mark shours - apply(fhours, function(x){if (x 0) x - x+24}) Error in match.fun(FUN) : argument FUN is missing, with no default [[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] an apply question
Hi m p, You can use either apply(fhours, 2, function(x) ifelse(x 0, x + 24, x) or shours - fhours shours[shours 0 ] - shours[shours 0 ] + 24 shours HTH, Jorge.- On Sat, Jan 19, 2013 at 4:17 PM, m p wrote: Hello, It should be easu but I cannot figure out how to use apply function. I am trying to replace negative values in an array with these values + 24. Would appreciate help. Thanks, Mark shours - apply(fhours, function(x){if (x 0) x - x+24}) Error in match.fun(FUN) : argument FUN is missing, with no default [[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. [[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] reading multiple key=value pairs per line
Thanks Arun,Petr,Greg and Christophe, I have some possibilities to consider now :-) Cheers / Frank On 01/18/2013 08:37 AM, arun wrote: Hi, Sorry, there was a mistake. I didn't notice comma in key3 .Lines1-readLines(textConnection('key1=23, key2=67, key3=hello there key1=7, key2=22, key3=how are you key1=2, key2=77, key3=nice day, thanks')) res1-read.table(text=gsub(key{0,1}\\d,,gsub([\],,Lines1)),sep==,header=FALSE,stringsAsFactors=F)[-1] res1[,1:2]-sapply(res1[,1:2],function(x) as.numeric(gsub(\\, $,,x))) names(res1)- paste(substr(Lines1,1,3),1:3,sep=) res1 # key1 key2 key3 #1 23 67 hello there #27 22 how are you #32 77 nice day, thanks str(res1) #'data.frame':3 obs. of 3 variables: # $ key1: num 23 7 2 # $ key2: num 67 22 77 # $ key3: chr hello there how are you nice day, thanks A.K. - Original Message - From: Frank Singleton b17fly...@gmail.com To: r-help@r-project.org Cc: Sent: Thursday, January 17, 2013 11:21 PM Subject: [R] reading multiple key=value pairs per line Hi, Thanks for a great environmentfor statistical computing :-) I have some input data in a file (input_kvpairs.csv) of the form key1=23, key2=67, key3=hello there key1=7, key2=22, key3=how are you key1=2, key2=77, key3=nice day, thanks Now in my head I wish it was of the form (input.csv) #key1, key2, key3 23,67, hello there 7, 22, how are you 2, 77, nice day, thanks so I could do data - read.csv(input.csv, header=TRUE) where the header column names are derived from the key names dynamically, and I could access the data using normal data$key1 or data$key2 mechanism. I guess I could just pre process the file first using python etc to create a CSV file with column header derived from key names, and values derived from key values, but I am interested to see how experienced R folks would handle this inside R. Thanks, Frank __ 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-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] random effects model
Hi AK I got the gg plots , thanks. All of a sudden I just got it. I didn't even update packages. Not sure what worked. Just a basic confusion, to find out if in the association between HiBP and Obese/Overweight status, to quantify if obese males and obese females run a different risk of becoming hypertensive, will the gee output of BP.gee - gee(HiBP~Obese* Sex ,data=BPsub3,id=CODEA,family=binomial, corstr=exchangeable,na.action=na.omit) suffice? Cheers. On Thu, Jan 17, 2013 at 2:01 PM, arun kirshna [via R] ml-node+s789695n4655802...@n4.nabble.com wrote: HI, In the same link at the bottom of the page, All is well now after updating all packages with the following: update.packages() It may or may not solve your problem. I got your attachments. You should post those questions in ([hidden email] http://user/SendEmail.jtp?type=nodenode=4655802i=0). I suggest you to read lme4 book (http://lme4.r-forge.r-project.org/lMMwR/) #lrgprt.pdf A.K. - Original Message - From: rex2013 [hidden email]http://user/SendEmail.jtp?type=nodenode=4655802i=1 To: [hidden email] http://user/SendEmail.jtp?type=nodenode=4655802i=2 Cc: Sent: Wednesday, January 16, 2013 5:06 AM Subject: Re: [R] random effects model Hi I tried removing the missing values and installing plyr. Still error message appears with ggplot2 Btw, did you get the attachments with my earlier mail? Ta. On Wed, Jan 16, 2013 at 3:16 AM, arun kirshna [via R] [hidden email] http://user/SendEmail.jtp?type=nodenode=4655802i=3 wrote: Hi, Check these links: http://comments.gmane.org/gmane.comp.lang.r.ggplot2/6527 https://groups.google.com/forum/#!msg/ggplot2/nfVjxL0DXnY/5zf50zCeZuMJ A.K. From: Usha Gurunathan [hidden email] http://user/SendEmail.jtp?type=nodenode=4655612i=0 To: arun [hidden email] http://user/SendEmail.jtp?type=nodenode=4655612i=1 Cc: R help [hidden email] http://user/SendEmail.jtp?type=nodenode=4655612i=2 Sent: Tuesday, January 15, 2013 6:31 AM Subject: Re: [R] random effects model Hi AK Got an error message with library(ggplot2) ggplot(BP.stack1,aes(x=factor(HiBP),fill=Obese))+geom_bar(position=fill) Error in rename(x, .base_to_ggplot, warn_missing = FALSE) : could not find function revalue ggplot(BP.stack1,aes(x=factor(HiBP),fill=Overweight))+geom_bar(position=fill) Error in rename(x, .base_to_ggplot, warn_missing = FALSE) : could not find function revalue I got the dot plot, thanks for that. I have attached some plots, not sure how to interpret, they had unusual patterns.Is it because of missing data? I tried removing the missing data too. They still appeared the same. Do I need to transform the data? Thanks in advance. On Tue, Jan 15, 2013 at 8:54 AM, arun [hidden email] http://user/SendEmail.jtp?type=nodenode=4655612i=3 wrote: HI, BP_2b-read.csv(BP_2b.csv,sep=\t) BP_2bNM-na.omit(BP_2b) BP.stack3 - reshape(BP_2bNM,idvar=CODEA,timevar=time,sep=,varying=list(c(Obese14,Obese21),c(Overweight14,Overweight21),c(hibp14,hibp21)),v.names=c(Obese,Overweight,HiBP),times=factor(c(1,2)),direction=long) library(car) BP.stack3$Obese- recode(BP.stack3$Obese,1='Obese';0='Not Obese') BP.stack3$Overweight- recode(BP.stack3$Overweight,1='Overweight';0='Not Overweight') library(ggplot2) ggplot(BP.stack3,aes(x=factor(HiBP),fill=Obese))+geom_bar(position=fill) ggplot(BP.stack3,aes(x=factor(HiBP),fill=Overweight))+geom_bar(position=fill) You could try lmer() from lme4. library(lme4) fm1-lmer(HiBP~time+(1|CODEA), family=binomial,data=BP.stack3) #check codes, not sure print(dotplot(ranef(fm1,post=TRUE), scales = list(x = list(relation = free)))[[1]]) qmt1- qqmath(ranef(fm1, postVar=TRUE)) print(qmt1[[1]]) A.K. From: Usha Gurunathan [hidden email] http://user/SendEmail.jtp?type=nodenode=4655612i=4 To: arun [hidden email] http://user/SendEmail.jtp?type=nodenode=4655612i=5 Cc: R help [hidden email] http://user/SendEmail.jtp?type=nodenode=4655612i=6 Sent: Monday, January 14, 2013 6:32 AM Subject: Re: [R] random effects model Hi AK I have been trying to create some plots. All being categorical variables, I am not getting any luck with plots. The few ones that have worked are below: barchart(~table(HiBP)|Obese,data=BP.sub3) ## BP.sub3 is the stacked data without missing values barchart(~table(HiBP)|Overweight,data=BP.sub3) plot(jitter(hibp14,factor=2)~jitter(Obese14,factor=2),col=gray,cex=0.7, data=Copy.of.BP_2) ## Copy.of.BP_2 is the original wide format ## not producing any good plots with mixed models as well. summary(lme.3 - lme(HiBP~time, data=BP.sub3,random=~1|CODEA, na.action=na.omit)) anova(lme.3) head(ranef(lme.3)) print(plot(ranef(lme.3))) ## Thanks
Re: [R] an apply question
HI, Assuming a matrix: set.seed(15) mat1-matrix(sample(-10:10,40,replace=TRUE),ncol=5) apply(mat1,2,function(x) ifelse(x0,x+24, x)) # [,1] [,2] [,3] [,4] [,5] #[1,] 2 4 23 1 0 #[2,] 18 7 10 3 16 #[3,] 10 16 16 16 0 #[4,] 3 3 6 17 3 #[5,] 21 0 6 9 16 #[6,] 10 4 6 0 0 #[7,] 7 8 21 0 20 #[8,] 19 7 15 19 5 A.K. - Original Message - From: m p mzp3...@gmail.com To: r-h...@stat.math.ethz.ch Cc: Sent: Saturday, January 19, 2013 12:17 AM Subject: [R] an apply question Hello, It should be easu but I cannot figure out how to use apply function. I am trying to replace negative values in an array with these values + 24. Would appreciate help. Thanks, Mark shours - apply(fhours, function(x){if (x 0) x - x+24}) Error in match.fun(FUN) : argument FUN is missing, with no default [[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-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.