John Owens <john_owens <at> yahoo.com> writes:

> 
> I'm having some trouble figuring out how to change a column in a DataFrame.

Laurent, thanks for the careful and thoughtful replies. May I offer a
more realistic example and ask three questions? What I would like to do
is read in a csv with a Date column into R, then manipulate that column
in python, then put the manipulated result as a date back into R. I
am aware that R has some date-manipulation commands, but they're not
nearly as complete as python's.

#!/usr/bin/env python2.6
import rpy2.robjects as ro

df = ro.DataFrame({'a': ro.StrVector(("Mar-15-2010", 
                                      "Mar-16-2010", 
                                      "Mar-17-2010")), 
                   'b': ro.IntVector((4,5,6))})
print df
print df.colnames
print df.rx2('a')
print [x for x in df.rx2('a')]    # question 1

df[0] = ro.StrVector(("2010-03-15",  # question 2
                      "2010-03-16",
                      "2010-03-17"));
print df.rx2('a')
# question 3

Question 1: What I want is to see three text strings (the dates
starting with "Mar"). Instead, what I see is [1, 2, 3]. I can't
manipulate [1, 2, 3]. How do I get the text strings back out
instead of [1, 2, 3]? (And what are [1, 2, 3] - a factorvector
kind of representation?)

Question 2: Is there any way to do df['a'] instead of df[0]? 
How do I find out what the index is for a particular column name?

Question 3: How do I apply the R 'as.date' function to a
column in a DataFrame, since that's what I need to do to treat
them as dates in R?

Once I get this working I'm happy to write it up as an example for
the docs (tell me where you want me to put it in the source distro
and I'll send a patch file).

JDO


------------------------------------------------------------------------------

_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to