Skip to content

turning list into vector/dataframe

4 messages · Melissa2k9, Alain Guillet, ONKELINX, Thierry +1 more

#
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
#
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:

  
    
#
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
#
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:

            
_____________________________

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