Hi,
I have used this command :
resamples<-lapply(1:1000,function(i) sample(lambs,replace=F))
resamples2<-lapply(resamples,Cusum)
to get a list of 1000 samples of my data. The function Cumsum is defined as
follows:
Cusum<-function(x){
SUM<-cumsum(x)-(1:length(x))*mean(x)
min<-min(cumsum(x)-(1:length(x))*mean(x))
max<-max(cumsum(x)-(1:length(x))*mean(x))
diff<-max-min
ans<-c(min,max,diff)
ans
}
where lambs is a vector of temperatures.
An example of part of my list is:
[[998]]
[1] -5.233176 6.903034 12.136210
[[999]]
[1] -9.296690 1.516233 10.812922
[[1000]]
[1] -1.502066e+01 -4.547474e-13 1.502066e+01
Now I want to convert this list into a dataframe so for example 1000 rows
with col names Min, Max and Diff. My supervisor said I first had to turn
this into a vector but I don't seem to be able to do that!
Any ideas on how to turn this list into a dataframe would be really
appreciated :) Thanks in advance
Melissa
--
View this message in context:
http://www.nabble.com/turning-list-into-vector-dataframe-tp22984623p22984623.html
Sent from the R help mailing list archive at Nabble.com.
Hi Melissa, L <- list(min=rnorm(5),mean=rnorm(5),max=rnorm(5)) matrix(unlist(L),ncol=3) gives what you want Alain Melissa2k9 wrote:> Hi, > > I have used this command : > > resamples<-lapply(1:1000,function(i) sample(lambs,replace=F)) > resamples2<-lapply(resamples,Cusum) > > to get a list of 1000 samples of my data. The function Cumsum is defined as > follows: > > Cusum<-function(x){ > SUM<-cumsum(x)-(1:length(x))*mean(x) > min<-min(cumsum(x)-(1:length(x))*mean(x)) > max<-max(cumsum(x)-(1:length(x))*mean(x)) > diff<-max-min > ans<-c(min,max,diff) > ans > } > > > where lambs is a vector of temperatures. > > An example of part of my list is: > > [[998]] > [1] -5.233176 6.903034 12.136210 > > [[999]] > [1] -9.296690 1.516233 10.812922 > > [[1000]] > [1] -1.502066e+01 -4.547474e-13 1.502066e+01 > > Now I want to convert this list into a dataframe so for example 1000 rows > with col names Min, Max and Diff. My supervisor said I first had to turn > this into a vector but I don't seem to be able to do that! > > Any ideas on how to turn this list into a dataframe would be really > appreciated :) Thanks in advance > > Melissa >-- Alain Guillet Statistician and Computer Scientist SMCS - Institut de statistique - Universit? catholique de Louvain Bureau d.126 Voie du Roman Pays, 20 B-1348 Louvain-la-Neuve Belgium tel: +32 10 47 30 50
Dear Melissa,
Use sapply instead of lapply and name the output vector of your Cusum
function. Note that I have simplified that function.
Cusum <- function(x){
SUM <- cumsum(x) - seq_along(x) * mean(x)
c(Min = min(SUM), Max = max(SUM), Diff = diff(range(SUM)))
}
lambs <- rnorm(10)
resamples <- lapply(1:1000,function(i) sample(lambs, replace = FALSE))
resamples2 <- t(sapply(resamples, Cusum))
HTH,
Thierry
------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
Namens Melissa2k9
Verzonden: vrijdag 10 april 2009 10:04
Aan: r-help at r-project.org
Onderwerp: [R] turning list into vector/dataframe
Hi,
I have used this command :
resamples<-lapply(1:1000,function(i) sample(lambs,replace=F))
resamples2<-lapply(resamples,Cusum)
to get a list of 1000 samples of my data. The function Cumsum is defined
as
follows:
Cusum<-function(x){
SUM<-cumsum(x)-(1:length(x))*mean(x)
min<-min(cumsum(x)-(1:length(x))*mean(x))
max<-max(cumsum(x)-(1:length(x))*mean(x))
diff<-max-min
ans<-c(min,max,diff)
ans
}
where lambs is a vector of temperatures.
An example of part of my list is:
[[998]]
[1] -5.233176 6.903034 12.136210
[[999]]
[1] -9.296690 1.516233 10.812922
[[1000]]
[1] -1.502066e+01 -4.547474e-13 1.502066e+01
Now I want to convert this list into a dataframe so for example 1000
rows
with col names Min, Max and Diff. My supervisor said I first had to turn
this into a vector but I don't seem to be able to do that!
Any ideas on how to turn this list into a dataframe would be really
appreciated :) Thanks in advance
Melissa
--
View this message in context:
http://www.nabble.com/turning-list-into-vector-dataframe-tp22984623p2298
4623.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________
R-help at 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.
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.
try this, do.call(rbind, resample2) #or simply, replicate(1000, Cusum(sample(lambs,replace=F))) you could also look at the plyr package. Hope this helps, baptiste On 10 Apr 2009, at 09:03, Melissa2k9 wrote:> > Hi, > > I have used this command : > > resamples<-lapply(1:1000,function(i) sample(lambs,replace=F)) > resamples2<-lapply(resamples,Cusum) > > to get a list of 1000 samples of my data. The function Cumsum is > defined as > follows: > > Cusum<-function(x){ > SUM<-cumsum(x)-(1:length(x))*mean(x) > min<-min(cumsum(x)-(1:length(x))*mean(x)) > max<-max(cumsum(x)-(1:length(x))*mean(x)) > diff<-max-min > ans<-c(min,max,diff) > ans > } > > > where lambs is a vector of temperatures. > > An example of part of my list is: > > [[998]] > [1] -5.233176 6.903034 12.136210 > > [[999]] > [1] -9.296690 1.516233 10.812922 > > [[1000]] > [1] -1.502066e+01 -4.547474e-13 1.502066e+01 > > Now I want to convert this list into a dataframe so for example 1000 > rows > with col names Min, Max and Diff. My supervisor said I first had to > turn > this into a vector but I don't seem to be able to do that! > > Any ideas on how to turn this list into a dataframe would be really > appreciated :) Thanks in advance > > Melissa > -- > View this message in context: http://www.nabble.com/turning-list-into-vector-dataframe-tp22984623p22984623.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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._____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag