[R] Writing a .pdf file within a function - what do I need to return()?
I am trying to write a function that generates one PDf containing plots from several .csv files within a directory. When I manually execute the code it seems to work, but not when it is a function. I think I need to return() something, but haven't had much luck figuring out what/how. plot.isi-function(csv.path=~/project/csv by cell) { csv.files-grep('.csv', list.files(path = csv.path, full.names=T), value=T) pdf(file='plots/isi plots.pdf', width=10, height=8) #par(mfrow=c(2,1)) #ideally 2 plots per page, but will work on details after fx. works for (i in 1:length(csv.files)){ raw.df-read.csv(csv.files[i]) names(raw.df)-c('t','isi','logic','cond') xyplot(isi ~ t, raw.df, ylim=c(0,1500), ylab='isi', xlab='time', main=basename(csv.files[i])) } dev.off() } Thank you all for the help, Jason Deignan -- View this message in context: http://r.789695.n4.nabble.com/Writing-a-pdf-file-within-a-function-what-do-I-need-to-return-tp4470165p4470165.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] partial duplicates of dataframe rows, indexing and removal
Hello. I am trying to remove from my dataframe, those rows in which the first 7 columns are duplicated even if subsequent columns make those rows unique. df-data.frame(id=rep(c('amy','bob','joe') , each=5), pet1=sample(LETTERS[1:3],15, replace=T), pet2=sample(LETTERS[1:3],15, replace=T), pet3=sample(LETTERS[1:5],15, replace=T)) df id pet1 pet2 pet3 1 amyCBA 2 amyBAA 3 amyAAD 4 amyBCA 5 amyCBB 6 bobBAA 7 bobCAC 8 bobCCA 9 bobBCE 10 bobCBC 11 joeCBA 12 joeABE 13 joeCCB 14 joeCAD 15 joeACC I am trying to identify and remove the rows of df that are duplicates in df[,1:3]. culled.df-unique(x[,1:3]) culled.df id pet1 pet2 1 amyAA 2 amyCC 3 amyCA 5 amyAB 6 bobAB 7 bobCC 8 bobBC 10 bobBA 11 joeBB 13 joeBC 14 joeBA This is where I'm hung up. I've been trying match() or %in% to get the rows of df where df[,1:3] match df.culled df[df.culled %in% df[,1:3],] Is this a reasonable solution, or am I making it more difficult than it need to be? Thanks for your suggestions, Jason -- View this message in context: http://r.789695.n4.nabble.com/partial-duplicates-of-dataframe-rows-indexing-and-removal-tp4171322p4171322.html Sent from the R help mailing list archive at Nabble.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.
[R] list index rules evaluation behavior
I have what may be a simple/foolish question, but I've done the due diligence and looked through pages of posts here as well as several of the PDFs on the CRAN site, but haven't been able find what I'm after. I am working with a list of say 3 histogram objects A, B C, and each histogram is a list of 7 elements. I would like to access $name, the 6th element, of histograms A,B and C. Trial and error yielded some results that told me I clearly don't understand how R interprets index commands. For the histogram list above: a[1:2] give histograms A and B as expected. a[[1:2]] gives the second element of histogram 1, but a[[1:1]] gives all elements of histogram 1, while a[[1:3]] gives null?! If anyone could help with an explanation of indexing rules, or a source that does so, I would very much appreciate it. Oh and an answer to the first question! Thanks All Jason -- View this message in context: http://n4.nabble.com/list-index-rules-evaluation-behavior-tp1745398p1745398.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] Code is too slow: mean-centering variables in a data frame by subgroup
I posted a similar problem last week (but with an uninformative subject header) See if this http://n4.nabble.com/a-vectorized-solution-to-some-simple-dataframe-math-td1692810.html#a1710410 this helps. -- View this message in context: http://n4.nabble.com/Code-is-too-slow-mean-centering-variables-in-a-data-frame-by-subgroup-tp1745335p1745434.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] list index rules evaluation behavior
Sorry for not supplying some example code for the above example. Here's an example list 'a' with histogram elements A, B, and C which are also lists. a $A $breaks [1] -80 -70 -60 -50 -40 -30 -20 -10 0 10 20 30 40 50 60 $counts [1] 1 0 0 2 29 120 301 433 421 265 93 43 9 3 $intensities [1] 5.81e-05 0.00e+00 0.00e+00 1.162791e-04 1.686047e-03 6.976744e-03 1.75e-02 [8] 2.512e-02 2.447674e-02 1.540698e-02 5.406977e-03 2.50e-03 5.232558e-04 1.744186e-04 $density [1] 5.813-05 0.00e+00 0.00e+00 1.162791e-04 1.686047e-03 6.976744e-03 1.75e-02 [8] 2.517442e-02 2.447674e-02 1.540698e-02 5.406977e-03 2.50e-03 5.232558e-04 1.744186e-04 $mids [1] -75 -65 -55 -45 -35 -25 -15 -5 5 15 25 35 45 55 $xname [1] X[[1L]] $equidist [1] TRUE attr(,class) [1] histogram $B $breaks [1] -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 20 25 30 35 $counts [1] 2 1 3 20 86 225 396 408 404 294 182 125 38 11 1 ... $C ... Is there a way to index the $xname element from each of the elements of a? -- View this message in context: http://n4.nabble.com/list-index-rules-evaluation-behavior-tp1745398p1745579.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] a vectorized solution to some simple dataframe math?
Thanks a lot for the help guys. That's exactly what I was looking for. I'm trying to avoid loops, but still don't know what tasks simply require them. Thanks again! Cheers, Jasn -- View this message in context: http://n4.nabble.com/a-vectorized-solution-to-some-simple-dataframe-math-tp1692810p1710410.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] a vectorized solution to some simple dataframe math?
I have a data frame containing the results of time measurements taken from several cells. Each cell was measured in conditions A and B, and there are an arbitrary number of measurements in each condition. I am trying to calculate the difference of each measurement from the mean of a given cell in a given condition without relying on loops. my.df id condtime 1 cell1 A 343.5 2 cell1 A 355.2 ... 768 cell1 B 454.0 ... 2106cell2 A 433.9 ... as a first approach I tried: mews-aggregate(my.df$time, list(cond=data$id, id=data$cond), mean) id cond time cell1A 352 cell1B 446 cell2A 244 cell2B ... I then tried to use %in% to match id and cond of mews with my.df, but I haven't been able to get it to work. Am I on the right track? What are some other solutions? Thanks for any help. jason -- View this message in context: http://n4.nabble.com/a-vectorized-solution-to-some-simple-dataframe-math-tp1692810p1692810.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] lapply and list indexing basics (after realizing I wasn't previously subscribed...sorry)
I have split my original dataframe to generate a list of dataframes each of which has 3 columns of factors and a 4th column of numeric data. I would like to use lapply to apply the fitdistr() function to only the 4th column (x$isi) of the dataframes in the list. Is there a way to do this or am I misusing lapply? As a second solution I tried splitting only the numeric data column to yield a list of vectors and then using lapply(myList, fitdistr, densfun='gamma',start=list(scale=1, shape=2)) returns the error: Error in optim(x = c(305, 290, 283, 363, 331, 293, 304, 312, 286, 339, : non-finite finite-difference value [2] In addition: Warning message: In dgamma(x, shape, scale, log) : NaNs produced However, if I use fitdistr(myList[[i]]) on each element of the list, there are no errors. Thanks in advance for any comments. -jason -- View this message in context: http://n4.nabble.com/lapply-and-list-indexing-basics-after-realizing-I-wasn-t-previously-subscribed-sorry-tp1584053p1584053.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] lapply and list indexing basics
I have split my original dataframe to generate a list of dataframes each of which has 3 columns of factors and a 4th column of numeric data. I would like to use lapply to apply the fitdistr() function to only the 4th column (x$isi) of the dataframes in the list. Is there a way to do this or am I misusing lapply? As a second solution I tried splitting only the numeric data column to yield a list of vectors and then using lapply(myList, fitdistr, densfun='gamma',start=list(scale=1, shape=2)) returns the error: Error in optim(x = c(305, 290, 283, 363, 331, 293, 304, 312, 286, 339, : non-finite finite-difference value [2] In addition: Warning message: In dgamma(x, shape, scale, log) : NaNs produced However, if I use fitdistr(myList[[i]]) on each element of the list, there are no errors. Thanks in advance for any comments. -jason -- View this message in context: http://n4.nabble.com/lapply-and-list-indexing-basics-tp1584006p1584006.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] Extract vector elements until cumsum = x
Thanks a lot, everybody! I've been working with these data for a while, and didn't realize that some of my assumptions lead me to leave out some important info. Specifically, that the vector contains time values of intervals between adjacent events, and so are always positive. Thanks again for your help Dgnn -- View this message in context: http://n4.nabble.com/Extract-vector-elements-until-cumsum-x-tp998023p998420.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] Extract vector elements until cumsum = x
Hi All, I have a vector n, and for each n[i] I want to extract n[i], n[i+1], n[i+2]..., until the cumulative sum of n[i] and subsequent elements exceeds a CheckValue, whereupon I move to the next index and repeat. I am trying to find a Vectorized approach, and have seen similar posts where filter{stat} and rollmean{zoo} were suggested, but, I haven't been able to figure a way to use them for this problem. Best wishes to all, and thanks for the help! Dgnn -- View this message in context: http://n4.nabble.com/Extract-vector-elements-until-cumsum-x-tp998023p998023.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.