[R] Appending data to a data.frame and writing a csv

Ista Zahn izahn at psych.rochester.edu
Fri Mar 25 17:02:41 CET 2011


Hi Vincy,
Please read the help file, particularly the part about write.csv and
write.csv2 where it says "These wrappers are deliberately inflexible:
they are designed to ensure that the correct conventions are used to
write a valid file. Attempts to change append, col.names, sep, dec or
qmethod are ignored, with a warning."

Use write.table instead.

Best,
Ista

On Fri, Mar 25, 2011 at 8:55 AM, Vincy Pyne <vincy_pyne at yahoo.ca> wrote:
> Dear R helpers
>
> exposure <- data.frame(id = c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20),
> ead = c(9483.686,50000,6843.4968,10509.37125,21297.8905,50000,706152.8354, 62670.5625, 687.801995,50641.4875,59227.125,43818.5778,52887.72534,601788.7937, 56813.14859,4012356.056,1419501.179,210853.4743,749961,6599.0862),
> pd = c(0.0191,0.0050,0.0298,0.0449,0.0442,0.0479,0.0007,0.0203,0.0431,0.0069, 0.0122,0.0022,0.0016,0.0082,0.0109,0.0008,0.0142,0.0171,0.0276,0.0178),
> lgd = c(0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45,0.45, 0.45,0.45,0.45,0.45))
>
> param <- data.frame(alpha = 0.99, size = 50)    # size is basically no of simulations
>
>
> n          <-
>  length(exposure$id)
> id         <- exposure$id
> ead        <- exposure$ead
> lgd        <- exposure$lgd
> pd         <- exposure$pd
> alpha      <- param$alpha
> samplesize <- param$size
>
> ## generate random numbers s.t. 1 = Default, 0 = no-default.
>
> L <- matrix(data=NA, nrow=n, ncol=samplesize, byrow=TRUE)
>
> for(i in 1:n)
>     L[i,] <- rbinom(n=samplesize, size=1, prob=exposure$pd[i])
>
> # ________________________________________________________________
>
> # compute for each simulation
>
> p_loss <- e_loss <- u_loss <- NULL
>
> for(i in 1:samplesize)
>
> {
>
> defaulting <- subset(data.frame(id=exposure$id, ead=exposure$ead, lgd=exposure$lgd, pd=exposure$pd, loss=L[,i]),
>  loss==1)
>
> p_loss[i]  <- sum(defaulting$ead * defaulting$lgd)
> e_loss[i]  <- sum(defaulting$ead * defaulting$lgd * defaulting$pd)
> u_loss[i]  <- sum(sqrt((defaulting$ead*defaulting$lgd)^2*defaulting$pd - (defaulting$ead * defaulting$lgd * defaulting$pd)^2))
>
> sim_data   <- data.frame(sim_no=rep(i,length(defaulting$id)), id=defaulting$id, ead=defaulting$ead, lgd=defaulting$lgd, pd=defaulting$pd)
>
> write.csv(sim_data, file='sim_data.csv', append=TRUE, row.names=FALSE)
>
> }
>
> For a given set of 0's and 1's (i.e. for each simulation and there are 50 simulations), first I filter all the entries corresponding to 0's i.e. for a given simulation, I need to store ead, lgd and pd pertaining to only non-zeros i.e. pertaining to 1. Thus, for each of these 50 simulations, I need to define a data.frame giving me filtered ead, lgd and pd and in teh end write a single file sim_data.csv
>
> I get
>  following warnings.
>
> Warning messages:
> 1: In write.csv(sim_data, file = "sim_data.csv", append = TRUE,  ... :
>   attempt to set 'append' ignored
> 2: In write.csv(sim_data, file = "sim_data.csv", append = TRUE,  ... :
>   attempt to set 'append' ignored
> .....................................................................................................
>
> .....................................................................................................
> 50: In write.csv(sim_data, file = "sim_data.csv", append = TRUE,  ... :
>
>   attempt to set 'append' ignored
>
> Kindly guide
>
> Regards
>
> Vincy
>
>
>
>
>        [[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.
>
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org



More information about the R-help mailing list