[R] Adding a column into the file

Sarah Goslee sarah.goslee at gmail.com
Fri Jun 1 20:36:30 CEST 2012


Hi,

On Fri, Jun 1, 2012 at 12:27 PM, pigpigmeow <glorykwok at hotmail.com> wrote:
> Dear all,
> I have a lot of problems on R-programming.
> for example
> my csv. file is ..
> Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta
> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664
> 22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227
> 23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493
> 24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663
> 25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321
> 26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856

Did you read it into R with read.table() or read.csv()?

> i calculate the ratio of wrfRH/wrfsolar for each day.

And got XXXX, YYYY, ZZZZ - I don't think so. Is that part of the question?

myratio <- with(mydata, wrfRH/wrfsolar) might do it.

>
> that is
> Date                  wrfRH wrfsolar wrfwindspeed wrfrain wrftd wrfta ratio
> 21/10/2010 92.97 22.11 53.27 0 1546.337861 61.00852664  XXXX
> 22/10/2010 87.35 21.99 40.89 0 1300.408288 62.85352227  YYYY
> 23/10/2010 88.38 21.71 28.04 0.01 1381.768284 54.80594493 ZZZZ
> 24/10/2010 92.32 15.45 22.38 0.51 1113.90981 39.46573663 AAAA
> 25/10/2010 93.42 21.59 35.5 0.52 868.4895334 28.42952321 BBBB
> 26/10/2010 93.38 20.15 42.58 0.07 1404.722837 40.29300856  CCCC
>
> how to add new column in the original file ? Anyone can help me?!

First add it to your data frame with cbind(). Then you can use
write.table() or write.csv() to overwrite your original file, if you
really want to do that.

And please, as I already recommended, read some of the excellent
introductory material available online. This is very basic stuff, and
some evidence that you'd tried to work it out on your would would
encourage the volunteers here to help you out.

Reading the posting guide wouldn't be a bad idea either.

Sarah


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list