[R] data.frame query
David Winsemius
dwinsemius at comcast.net
Thu Oct 21 19:27:23 CEST 2010
On Oct 21, 2010, at 12:52 PM, Etn wrote:
> Hi All,
>
> Apologies for the simplicity of my question, but I would be grateful
> for any
> advice. Thanks
>
> I'm trying to put the output from a for loop into a data frame,
> however I
> have not been successful.
>
> The steps I have taken are:
>
>
>
> *R-code:*
>
>> for (k in 1:(nt-1-n0) ){
>> n<- n0-1+k
>> lam=n/nt
>> Q=x[n]
so n=n0+1+(1:(nt-1-n0))
and lam=(n0+1+(1:(nt-1-n0))/nt
and Q=x[0+1+(1:(nt-1-n0))]
Using the parameters below:
nt=20
n0=5
x=c(1:20)
> output3 = data.frame(k = 1:(nt-1-n0),
+ n = n0+1+(1:(nt-1-n0)),
+ lam = (n0+1+(1:(nt-1-n0)))/nt ,
+ Q = x[0+1+(1:(nt-1-n0))] )
(I don't get what you calculated but you should be able to work toward
a solution.)
> output3
k n lam Q
1 1 7 0.35 2
2 2 8 0.40 3
3 3 9 0.45 4
4 4 10 0.50 5
5 5 11 0.55 6
6 6 12 0.60 7
7 7 13 0.65 8
8 8 14 0.70 9
9 9 15 0.75 10
10 10 16 0.80 11
11 11 17 0.85 12
12 12 18 0.90 13
13 13 19 0.95 14
14 14 20 1.00 15
>
>> output1<-data.frame(cbind(k,n,lam,Q))
>> output1
>> }
>
>> output1
>
>
> *R-Output *
> k n lam Q
> 1 14 18 0.9 18
>
>
> I would like the output in this format, but for all the values of k
> (i.e.
> 1-14 as opposed to just the last value)
>
>
> I have also tried
>
> *R-code:*
>
>> nt=20
>> n0=5
>> x=c(1:20)
>> for (k in 1:(nt-1-n0) ){
>> n<- n0-1+k
>> lam=n/nt
>> Q=x[n]
>
>> output1<-data.frame(cbind(k,n,lam,Q))
>> print(output1)
>> }
>
>
> *R-Output *
>
> k n lam Q
> 1 1 5 0.25 5
> k n lam Q
> 1 2 6 0.3 6
> k n lam Q
> 1 3 7 0.35 7
> k n lam Q
> 1 4 8 0.4 8
> k n lam Q
> 1 5 9 0.45 9
> k n lam Q
> 1 6 10 0.5 10
> k n lam Q
> 1 7 11 0.55 11
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list