Re: [R] Setting up a blank table with column names in the hard drive

2007-06-24 Thread Duncan Murdoch
On 23/06/2007 7:51 PM, Yuchen Luo wrote:
> Dear Professor Murdoch.
> Thank you so much! Your help is highly appreciated!

Please send replies to the mailing list.
> 
> When I use the following commands, there is a blank cell before title "name"
> in the resulting csv (excel) file. I am wondering how to get rid of it? I
> want to get rid of it because when I write subsequent rows to the file, the
> "name" part start from the beginning which is not aligned with the first
> line of the column titles.

That's a place holder for the row names.  If you don't want it, specify 
row.names=FALSE (and do the same when you write subsequent lines).

Duncan Murdoch

> 
> " blank <- data.frame(name=character(0), wife=character(0),
> no.children=numeric(0))
> write.csv(blank, 'file.csv')"
> 
> Thank you for your help again and your time is highly appreciated!
> 
> Best Wishes
> Yuchen Luo
> I
> 
> 
> On 6/23/07, Duncan Murdoch <[EMAIL PROTECTED]> wrote:
>> Yuchen Luo wrote:
>>> Dear Friends.
>>> Greetings!
>>>
>>> This should be a very common operation and I believe there should be a
>> nice
>>> way in R to handle it.  I couldn't find it in the manual or by searching
>> on
>>> line. I am wondering if I could ask for some help in this community.
>>>
>>>
>>>
>>> I am trying to record the results of my program to a csv file in the
>> hard
>>> drive so as to save memory space and also to read the results in excel
>> after
>>> running the program.  Every loop of my program will result in a list
>> with
>>> element belonging to different class. For example, things like
>>>
>>>
>>>
>>> a1 <- list(name="Fred", wife="Mary", no.children=3)
>>> a2 <- list(name="Tom", wife="Joy", no.children=9)
>>> a3 <- list(name="Paul", wife="Alic", no.children=5)
>>>
>>>
>>>
>>> I want the columns to have titles, in the example above, I want to see
>> the
>>> title "name", "wife" and "no.children" in the excel file.
>>>
>>>
>>>
>>> To set up the table in the csv file, I need to add at least one row in
>> the
>>> table up front. How ever, I do not have the first loop of the program
>>> completed yet at that time. If I add a row that is meaningless, how may
>> I
>>> delete it after all the loops are completed and all the meaningful rows
>> are
>>> added to the table?
>>>
>>>
>> I'd use data frames rather than plain lists for the results, so
>> write.csv will work.
>>
>> Create a data frame with 0 rows, and write it out:  this will give you
>> your header line.
>>
>> e.g.
>>
>>
>>
>> Now you can rbind new lines onto the data frame as you calculate new
>> records and rewrite the whole thing, or just append them to the file
>> with append=TRUE and writing with col.names=FALSE.
>>
>> Duncan Murdoch
>>
>>
>

__
R-help@stat.math.ethz.ch 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] Setting up a blank table with column names in the hard drive

2007-06-23 Thread Dieter Menne
Yuchen Luo  gmail.com> writes:

> This should be a very common operation and I believe there should be a nice
> way in R to handle it.  I couldn't find it in the manual or by searching on
> line. I am wondering if I could ask for some help in this community.
> 
> I am trying to record the results of my program to a csv file in the hard
> drive so as to save memory space and also to read the results in excel after
> running the program.  Every loop of my program will result in a list with
> element belonging to different class. For example, things like
> 
> a1 <- list(name="Fred", wife="Mary", no.children=3)
> a2 <- list(name="Tom", wife="Joy", no.children=9)
> a3 <- list(name="Paul", wife="Alic", no.children=5)
> 
> I want the columns to have titles, in the example above, I want to see the
> title "name", "wife" and "no.children" in the excel file.


Use a data frame to do the work, and save it with write.table or write.csv

maxallocate=10 # we assume no more than 10 members
myfamily = data.frame(name=rep(NA,maxallocate),wife=NA,nchildren=NA)
myfamily[1,]=c("Fred","Ginger",3)
myfamily[2,]=c("Charles","Mary",1)
myfamily[4,]=c("Frank","Salsa",4)
myfamily$name[3]="Anton"
myfamily$wife[3]="Sue"
myfamily$name[10] = "Charly"
myfamily$name[8] = "Fuhrman"
myfamily= myfamily[-1,] # delete first row
myfamily= myfamily[-1,] # delete current first row, i.e. Charles
# cleanup: assume that all entries having a name are valid
myfamily = myfamily[!is.na(myfamily$name),]
# oops .. I forgot .. another family member turned up unexpectedly
#Add it explicitely
rbind(myfamily,c("Tango","Tanga",33))
# The easy part. Check write.table for other options
write.csv(myfamily,file="myfamily.csv",row.names=FALSE)

__
R-help@stat.math.ethz.ch 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] Setting up a blank table with column names in the hard drive

2007-06-23 Thread Duncan Murdoch
Yuchen Luo wrote:
> Dear Friends.
> Greetings!
>
> This should be a very common operation and I believe there should be a nice
> way in R to handle it.  I couldn't find it in the manual or by searching on
> line. I am wondering if I could ask for some help in this community.
>
>
>
> I am trying to record the results of my program to a csv file in the hard
> drive so as to save memory space and also to read the results in excel after
> running the program.  Every loop of my program will result in a list with
> element belonging to different class. For example, things like
>
>
>
> a1 <- list(name="Fred", wife="Mary", no.children=3)
> a2 <- list(name="Tom", wife="Joy", no.children=9)
> a3 <- list(name="Paul", wife="Alic", no.children=5)
>
>
>
> I want the columns to have titles, in the example above, I want to see the
> title "name", "wife" and "no.children" in the excel file.
>
>
>
> To set up the table in the csv file, I need to add at least one row in the
> table up front. How ever, I do not have the first loop of the program
> completed yet at that time. If I add a row that is meaningless, how may I
> delete it after all the loops are completed and all the meaningful rows are
> added to the table?
>
>   
I'd use data frames rather than plain lists for the results, so 
write.csv will work.

Create a data frame with 0 rows, and write it out:  this will give you 
your header line.

e.g.

blank <- data.frame(name=character(0), wife=character(0), 
no.children=numeric(0))
write.csv(blank, 'file.csv')

Now you can rbind new lines onto the data frame as you calculate new 
records and rewrite the whole thing, or just append them to the file 
with append=TRUE and writing with col.names=FALSE.

Duncan Murdoch

__
R-help@stat.math.ethz.ch 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.