Re: [R] how to save this result in a vector
My guess is that what you want probably is best done by using ecdf. You might want to look it up in the docs. 1-ecdf(test)(x) Will give you the percentage of values in test larger than x. On 11/1/2010 2:24 AM, Changbin Du wrote: Thanks Joshua! Yes, i is not going up sequentially by 1, as i here is the raw number of reads for each DNA base. Thanks so much for the great help! On Sun, Oct 31, 2010 at 6:03 PM, Joshua Wiley jwiley.ps...@gmail.comwrote: On Sun, Oct 31, 2010 at 5:44 PM, Joshua Wiley jwiley.ps...@gmail.com wrote: snip #cover is a vector cover_per - function(cover) { ## create a vector to store the results of your for loop output - vector(numeric, length(min(cover):max(cover))) for (i in min(cover):max(cover)) { ## rather than print()ing the output, assign it to an object output[i] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) } ## have the return value from the function be ## the object 'output' return(output) } I did not catch that i was not necessarily starting at 1 going sequentially up, so that would have to be done manually (or use cumsum per David rather than the function you wrote). cover_per2 - function(cover) { output - vector(numeric, length(min(cover):max(cover))) j - 1 for (i in min(cover):max(cover)) { output[j] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) j - j + 1 } return(output) } Josh __ 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 save this result in a vector
Thanks, Erich! On Mon, Nov 1, 2010 at 4:55 AM, Erich Neuwirth erich.neuwi...@univie.ac.atwrote: My guess is that what you want probably is best done by using ecdf. You might want to look it up in the docs. 1-ecdf(test)(x) Will give you the percentage of values in test larger than x. On 11/1/2010 2:24 AM, Changbin Du wrote: Thanks Joshua! Yes, i is not going up sequentially by 1, as i here is the raw number of reads for each DNA base. Thanks so much for the great help! On Sun, Oct 31, 2010 at 6:03 PM, Joshua Wiley jwiley.ps...@gmail.com wrote: On Sun, Oct 31, 2010 at 5:44 PM, Joshua Wiley jwiley.ps...@gmail.com wrote: snip #cover is a vector cover_per - function(cover) { ## create a vector to store the results of your for loop output - vector(numeric, length(min(cover):max(cover))) for (i in min(cover):max(cover)) { ## rather than print()ing the output, assign it to an object output[i] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) } ## have the return value from the function be ## the object 'output' return(output) } I did not catch that i was not necessarily starting at 1 going sequentially up, so that would have to be done manually (or use cumsum per David rather than the function you wrote). cover_per2 - function(cover) { output - vector(numeric, length(min(cover):max(cover))) j - 1 for (i in min(cover):max(cover)) { output[j] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) j - j + 1 } return(output) } Josh -- Sincerely, Changbin -- Changbin Du DOE Joint Genome Institute Bldg 400 Rm 457 2800 Mitchell Dr Walnut Creet, CA 94598 Phone: 925-927-2856 [[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] how to save this result in a vector
HI, Dear R community, I have the following codes to calculate the commulative coverage. I want to save the output in a vector, How to do this? test-seq(10, 342, by=2) #cover is a vector cover_per-function (cover) { for (i in min(cover):max(cover)) {print(100*sum(ifelse(cover = i, 1, 0))/length(cover))} } result-cover_per(test) result NULL Can anyone help me this this? -- Sincerely, Changbin -- [[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] how to save this result in a vector
Hi Changbin, The yek is that you need to save the results of your for loop rather than just printing it. It also may be possible to vectorize your for loop which might simplify things and speed them up, but I did not look at that. Here is one way to save the results, see inline comments for more details. Cheers, Josh ## test-seq(10, 342, by=2) #cover is a vector cover_per - function(cover) { ## create a vector to store the results of your for loop output - vector(numeric, length(min(cover):max(cover))) for (i in min(cover):max(cover)) { ## rather than print()ing the output, assign it to an object output[i] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) } ## have the return value from the function be ## the object 'output' return(output) } ## here the results will be printed to the screen ## (the results are just the 'output' object) cover_per(test) ## if you assign it to another object, nothing is printed ## but you can easily print 'result' if you want result - cover_per(test) ## On Sun, Oct 31, 2010 at 5:35 PM, Changbin Du changb...@gmail.com wrote: HI, Dear R community, I have the following codes to calculate the commulative coverage. I want to save the output in a vector, How to do this? test-seq(10, 342, by=2) #cover is a vector cover_per-function (cover) { for (i in min(cover):max(cover)) {print(100*sum(ifelse(cover = i, 1, 0))/length(cover))} } result-cover_per(test) result NULL Can anyone help me this this? -- Sincerely, Changbin -- [[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. -- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.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 save this result in a vector
On Oct 31, 2010, at 8:35 PM, Changbin Du wrote: HI, Dear R community, I have the following codes to calculate the commulative coverage. Not sure exactly what you mean by this. My guess is implemented below. I want to save the output in a vector, How to do this? test-seq(10, 342, by=2) #cover is a vector cover_per-function (cover) { for (i in min(cover):max(cover)) Using either for (i in cover) { ...} or for (i in seq_along(cover) ) {...} would be more typical. {print(100*sum(ifelse(cover = i, 1, 0))/length(cover))} } result-cover_per(test) Are you looking for cumsum? test-seq(10, 34, by=2) 100*cumsum(test)/sum(test) [1] 3.496503 7.692308 12.587413 18.181818 24.475524 31.468531 39.160839 [8] 47.552448 56.643357 66.433566 76.923077 88.111888 100.00 print(100*cumsum(test)/sum(test), digits=2) [1] 3.5 7.7 12.6 18.2 24.5 31.5 39.2 47.6 56.6 66.4 76.9 88.1 100.0 -- David Winsemius, MD West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to save this result in a vector
On Sun, Oct 31, 2010 at 5:44 PM, Joshua Wiley jwiley.ps...@gmail.com wrote: snip #cover is a vector cover_per - function(cover) { ## create a vector to store the results of your for loop output - vector(numeric, length(min(cover):max(cover))) for (i in min(cover):max(cover)) { ## rather than print()ing the output, assign it to an object output[i] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) } ## have the return value from the function be ## the object 'output' return(output) } I did not catch that i was not necessarily starting at 1 going sequentially up, so that would have to be done manually (or use cumsum per David rather than the function you wrote). cover_per2 - function(cover) { output - vector(numeric, length(min(cover):max(cover))) j - 1 for (i in min(cover):max(cover)) { output[j] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) j - j + 1 } return(output) } Josh __ 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 save this result in a vector
HI, David, Juan, and Joshua, Thanks so much for your help! The following codes works. Appreciated! test-seq(10, 342, by=2) #data is a vector cover_per-function (data) { output-vector(numeric,length(min(data):max(data))) for (i in min(data):max(data)) { output[i]-(100*sum(ifelse(data = i, 1, 0))/length(data)) } return(output) } result-cover_per(test) #*** #data is a vector cover_per-function (data) { output-numeric(0) for (i in min(data):max(data)) { x-(100*sum(ifelse(data = i, 1, 0))/length(data)) output-c(output, x) } return(output) } result-cover_per(test) On Sun, Oct 31, 2010 at 5:46 PM, David Winsemius dwinsem...@comcast.netwrote: On Oct 31, 2010, at 8:35 PM, Changbin Du wrote: HI, Dear R community, I have the following codes to calculate the commulative coverage. Not sure exactly what you mean by this. My guess is implemented below. I want to save the output in a vector, How to do this? test-seq(10, 342, by=2) #cover is a vector cover_per-function (cover) { for (i in min(cover):max(cover)) Using either for (i in cover) { ...} or for (i in seq_along(cover) ) {...} would be more typical. {print(100*sum(ifelse(cover = i, 1, 0))/length(cover))} } result-cover_per(test) Are you looking for cumsum? test-seq(10, 34, by=2) 100*cumsum(test)/sum(test) [1] 3.496503 7.692308 12.587413 18.181818 24.475524 31.468531 39.160839 [8] 47.552448 56.643357 66.433566 76.923077 88.111888 100.00 print(100*cumsum(test)/sum(test), digits=2) [1] 3.5 7.7 12.6 18.2 24.5 31.5 39.2 47.6 56.6 66.4 76.9 88.1 100.0 -- David Winsemius, MD West Hartford, CT -- Sincerely, Changbin -- Changbin Du DOE Joint Genome Institute [[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] how to save this result in a vector
Thanks Joshua! Yes, i is not going up sequentially by 1, as i here is the raw number of reads for each DNA base. Thanks so much for the great help! On Sun, Oct 31, 2010 at 6:03 PM, Joshua Wiley jwiley.ps...@gmail.comwrote: On Sun, Oct 31, 2010 at 5:44 PM, Joshua Wiley jwiley.ps...@gmail.com wrote: snip #cover is a vector cover_per - function(cover) { ## create a vector to store the results of your for loop output - vector(numeric, length(min(cover):max(cover))) for (i in min(cover):max(cover)) { ## rather than print()ing the output, assign it to an object output[i] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) } ## have the return value from the function be ## the object 'output' return(output) } I did not catch that i was not necessarily starting at 1 going sequentially up, so that would have to be done manually (or use cumsum per David rather than the function you wrote). cover_per2 - function(cover) { output - vector(numeric, length(min(cover):max(cover))) j - 1 for (i in min(cover):max(cover)) { output[j] - 100*sum(ifelse(cover = i, 1, 0))/length(cover) j - j + 1 } return(output) } Josh -- Sincerely, Changbin -- Changbin Du DOE Joint Genome Institute Bldg 400 Rm 457 2800 Mitchell Dr Walnut Creet, CA 94598 Phone: 925-927-2856 [[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.